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 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!')