def test_simple_export_case(self): customer = CustomerFactory() u1_stop = datetime(2018, 5, 28, 18, 38, 0) u1 = UserFactory( stop_timestamp=u1_stop, customer=customer, ) u2 = UserFactory( stop_timestamp=None, customer=customer, ) data = json.loads(export_to_json(self.session, customer.id)) self.assertEqual([ dict( member_number=int(u1.name), rfid_tag=u1.card, end_timestamp="2018-05-28T16:38:00.000000Z", ), dict( member_number=int(u2.name), rfid_tag=u2.card, end_timestamp=None, ), ], data)
def test_duplicate_member_id_fails_fatally(self): c = CustomerFactory() UserFactory(name="1001", customer=c) UserFactory(name="1001", customer=c) with self.assertRaises(SystemExit): get_multi_access_members(self.session, self.tui, customer_id=c.id)
def test_does_not_export_data_for_other_custoemr(self): c1 = CustomerFactory() u1 = UserFactory(customer=c1) c2 = CustomerFactory() u2 = UserFactory(customer=c2) c3 = CustomerFactory() u3 = UserFactory(customer=c3) data = json.loads(export_to_json(self.session, c2.id)) self.assertEqual(1, len(data)) self.assertEqual(int(u2.name), data[0]['member_number'])
def test_multiple_unsorted_no_diff(self): ds = [DbMember(UserFactory(name=str(i))) for i in range(1001, 1010)] shuffle(ds) ms = [MakerAdminMemberFactory(member_number=i) for i in range(1001, 1010)] shuffle(ms) self.assertEqual([], BlockMember.find_diffs(ds, ms))
def test_get_members_ignores_after_whitespace(self): c = CustomerFactory() UserFactory(name="1001 SUNE", customer=c) u, = get_multi_access_members(self.session, self.tui, customer_id=c.id) self.assertEqual(1001, u.member_number)
def test_multiple_unsorted_no_diff(self): ds = [DbMember(UserFactory(stop_timestamp=self.datetime(days=i), name=str(i), card=str(i))) for i in range(1001, 1010)] shuffle(ds) ms = [MakerAdminMemberFactory(member_number=i, end_timestamp=self.datetime(days=i), rfid_tag=str(i)) for i in range(1001, 1010)] shuffle(ms) self.assertEqual([], UpdateMember.find_diffs(ds, ms))
def test_update_diff_blocks_member(self): u = UserFactory(stop_timestamp=self.datetime(days=1), name="1001", blocked=False, card="1") d = DbMember(u) BlockMember(d).update(self.session, self.tui) self.session.refresh(u) self.assertEqual(self.datetime(days=1), u.stop_timestamp) self.assertEqual("1", u.card) self.assertTrue(u.blocked)
def test_export_bad_member_is_filterd(self): customer = CustomerFactory() u1 = UserFactory( name="1234 Things", customer=customer, ) data = json.loads(export_to_json(self.session, customer.id)) self.assertEqual([], data)
def test_update_diff_changes_timestamp_when_called(self): u = UserFactory(stop_timestamp=self.datetime(days=1), name="1001", blocked=False) d = DbMember(u) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(days=2)) etd = UpdateMember(d, m) etd.update(self.session, self.tui) self.session.refresh(u) self.assertEqual(self.datetime(days=2), u.stop_timestamp) self.assertFalse(u.blocked)
def test_update_diff_changes_all_three_values_when_both_diffs(self): u = UserFactory(stop_timestamp=self.datetime(days=1), name="1001", blocked=True, card="1") d = DbMember(u) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(days=2), rfid_tag="2") etd = UpdateMember(d, m) etd.update(self.session, self.tui) self.session.refresh(u) self.assertEqual(self.datetime(days=2), u.stop_timestamp) self.assertEqual("2", u.card) self.assertFalse(u.blocked)
def test_block_one_member(self): c = CustomerFactory() a = AuthorityFactory() u = UserFactory(stop_timestamp=self.datetime(), name="1001", customer=c) self.client.fetch_members = MagicMock(return_value=[]) sync(session=self.session, client=self.client, ui=self.ui, customer_id=c.id, authority_id=a.id) u = self.session.query(User).get(u.id) self.assertTrue(u.blocked)
def test_no_update_is_made_when_user_breaks(self): c = CustomerFactory() a = AuthorityFactory() old_stop = self.datetime(days=30) u = UserFactory(stop_timestamp=old_stop, name="1001", customer=c) new_stop = self.datetime(days=50) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=new_stop) self.client.fetch_members = MagicMock(return_value=[m]) with self.assertRaises(SystemExit): sync(session=self.session, client=self.client, ui=self.ui, customer_id=c.id, authority_id=a.id) u = self.session.query(User).get(u.id) self.assertEqual(old_stop, u.stop_timestamp)
def test_sync_updates_end_timestamp(self): c = CustomerFactory() a = AuthorityFactory() old_stop = self.datetime(days=30) u = UserFactory(stop_timestamp=old_stop, name="1001 KUNO", customer=c) new_stop = self.datetime(days=50) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=new_stop) self.client.fetch_members = MagicMock(return_value=[m]) sync(session=self.session, client=self.client, ui=self.ui, customer_id=c.id, authority_id=a.id) u = self.session.query(User).get(u.id) self.assertEqual(new_stop, u.stop_timestamp) self.assertEqual("1001 KUNO", u.name)
def test_sync_updates_rfid_tag(self): c = CustomerFactory() a = AuthorityFactory() u = UserFactory(stop_timestamp=self.datetime(), name="1001", customer=c, card="1") m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(), rfid_tag="2") self.client.fetch_members = MagicMock(return_value=[m]) sync(session=self.session, client=self.client, ui=self.ui, customer_id=c.id, authority_id=a.id) u = self.session.query(User).get(u.id) self.assertEqual("2", u.card)
def test_multiple_diffs_and_multiple_non_diffs_works(self): d1 = DbMember(UserFactory(stop_timestamp=self.datetime(days=1), name="1001", card="1", blocked=False)) d2 = DbMember(UserFactory(stop_timestamp=self.datetime(days=2), name="1002", card="2", blocked=False)) d3 = DbMember(UserFactory(stop_timestamp=self.datetime(days=3), name="1003", card="3", blocked=False)) d4 = DbMember(UserFactory(stop_timestamp=self.datetime(days=4), name="1004", card="4", blocked=True)) d5 = DbMember(UserFactory(stop_timestamp=self.datetime(days=5), name="1005", card="X", blocked=False)) dx = DbMember(UserFactory(stop_timestamp=self.datetime(days=0), name="2001", card="1", blocked=False)) m1 = MakerAdminMemberFactory(member_number=1001, rfid_tag="1", end_timestamp=self.datetime(days=1)) m2 = MakerAdminMemberFactory(member_number=1002, rfid_tag="2", end_timestamp=self.datetime(days=8)) m3 = MakerAdminMemberFactory(member_number=1003, rfid_tag="3", end_timestamp=self.datetime(days=3)) m4 = MakerAdminMemberFactory(member_number=1004, rfid_tag="4", end_timestamp=self.datetime(days=4)) m5 = MakerAdminMemberFactory(member_number=1005, rfid_tag="5", end_timestamp=self.datetime(days=5)) mx = MakerAdminMemberFactory(member_number=3001, rfid_tag="1", end_timestamp=self.datetime(days=1)) diffs = UpdateMember.find_diffs([d1, d2, d3, d4, d5, dx], [mx, m1, m2, m3, m4, m5]) self.assertIn(UpdateMember(d2, m2), diffs) self.assertIn(UpdateMember(d4, m4), diffs) self.assertIn(UpdateMember(d5, m5), diffs) self.assertEqual(3, len(diffs))
def test_no_timestamp_vs_no_timestamp_creates_no_diff(self): d = DbMember(UserFactory(stop_timestamp=None, name="1001", blocked=False, card="1")) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=None, rfid_tag="1") self.assertEqual([], UpdateMember.find_diffs([d], [m]))
def test_sync_with_multiple_changes_non_changes_and_other_customers(self): other_customer = CustomerFactory() other_authority = AuthorityFactory() customer = CustomerFactory() authority = AuthorityFactory() # Users to update. u1 = UserFactory(stop_timestamp=self.datetime(), name="1001", card="1", blocked=True, customer=customer) u2 = UserFactory(stop_timestamp=self.datetime(), name="1002", card="2", blocked=None, customer=customer) # Users to block. u3 = UserFactory(stop_timestamp=self.datetime(), name="1003", card="3", blocked=None, customer=customer) u4 = UserFactory(stop_timestamp=self.datetime(), name="1004", card="4", blocked=None, customer=customer) # Other users not to be touched. u5 = UserFactory(stop_timestamp=self.datetime(), name="1005", card="5", blocked=None, customer=other_customer) u6 = UserFactory(stop_timestamp=self.datetime(), name="1006", card="6", blocked=None, customer=other_customer) u7 = UserFactory(stop_timestamp=self.datetime(), name="1007", card="7", blocked=None, customer=other_customer) u8 = UserFactory(stop_timestamp=self.datetime(), name="1008", card="8", blocked=None, customer=other_customer) self.client.fetch_members = MagicMock(return_value=[ # Updates. MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(days=1), rfid_tag="1"), MakerAdminMemberFactory(member_number=1002, end_timestamp=self.datetime(), rfid_tag="22"), # Creates. MakerAdminMemberFactory(member_number=1007, end_timestamp=self.datetime(days=7), rfid_tag="77"), MakerAdminMemberFactory(member_number=1008, end_timestamp=self.datetime(days=8), rfid_tag="88"), ]) sync(session=self.session, client=self.client, ui=self.ui, customer_id=customer.id, authority_id=authority.id) self.session.refresh(u5) self.assertEqual("5", u5.card) self.assertEqual(self.datetime(), u5.stop_timestamp) self.session.refresh(u6) self.assertEqual("6", u6.card) self.assertEqual(self.datetime(), u6.stop_timestamp) self.session.refresh(u7) self.assertEqual("7", u7.card) self.assertEqual(self.datetime(), u7.stop_timestamp) self.session.refresh(u8) self.assertEqual("8", u8.card) self.assertEqual(self.datetime(), u8.stop_timestamp) [u1, u2, u3, u4, u7, u8] = self.session.query(User).filter_by(customer=customer).order_by( User.name) self.assertEqual("1001", u1.name) self.assertEqual("1", u1.card) self.assertEqual(False, u1.blocked) self.assertEqual(self.datetime(days=1), u1.stop_timestamp) self.assertEqual("1002", u2.name) self.assertEqual("22", u2.card) self.assertEqual(False, u2.blocked) self.assertEqual(self.datetime(), u2.stop_timestamp) self.assertEqual("1003", u3.name) self.assertEqual("3", u3.card) self.assertEqual(True, u3.blocked) self.assertEqual(self.datetime(), u3.stop_timestamp) self.assertEqual("1004", u4.name) self.assertEqual("4", u4.card) self.assertEqual(True, u4.blocked) self.assertEqual(self.datetime(), u4.stop_timestamp) self.assertEqual("1007", u7.name) self.assertEqual("77", u7.card) self.assertEqual(None, u7.blocked) self.assertEqual(self.datetime(days=7), u7.stop_timestamp) self.assertEqual("1008", u8.name) self.assertEqual("88", u8.card) self.assertEqual(None, u8.blocked) self.assertEqual(self.datetime(days=8), u8.stop_timestamp)
def test_excessive_db_user_creates_no_diff(self): d = DbMember(UserFactory(name="1001")) self.assertEqual([], AddMember.find_diffs([d], []))
def test_simple_no_diff(self): d = DbMember(UserFactory(name="1001")) m = MakerAdminMemberFactory(member_number=1001) self.assertEqual([], BlockMember.find_diffs([d], [m]))
def test_simple_diff_block_one_user(self): d = DbMember(UserFactory(name="1001")) self.assertEqual([BlockMember(d)], BlockMember.find_diffs([d], []))
def test_no_block_diff_if_already_blocked(self): d = DbMember(UserFactory(name="1001", blocked=True)) self.assertEqual([], BlockMember.find_diffs([d], []))
def test_two_different_members_creates_no_diff(self): d = DbMember(UserFactory(stop_timestamp=self.datetime(), name="1002", card="1")) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(), rfid_tag="1") self.assertEqual([], UpdateMember.find_diffs([d], [m]))
def test_negative_2_second_diff_creates_diff(self): d = DbMember(UserFactory(stop_timestamp=self.datetime(), name="1001", blocked=False, card="1")) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(seconds=-2), rfid_tag="1") self.assertEqual([UpdateMember(d, m)], UpdateMember.find_diffs([d], [m]))
def test_half_second_diff_creates_no_diff(self): d = DbMember(UserFactory(stop_timestamp=self.datetime(milliseconds=1), name="1001", blocked=False, card="1")) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(milliseconds=500), rfid_tag="1") self.assertEqual([], UpdateMember.find_diffs([d], [m]))
def test_diff_on_tag(self): d = DbMember(UserFactory(stop_timestamp=self.datetime(), name="1001", card="1")) m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(), rfid_tag="2") self.assertEqual([UpdateMember(d, m)], UpdateMember.find_diffs([d], [m]))
def test_multiple_diffs_and_multiple_non_diffs_works(self): ds = [DbMember(UserFactory(name=n)) for n in ["1001", "1002", "1003"]] ms = [MakerAdminMemberFactory(member_number=n) for n in [1001, 1003, 1004, 1005]] diffs = [d.db_member.member_number for d in BlockMember.find_diffs(ds, ms)] self.assertCountEqual([1002], diffs)
def test_fails_fatally_on_problem_member(self): c = CustomerFactory() UserFactory(name="SUNE 1001", customer=c) with self.assertRaises(SystemExit): get_multi_access_members(self.session, self.tui, customer_id=c.id)