def test_create_account_constraint_fail(self): validator = Validator(CreateAccount()) event = {"account": {"activeCard": "true", "availableLimit": 100}} violations = validator.validate(self.account, event) self.assertEqual(violations, ['account-already-initialized'])
def test_empty_validator_pass(self): account = Account() validator = Validator() violations = validator.validate(account, {}) self.assertEqual(violations, [])
def test_high_doubled_transaction_constraint_fail(self): self.account.logs.append({ "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:52:00.000Z" }) self.account.logs.append({ "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:53:00.000Z" }) base_time = datetime.strptime('2019-09-09T15:54:00.000Z', "%Y-%m-%dT%H:%M:%S.%fZ") validator = Validator(DoubledTransaction(base_time)) event = { "transaction": { "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:54:00.000Z" } } violations = validator.validate(self.account, event) self.assertEqual(violations, ['doubled_transaction'])
def test_high_frequency_small_interval_constraint_fail(self): self.account.logs.append({ "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:52:00.000Z" }) self.account.logs.append({ "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:52:30.000Z" }) self.account.logs.append({ "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:53:30.000Z" }) base_time = datetime.strptime('2019-09-09T15:54:00.000Z', "%Y-%m-%dT%H:%M:%S.%fZ") validator = Validator(HighFrequencySmallInterval(base_time)) event = { "transaction": { "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:54:00.000Z" } } violations = validator.validate(self.account, event) self.assertEqual(violations, ['high_frequency_small_interval'])
class MessageHandler: """ While there are messages from the shared messagequeue route the messages to the correct functions Read messages from the message queue and pass them to the validator. """ def __init__(self): self.__validator = Validator() def process_messages(self): while True: msg = MessageQueue.get_message() if msg is None: logger.debug("No more messages, thread ends") return self.__validator.validate(msg)
def test_create_account_constraint_pass(self): account = Account() validator = Validator(CreateAccount()) event = {"account": {"activeCard": "true", "availableLimit": 100}} violations = validator.validate(account, event) self.assertEqual(violations, [])
def test_insufficient_limit_constraint_fail(self): validator = Validator(InsufficientLimit()) event = { "transaction": { "merchant": "Burger King", "amount": 101, "time": "2019-09-09T15:52:00.000Z" } } violations = validator.validate(self.account, event) self.assertEqual(violations, ['insufficient_limit'])
def test_active_card_constraint_pass(self): validator = Validator(ActiveCard()) event = { "transaction": { "merchant": "Burger King", "amount": 20, "time": "2019-09-09T15:52:00.000Z" } } violations = validator.validate(self.account, event) self.assertEqual(violations, [])