def test_fellow_load_state(self): path = "db/" file_name = "mydb" file_ = file_name + ".db" #clean up to avoid conflict between tests os.remove(path + file_) #clear memory stores self.clear_stores() #memory fellow1 = Fellow("osdflkjsd", "eksdlkfjk", "774881228", "Y") fellow1.register() Person.save_state(file_name) #clear memory stores self.clear_stores() #memory Person.load_state(file_name) fellow = Fellow.from_phone("774881228") #db engine = create_engine("sqlite:///" + path + file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_fellow = session.query(Person).filter_by( phonenumber="774881228").first() session.close() #compare full_fellow = [ fellow.first_name, fellow.last_name, fellow.phone, fellow.type_, fellow.opt_in ] full_db_fellow = [ db_fellow.firstname, db_fellow.lastname, db_fellow.phonenumber, db_fellow.role, db_fellow.optin ] self.assertEqual(full_db_fellow, full_fellow)
def test_staff_save_state(self): path = "db/" file_name = "mydb" file_ = file_name + ".db" #clean up to avoid conflict between tests os.remove(path + file_) self.clear_stores() #memory staff = Staff("onsn", "edsk", "46546412") staff.register() Person.save_state(file_name) #db engine = create_engine("sqlite:///" + path + file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_staff = session.query(Person).filter_by( phonenumber="46546412").first() #compare full_staff = [ staff.first_name, staff.last_name, staff.phone, staff.type_, staff.opt_in ] full_db_staff = [ db_staff.firstname, db_staff.lastname, db_staff.phonenumber, db_staff.role, db_staff.optin ] session.close() self.assertEqual(full_staff, full_db_staff)
def save_state(file_name="default"): file_name = str(file_name) path = "db/" file_ = file_name + ".db" try: if os.path.isfile(path + file_): os.remove(path + file_) Person.save_state(file_name) LivingSpace.save_state(file_name) Office.save_state(file_name) Allocation.save_state(file_name) print_pretty( "The current state of the application has successfully been saved in the db directory under the file: %s." % file_) except exc.DBAPIError: print_pretty( str("There is currently a problem with the specified file please try a different one." )) except Exception as e: print_pretty(str(e))