def test_duplicate(self): with patch('datetime.datetime') as mock_date: mock_date.now.return_value = self.date mock_date.side_effect = lambda *args, **kw: self.datetime_obj( *args, **kw) # 1st transaction at edge of window transaction_time = datetime.datetime(2019, 2, 13, 9, 58, 0) transaction_time_str = transaction_time.strftime( self.config["date_time_format"]) test_transaction = dict() test_transaction["merchant"] = "Burger King" test_transaction["amount"] = 20 test_transaction["time"] = transaction_time_str transaction = Transaction(test_transaction, self.config["date_time_format"]) self.assertEqual( self.recent_transactions.is_valid_transaction(transaction), (True, None)) self.recent_transactions.add_recent_transaction(transaction) # 2nd transaction transaction_time = datetime.datetime(2019, 2, 13, 9, 59, 30) transaction_time_str = transaction_time.strftime( self.config["date_time_format"]) test_transaction["amount"] = 21 test_transaction["time"] = transaction_time_str transaction = Transaction(test_transaction, self.config["date_time_format"]) self.assertEqual( self.recent_transactions.is_valid_transaction(transaction), (True, None)) self.recent_transactions.add_recent_transaction(transaction) # 3rd transaction - repeat transaction transaction_time = datetime.datetime(2019, 2, 13, 9, 59, 50) transaction_time_str = transaction_time.strftime( self.config["date_time_format"]) test_transaction["amount"] = 20 test_transaction["time"] = transaction_time_str transaction = Transaction(test_transaction, self.config["date_time_format"]) is_valid = self.recent_transactions.is_valid_transaction( transaction) self.assertEqual((is_valid[0], type(is_valid[1])), (False, type(DuplicateTransactionViolation())))
class TestTransactions(unittest.TestCase): def setUp(self): self.config = yaml.load(open('config.yml')) str = """{"transaction": {"merchant": "Burger King", "amount": 20, "time":"2019-02-13T10:00:00.001Z"}}""" self.input = json.loads(str).get('transaction') self.transaction = Transaction(self.input, self.config["date_time_format"]) def test_transaction_data(self): self.assertEqual(self.transaction.get_amount(), 20) self.assertEqual(self.transaction.get_merchant(), "Burger King") self.assertEqual(self.transaction.get_time(), datetime.datetime(2019, 2, 13, 10, 0, 0, 1000)) self.assertEqual(self.transaction.get_distinct_key(), "Burger King" + "#####" + str(20)) def tearDown(self): pass
def validate_transaction(self, transaction: Transaction): is_valid = True #print(transaction) if not self.get_card_status(): self.add_violation(CardNotActive()) is_valid = False #print(transaction.get_amount()) if transaction.get_amount() > self.get_limit(): self.add_violation(InsufficientLimit()) is_valid = False is_valid_by_recent, violation = self.__recent_transactions.is_valid_transaction( transaction) if not is_valid_by_recent: self.add_violation(violation) is_valid = False if is_valid: self.__recent_transactions.add_recent_transaction(transaction) self.deduct_amount(transaction.get_amount()) return is_valid
def test_one_transaction(self): transaction_time = datetime.datetime.now() transaction_time_str = transaction_time.strftime(self.config["date_time_format"]) test_transaction = dict() test_transaction["merchant"] = "Burger King" test_transaction["amount"] = 20 test_transaction["time"] = transaction_time_str #print(json.dumps(test_transaction)) transaction = Transaction(test_transaction, self.config["date_time_format"]) self.assertEqual(self.account.validate_transaction(transaction), True) self.assertEqual(self.account.get_limit(), 60)
def test_one_transaction(self): transaction_time = datetime.datetime.now() transaction_time_str = transaction_time.strftime(self.config["date_time_format"]) test_transaction = dict() test_transaction["merchant"] = "Burger King" test_transaction["amount"] = 20 test_transaction["time"] = transaction_time_str #print(json.dumps(test_transaction)) transaction = Transaction(test_transaction, self.config["date_time_format"]) self.assertEqual(self.account.validate_transaction(transaction), False) self.assertEqual(type(self.account.get_violations()[-1]), type(CardNotActive()))
def test_account_exceed_balance(self): transaction_time = datetime.datetime.now() transaction_time_str = transaction_time.strftime( self.config["date_time_format"]) test_transaction = dict() test_transaction["merchant"] = "Burger King" test_transaction["amount"] = 100 test_transaction["time"] = transaction_time_str #print(json.dumps(test_transaction)) transaction = Transaction(test_transaction, self.config["date_time_format"]) self.assertEqual(self.account.validate_transaction(transaction), False) self.assertEqual(type(self.account.get_violations()[-1]), type(InsufficientLimit())) print(str(self.account))
def process_event(self, event): stream_logger.info("Event received - " + event) event_dict = json.loads(event) if "account" in event_dict: if not self.account: self.account = Account(event_dict.get('account')) else: self.account.add_violation(AccountAlreadyIntialized()) elif "transaction" in event_dict: if not self.account: sys.stdout.write("account-not-initialized") else: self.account.validate_transaction(Transaction(event_dict.get('transaction'))) if self.account: sys.stdout.write(str(self.account)) sys.stdout.write('\n')
def setUp(self): self.config = yaml.load(open('config.yml')) str = """{"transaction": {"merchant": "Burger King", "amount": 20, "time":"2019-02-13T10:00:00.001Z"}}""" self.input = json.loads(str).get('transaction') self.transaction = Transaction(self.input, self.config["date_time_format"])