def check_db_rows(output: str, value_to_return): correct_num_of_cards = 10 numbers = re.findall(r'400000\d{10,}', output, re.MULTILINE) for number in numbers: if len(number) != 16: return CheckResult.wrong(f'Wrong card number \'{number}\'. The card number should be 16-digit length.') if not is_passed_luhn_algorithm(number): return CheckResult.wrong('The card number \'{}\' doesn\'t pass luhn algorithm!'.format(number)) if len(numbers) != correct_num_of_cards: return CheckResult.wrong( f'After creating {correct_num_of_cards} cards, found {len(numbers)} cards with correct format\n' f'The card number should be 16-digit length and should start with 400000.') with sqlite3.connect(db_file_name) as db: rows = db.execute('SELECT * FROM card').fetchall() for number in numbers: if len(number) != 16: return CheckResult.wrong(f'Wrong card number \'{number}\'. The card number should be 16-digit length.') is_found = False for row in rows: if number in row: is_found = True if not is_found: return CheckResult.wrong('Your database doesn’t save newly created cards.\n' 'Make sure you commit your DB changes right after saving a new card in the database!') return CheckResult.correct()
def test_balance_after_transfer(output: str, value_to_return): global card_number, second_card_number, are_all_inputs_read with sqlite3.connect(db_file_name) as db: first = db.execute('SELECT * FROM card WHERE number = {}'.format( card_number)).fetchone() if not first: return CheckResult.wrong( f'Can\' find card number \'{first}\' in the database!\n' f'Make sure you commit your DB changes right after saving a new card in the database!' ) second = db.execute('SELECT * FROM card WHERE number = {}'.format( second_card_number)).fetchone() if not second: return CheckResult.wrong( f'Can\' find card number \'{second}\' in the database!\n' f'Make sure you commit your DB changes right after saving a new card in the database!' ) first_balance = first[3] second_balance = second[3] if first_balance != 10000: return CheckResult.wrong( 'Incorrect account balance of the card used to make the transfer.' ) if second_balance != 10000: return CheckResult.wrong( 'Incorrect account balance of the card to which the transfer was made.' ) are_all_inputs_read = True return '0'
def test_rows_after_closing_account(output: str, value_to_return): global card_number, are_all_inputs_read with sqlite3.connect(db_file_name) as db: try: rows = db.execute('SELECT * FROM card WHERE number = \'{}\''.format(card_number)).fetchall() except: return CheckResult.wrong("Can't select data from 'card' table!") if rows: return CheckResult.wrong('After closing an account, the card number should be deleted from the database.') are_all_inputs_read = True return value_to_return
def test_second_add_income(output: str, value_to_return): global card_number expected_balance = 10000 with sqlite3.connect(db_file_name) as db: result = db.execute('SELECT * FROM card WHERE number = {}'.format(card_number)).fetchone() if not result: return CheckResult.wrong(f'Can\' find card number \'{card_number}\' in the database!\n' f'Make sure you commit your DB changes right after saving a new card in the database!') balance = result[3] if balance != expected_balance: return CheckResult.wrong( 'Account balance is wrong after adding income. Expected {}'.format(expected_balance)) return '2\n15000'
def test_output_after_correct_sign_in(output: str, value_to_return): global are_all_inputs_read are_all_inputs_read = True if 'successfully' not in output.lower(): return CheckResult.wrong( 'There is no \'successfully\' in your output after signing in with correct credentials') return value_to_return
def test_transfer_doesnt_pass_luhn(output: str, value_to_return): if 'mistake'.lower() not in output.lower(): return CheckResult.wrong('You should not allow to transfer to a card number that doesn\'t pass ' 'the Luhn algorithm.\n Instead output \'{}\''.format( 'Probably you made mistake in card number. Please try again!')) doesnt_exist_card = 4000003972196501 return '3\n{}'.format(doesnt_exist_card)
def test_transfer_not_enough_money(output: str, value_to_return): global second_card_number if 'not enough money' not in output.lower(): return CheckResult.wrong( 'You should not allow a transfer if there is not enough money ' 'in the account to complete it.\n') return '2\n20000\n3\n{}\n10000'.format(second_card_number)
def test_transfer_doesnt_exist_card(output: str, value_to_return): global second_card_number if 'not exist' not in output.lower(): return CheckResult.wrong( 'You should not allow to transfer to a card number that does not exist.' '\nYpu should print \'{}\''.format('Such a card does not exist.')) return '3\n{}\n10000'.format(second_card_number)
def test_output_after_wrong_pin(output: str, value_to_return): global are_all_inputs_read are_all_inputs_read = True if 'wrong' not in output.lower(): return CheckResult.wrong( 'There is no \'wrong\' in your output after signing in with incorrect credentials') return value_to_return
def test_luhn_algorithm(output: str, correct_num_of_cards): global are_all_inputs_read numbers = re.findall(r'400000\d{10,}', output, re.MULTILINE) for number in numbers: if len(number) != 16: return CheckResult.wrong(f'Wrong card number \'{number}\'. The card number should be 16-digit length.') if not is_passed_luhn_algorithm(number): return CheckResult.wrong('The card number \'{}\' doesn\'t pass luhn algorithm!'.format(number)) if len(numbers) != correct_num_of_cards: return CheckResult.wrong( f'After creating {correct_num_of_cards} cards, found {len(numbers)} cards with correct format\n' f'The card number should be 16-digit length and should start with 400000.') are_all_inputs_read = True return '0'
def test_difference_between_generations(output: str, value_to_return): global card_number, pin, are_all_inputs_read credentials = get_credentials(output) another_card_number = credentials[0] if another_card_number == card_number: return CheckResult.wrong('Your program generates two identical card numbers!') are_all_inputs_read = True return value_to_return
def test_balance_after_transfer(output: str, value_to_return): global card_number, second_card_number, are_all_inputs_read with sqlite3.connect(db_file_name) as db: first = db.execute('SELECT * FROM card WHERE number = {}'.format( card_number)).fetchone() second = db.execute('SELECT * FROM card WHERE number = {}'.format( second_card_number)).fetchone() first_balance = first[3] second_balance = second[3] if first_balance != 10000: return CheckResult.wrong( 'Incorrect account balance of the card used to make the transfer.' ) if second_balance != 10000: return CheckResult.wrong( 'Incorrect account balance of the card to which the transfer was made.' ) are_all_inputs_read = True return '0'
def test_second_add_income(output: str, value_to_return): global card_number expected_balance = 10000 with sqlite3.connect(db_file_name) as db: result = db.execute('SELECT * FROM card WHERE number = {}'.format( card_number)).fetchone() balance = result[3] if balance != expected_balance: return CheckResult.wrong( 'Account balance is wrong after adding income. Expected {}'. format(expected_balance)) return '2\n15000'
def test_luhn_algorithm(output: str, value_to_return): global are_all_inputs_read numbers = re.findall(r'400000\d{10}', output, re.MULTILINE) for number in numbers: if not is_passed_luhn_algorithm(number): return CheckResult.wrong( 'The card number \'{}\' doesn\'t pass luhn algorithm!'.format( number)) are_all_inputs_read = True return '0'
def check_db_rows(output: str, value_to_return): numbers = re.findall(r'400000\d{10}', output, re.MULTILINE) with sqlite3.connect(db_file_name) as db: rows = db.execute('SELECT * FROM card').fetchall() for number in numbers: is_found = False for row in rows: if number in row: is_found = True if not is_found: return CheckResult.wrong( 'Your database doesn’t save newly created cards.') return CheckResult.correct()
def check_db(output: str, value_to_return): if not os.path.exists(db_file_name): return CheckResult.wrong( 'Can\'t find db file named \'{}\''.format(db_file_name)) try: copy2(db_file_name, temp_db_file_name) except Exception: return CheckResult.wrong('Can\'t copy database file!') try: with sqlite3.connect(db_file_name) as db: response = db.execute( 'SELECT name FROM sqlite_master WHERE type = \'table\' AND name NOT LIKE \'sqlite_%\';' ) for _ in response.fetchall(): if 'card' in _: break else: return CheckResult.wrong( 'Your database doesn\'t have a table named \'card\'') except Exception as exp: return CheckResult.wrong('Can\'t connect to the database!') correct_columns = (('ID', 'INTEGER'), ('NUMBER', 'TEXT'), ('PIN', 'TEXT'), ('BALANCE', 'INTEGER')) try: with sqlite3.connect(db_file_name) as db: response = db.execute('PRAGMA table_info(card);') real_columns = response.fetchall() for correct_column in correct_columns: for real_column in real_columns: real_column = [ str(element).upper() for element in real_column ] if correct_column[0] in real_column and correct_column[ 1] in real_column: break else: return CheckResult.wrong( f'Can\'t find column named \'{correct_column[0].lower()}\' with \'{correct_column[1]}\' type.\n' 'Your table should have columns described in the stage instructions.' ) except Exception as ignored: return CheckResult.wrong('Can\'t connect to the database!') return CheckResult.correct()
def check(self, reply: str, attach) -> CheckResult: if are_all_inputs_read: return CheckResult.correct() else: return CheckResult.wrong('You didn\'t read all inputs!')