示例#1
0
    def test_read_only(self):
        """ Unit test """
        dbdriver = DbDriver.__new__(DbDriver)

        dbdriver._DbDriver__read_only = True
        self.assertTrue(dbdriver.read_only)
        dbdriver._DbDriver__read_only = False
        self.assertFalse(dbdriver.read_only)

        with self.assertRaises(TypeError):
            dbdriver.read_only()
示例#2
0
    def test_get_item(self):
        """ Unit test """
        dbdriver = DbDriver.__new__(DbDriver)
        table = MagicMock()
        dbdriver._DbDriver__tables = {"MyTable": table}
        dbdriver._load_table = MagicMock()

        with self.assertRaises(KeyError):
            dbdriver["NotExistingTable"]
        result = dbdriver["MyTable"]
        dbdriver._load_table.assert_not_called()
        self.assertIs(result, table)
示例#3
0
 def test_get_item_not_loaded(self):
     """ Unit test """
     # arrange
     dbdriver = DbDriver.__new__(DbDriver)
     table = MagicMock()
     dbdriver._DbDriver__tables = {"MyTable": None}
     dbdriver._load_table = MagicMock()
     dbdriver._load_table.return_value = table
     # act
     result = dbdriver["MyTable"]
     # assert
     dbdriver._load_table.assert_called_once_with("MyTable")
     self.assertIs(result, table)
示例#4
0
    def _split_db(self):
        """
        This is used to split data in DB to correspond only to the
        single unit of analysis. Function passed by user can use all
        the DB instance data given to it, and be sure that they are
        isolated from data corresponding to other units.
        """
        # prepare indexes
        db_refs = DbReferences(self.source_db, self.granularity)

        # prepare units list
        if self.unit_granularity is None:
            units = self.source_db[self.granularity].find({})
        else:
            # check if unit is correctly set
            self.source_db[self.unit_granularity][self.unit_id]
            units = db_refs.get_relation(
                _from=self.unit_granularity,
                _to=self.granularity,
                _id=self.unit_id,
            )

        # make DB driver instance for each unit
        for unit_id in units:
            # get IDs of records in tables
            gmina_ids = db_refs.get_gmina(unit_id)
            powiat_ids = db_refs.get_powiat(unit_id)
            okreg_ids = db_refs.get_okreg(unit_id)
            voivodship_ids = db_refs.get_voivodship(unit_id)
            obwody_ids = db_refs.get_obwod(unit_id)
            protocole_ids = db_refs.get_protocole(unit_id)
            list_ids = db_refs.get_list(unit_id)
            candidate_ids = db_refs.get_candidate(unit_id)
            mandate_ids = db_refs.get_mandate(unit_id)
            wyniki_ids = db_refs.get_wyniki(unit_id)

            tables_and_ids = {
                "gminy": gmina_ids,
                "powiaty": powiat_ids,
                "okręgi": okreg_ids,
                "województwa": voivodship_ids,
                "obwody": obwody_ids,
                "protokoły": protocole_ids,
                "listy": list_ids,
                "kandydaci": candidate_ids,
                "mandaty": mandate_ids
            }
            tables_and_ids.update(wyniki_ids)

            # create db driver instance
            db = DbDriver.__new__(DbDriver)
            db._DbDriver__read_only = False
            db._DbDriver__tables = {}
            db._DbDriver__dropped_tables = []

            # copy records
            for table_name, ids_list in tables_and_ids.items():
                db.create_table(table_name)
                for _id in ids_list:
                    record = self.source_db[table_name][_id]
                    db[table_name].put(dict(record), _id=_id)

            # freeze db and conclude iteration
            db._DbDriver__read_only = True
            yield db