def commit(self, db: DataBase): if db.find_obj(self) is None: self.__id = db.save_get_id(self) elif self.__is_changed: new_name = self.name if self.__new_name is None else self.__new_name new_country = self.country if self.__new_country is None else self.__new_country db.update(self, Recorder(new_name, new_country)) self.name, self.country = new_name, new_country self.__is_changed = False else: self.__id = db.get_id_obj(self)
def commit(self, db: DataBase): if db.find_obj(self) is None: self.__id = db.save_get_id(self) elif self.__is_changed: new_name = self.name if self.__new_name is None else self.__new_name new_soloists = self.soloists if self.soloists == self.__new_soloists else self.__new_soloists db.update(self, Author(new_name, new_soloists)) self.name, self.soloists = new_name, new_soloists self.__is_changed = False else: self.__id = db.get_id_obj(self) self.__new_soloists = []
def commit(self, db: DataBase): if db.find_obj(self) is None: self.__id = db.save_get_id(self) elif self.__is_changed: new_name_song = self.__new_name_song if self.__new_name_song is not None else self.name_song new_author = self.__new_author if self.__new_author is not None else self.author db.update(self, Song(new_name_song, new_author)) self.name_song = new_name_song self.author = new_author self.__is_changed = False else: self.__id = db.get_id_obj(self)
def commit(self, db: DataBase): if db.find_obj(self) is None: self.__id = db.save_get_id(self) elif self.__is_changed: new_name = self.__new_name if self.__new_name is not None else self.name new_year = self.__new_year if self.__new_year is not None else self.year new_recorder = self.__new_recorder if self.__new_recorder is not None else self.recorder new_songs = self.songs if self.songs == self.__new_songs else self.__new_songs new_types = self.types if self.types == self.__new_types else self.__new_types db.update( self, MediaProduct(new_name, new_types, new_recorder, new_year, new_songs)) self.name = new_name self.year = new_year self.recorder = new_recorder self.__types = new_types self.__songs = new_songs self.__is_changed = False else: self.__id = db.get_id_obj(self)
class TestDB(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestDB, self).__init__(*args, **kwargs) self.db = DataBase("test") def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.db.db.dropDatabase() def test_recorder_new(self): atlantic = Recorder("Atlantic", "USA") atlantic.commit(self.db) self.assertEqual({ "name": "Atlantic", "country": "USA" }, atlantic.__dict__()) self.assertEqual({ "name": "Atlantic", "country": "USA" }, self.db.db.recorder.find({"name": "Atlantic"}, {"_id": False})[0]) def test_update(self): expected = Recorder("boss", "huge") expected.commit(self.db) self.db.update(Recorder("asa", "sos"), expected) actual = self.db.db.recorder.find_one({"name": "boss"}, {"_id": False}) self.assertEqual(expected.__dict__(), actual) def test_author(self): itmoment = Author("in this moment", "maria brink", "eduard alal") itmoment.commit(self.db) expected = itmoment.__dict__() actual = self.db.db.author.find_one({"name": "in this moment"}, {"_id": False}) self.assertEqual(expected, actual) itmoment.add_soloist("f*g") actual = self.db.db.author.find_one({"name": "in this moment"}, {"_id": False}) expected = itmoment.__dict__() self.assertEqual(expected, actual) def test_get_id(self): a = Recorder("dfv", "dfgfd") a.commit(self.db) # print(self.db.get_id_obj(a)['_id']) def test_song(self): linda = Author("linda", "linda") linda.commit(self.db) vorona = Song("ya vorona", linda) vorona.commit(self.db) id = self.db.get_id_obj(vorona.author) actual = self.db.db.author.find_one({"_id": id}) vorona.name_song = "i am vorona" vorona.commit(self.db) # print(actual['name']) def test_media(self): pass