# -*- coding: utf-8 -*-

import os
import unittest

from widukind_common.utils import get_mongo_db, create_or_update_indexes
from widukind_common import tests_tools as utils

from dlstats import constants

from dlstats.tests import resources

RESOURCES_DIR = os.path.abspath(os.path.dirname(resources.__file__))

class BaseTestCase(unittest.TestCase):
    
    def setUp(self):
        unittest.TestCase.setUp(self)

class BaseDBTestCase(BaseTestCase):
    """Tests with MongoDB or ElasticSearch
    """

    def setUp(self):
        BaseTestCase.setUp(self)
        
        if not os.environ.get("WIDUKIND_MONGODB_URL", None):
            self.fail("The environment variable WIDUKIND_MONGODB_URL must be set with the value: WIDUKIND_MONGODB_URL=mongodb://localhost/widukind_test")
        
        self.db = get_mongo_db()
        
        self.assertEqual(self.db.name, "widukind_test")

        utils.clean_mongodb(self.db)
                
        create_or_update_indexes(self.db, force_mode=True)

    def _collections_is_empty(self):
        self.assertEqual(self.db[constants.COL_CATEGORIES].count(), 0)
        self.assertEqual(self.db[constants.COL_PROVIDERS].count(), 0)
        self.assertEqual(self.db[constants.COL_DATASETS].count(), 0)
        self.assertEqual(self.db[constants.COL_SERIES].count(), 0)
        self.assertEqual(self.db[constants.COL_TAGS_DATASETS].count(), 0)
        self.assertEqual(self.db[constants.COL_TAGS_SERIES].count(), 0)
        

@@ -475,26 +475,9 @@
 goDB
- or ElasticSearch
 %0A
+
     
@@ -556,72 +556,36 @@
     
-if not os.environ.get(%22WIDUKIND_MONGODB_URL%22, None):%0A   
+db = get_mongo_db()%0A
         
  sel
@@ -584,192 +584,45 @@
     
- 
 self.
-fail(%22The environment variable WIDUKIND_MONGODB_URL must be set with the value: WIDUKIND_MONGODB_URL=mongodb://localhost/widukind_test%22)%0A        %0A        self.db = get_mongo_db()
+db = db.client%5B%22widukind_test%22%5D 
 %0A   

remove require WIDUKIND_MONGODB_URL environ variable for tests