def test_update_diffs_fails_on_sanity_check_if_wrong_authority_name(self): c = CustomerFactory() a = AuthorityFactory(name="makerspace stockholm") m = MakerAdminMemberFactory() with self.assertRaises(Exception): update_diffs(self.session, self.tui, [AddMember(m)], customer_id=c.id, authority_id=a.id)
def test_add_one_member(self): c = CustomerFactory() a = AuthorityFactory() m = MakerAdminMemberFactory(member_number=1001, end_timestamp=self.datetime(), rfid_tag="123") 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) users = self.session.query(User).all() self.assertEqual(1, len(users)) u = users[0] self.assertEqual("1001", u.name) self.assertEqual(self.datetime(), u.stop_timestamp) self.assertEqual(False, bool(u.blocked)) self.assertEqual("123", u.card) self.assertEqual(c.id, u.customer_id) auths = self.session.query(AuthorityInUser).filter_by( user_id=u.id).all() self.assertEqual(1, len(auths)) auth = auths[0] self.assertEqual(a.id, auth.authority_id)
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_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_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_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_update_diff_adds_member_and_auth(self): c = CustomerFactory() a = AuthorityFactory() m = MakerAdminMemberFactory(member_number=1001, rfid_tag='4444', end_timestamp=self.datetime()) AddMember(m).update(self.session, self.tui, customer_id=c.id, authority_id=a.id) users = self.session.query(User).all() print(users) self.assertEqual(1, len(users)) u = users[0] self.assertEqual("1001", u.name) self.assertEqual(self.datetime(), u.stop_timestamp) self.assertEqual(None, u.blocked) self.assertEqual("4444", u.card) self.assertEqual(c.id, u.customer_id) auths = self.session.query(AuthorityInUser).filter_by(user_id=u.id).all() self.assertEqual(1, len(auths)) auth = auths[0] self.assertEqual(a.id, auth.authority_id)
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_simple_diff_add_one_user(self): m = MakerAdminMemberFactory(member_number=1001) self.assertEqual([AddMember(m)], AddMember.find_diffs([], [m]))
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_excessive_ma_user_creates_no_diff(self): m = MakerAdminMemberFactory(member_number=1001) self.assertEqual([], BlockMember.find_diffs([], [m]))
def test_simple_no_diff(self): d = DbMember(UserFactory(name="1001")) m = MakerAdminMemberFactory(member_number=1001) self.assertEqual([], BlockMember.find_diffs([d], [m]))