FAQ | This is a LIVE service | Changelog

Commit 3de840d0 authored by Monty Dawson's avatar Monty Dawson
Browse files

Add a test for logic of revoking duplicated cards

parent 42c07d19
from unittest import TestCase
from cardsync.tests.db_mock import MockConnection
from cardsync import revoke_duplicate_cards
class RevokeDuplicateCardsTestCase(TestCase):
def test_returns_nothing_with_no_records_to_revoke(self):
mock_connection = MockConnection()
result = revoke_duplicate_cards.revoke_duplicate_issued_cards(mock_connection)
self.assertEqual(result, [['cam_uid', 'card_id']])
self.assertEqual(
mock_connection.get_queries_executed(),
[(revoke_duplicate_cards.SELECT_CARDHOLDERS_WITH_MULTIPLE_ISSUED_CARDS, {})]
)
def test_revokes_duplicates_for_cardholder(self):
mock_connection = MockConnection([
# result for the initial query to get cardholders with duplicates
[
['cam_uid', 'count'],
['ab123c', 3],
],
# result for the second query to get the duplicates for a given cardholder
[
['issue_number', 'card_id'],
[3, 101],
[2, 100],
[2, 99],
],
# results from the query to remove the duplicates
[[True], [True]],
[],
[[True], [True]],
[]
])
result = revoke_duplicate_cards.revoke_duplicate_issued_cards(mock_connection)
# the output should show that the 100 and 99 cards have been revoked
self.assertEqual(result, [
['cam_uid', 'card_id'],
['ab123c', 99],
['ab123c', 100],
])
self.assertEqual(
mock_connection.get_queries_executed(),
[
(revoke_duplicate_cards.SELECT_CARDHOLDERS_WITH_MULTIPLE_ISSUED_CARDS, {}),
(revoke_duplicate_cards.SELECT_ISSUED_CARDS_FOR_CARDHOLDER, {'cam_uid': 'ab123c'}),
(revoke_duplicate_cards.REVOKE_CARD_AS_DUPLICATE, {'card_id': 99}),
('COMMIT', {}),
(revoke_duplicate_cards.REVOKE_CARD_AS_DUPLICATE, {'card_id': 100}),
('COMMIT', {}),
]
)
def test_can_revoke_multiple_duplicates(self):
mock_connection = MockConnection([
# result for the initial query to get cardholders with duplicates
[
['cam_uid', 'count'],
['x12c', 2],
['b12c', 2],
],
# result for the query to get the duplicates for the first cardholder
[
['issue_number', 'card_id'],
[4, 300],
[3, 299],
],
# results from the query to remove the first duplicate
[[True], [True]],
[],
# result for the query to get the duplicates for the second cardholder
[
['issue_number', 'card_id'],
[4, 400],
[3, 399],
],
# results from the query to remove the second duplicate
[[True], [True]],
[],
])
result = revoke_duplicate_cards.revoke_duplicate_issued_cards(mock_connection)
# the output should show that the 299 and 399 cards have been revoked
self.assertEqual(result, [
['cam_uid', 'card_id'],
['x12c', 299],
['b12c', 399],
])
self.assertEqual(
mock_connection.get_queries_executed(),
[
(revoke_duplicate_cards.SELECT_CARDHOLDERS_WITH_MULTIPLE_ISSUED_CARDS, {}),
(revoke_duplicate_cards.SELECT_ISSUED_CARDS_FOR_CARDHOLDER, {'cam_uid': 'x12c'}),
(revoke_duplicate_cards.REVOKE_CARD_AS_DUPLICATE, {'card_id': 299}),
('COMMIT', {}),
(revoke_duplicate_cards.SELECT_ISSUED_CARDS_FOR_CARDHOLDER, {'cam_uid': 'b12c'}),
(revoke_duplicate_cards.REVOKE_CARD_AS_DUPLICATE, {'card_id': 399}),
('COMMIT', {}),
]
)
def test_fails_when_revoking_one_card_touches_many(self):
mock_connection = MockConnection([
# result for the initial query to get cardholders with duplicates
[
['cam_uid', 'count'],
['z123x', 2],
],
# result for the query to get the duplicates for the first cardholder
[
['issue_number', 'card_id'],
[6, 500],
[6, 499],
],
# results from the query to remove the first duplicate - three list here indicate
# a row count of two - with the initial row being taken as the header row
# (indicating the names of the columns)
[[True], [True], [True]],
])
with self.assertRaisesRegex(
ValueError,
(
'Could not revoke card 499 for cardholder z123x, number of affected rows '
'is expected to be 1 but is 2'
)
):
revoke_duplicate_cards.revoke_duplicate_issued_cards(mock_connection)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment