def test_allocations_load_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 livingspace = LivingSpace('hwan') LivingSpace.add(livingspace) fellow = Fellow("onon", "ekek", "000009", "Y") fellow.register() livingspace.allocate_to(fellow) prev_allocations = Room.all_allocations().copy() Allocation.save_state(file_name) #clear memory stores self.clear_stores() empty_allocations = Room.all_allocations().copy() #db Allocation.load_state(file_name) loaded_allocations = Room.all_allocations().copy() #compare expected_output = ["HWAN", "LIVINGSPACE", "000009"] output = [prev_allocations[0], empty_allocations, loaded_allocations[0]] self.assertEqual(output, [expected_output, [], expected_output])
def test_print_existing_allocations_to_screen(self): self.clear_stores() office = Office("NDO2") Dojo.add_room(office) fellow = Fellow("Xone2", "Ndobile2", "0856443324", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() output = Room.members(allocations_, room_tag=True) expected_output = " NDO2-OFFICE, 0856443324, NDOBILE2, XONE2, FELLOW\n" self.assertEqual(output, expected_output)
def test_print_existing_allocations_to_default_file(self): self.clear_stores() office = Office("ND2") Dojo.add_room(office) fellow = Fellow("Xne2", "Ndoile2", "0868443324", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() expected_output = Room.members(allocations_, room_tag=True) Room.to_file(expected_output) path = "output/" f = open(path + "File.txt", "r") output = f.read() #"NDO2-Office, 0856443324, NDOBILE2, XONE2, FELLOW" f.close() self.assertEqual(expected_output, output)
def save_state(cls, file_name="default"): allocations = Room.all_allocations() path = "db/" file_ = file_name + ".db" engine = create_engine("sqlite:///" + path + file_, echo=False) cls.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() for allocation_ in allocations: roomname = allocation_[0] roomtype = allocation_[1] phonenumber = allocation_[2] this_allocation = cls(roomname, roomtype, phonenumber) session.add(this_allocation) session.commit() session.close()
def test_print_existing_allocations_to_specific_file(self): self.clear_stores() office = Office("ND88") Dojo.add_room(office) fellow = Fellow("Xne88", "Ndoile88", "086800000", "y") fellow.register() office.allocate_to(fellow) allocations_ = Room.all_allocations() expected_output = Room.members(allocations_, room_tag=True) file_name = office.name + "-Allocations" Room.to_file(expected_output, file_name) path = "output/" f = open(path + file_name + ".txt", "r") output = f.read() f.close() self.assertEqual(expected_output, output)
def print_allocations(out, file_name): try: allocations = Room.all_allocations() output = Room.members(allocations, room_tag=True) if len(output) > 0: if out is True: if file_name is not None: Room.to_file(output, file_name) print_pretty( " A list of allocated persons can be found in the output directory under the file %s.txt" % file_name) else: Room.to_file(output) print_pretty( " A list of allocated persons can be found in the output directory under the file File.txt" ) else: print_pretty(output) else: print_pretty(" There are no allocations to show") except Exception as e: print_pretty(str(e))
def test_allocations_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 livingspace = LivingSpace('hwan') LivingSpace.add(livingspace) fellow = Fellow("onon", "ekek", "000009", "Y") fellow.register() livingspace.allocate_to(fellow) Allocation.save_state(file_name) #db engine = create_engine("sqlite:///"+path+file_, echo=False) Session = sessionmaker(bind=engine) session = Session() db_allocation = session.query(Allocation).first() #compare db_output = [db_allocation.roomname, db_allocation.roomtype, db_allocation.phonenumber] self.assertEqual(Room.all_allocations()[0], db_output) session.close()