def run(args, db_name='main.sqlite', table_name='main'): """ Takes arguments (usually from command-line), parses them, and retrieves the words, filters, and formats them. Returns a formatted string ready to print-to-screen. """ rtn = '' # Parse the arguments options = dealArgs(args).to_object() # Get and filter the words words = getAndFilter(options) word_pairs = getPairs(words) conn = connect_db(db_name, table_name) # If -c argument passed, clear out the table if options['clear']: removed_rows = empty_table(conn, table_name) rtn += f'\nRemoved {removed_rows} rows.' # Add the word-pairs & get the count modified_rows = add_words(conn, word_pairs, table_name) # Close the database close_db(conn) # If nothing saved, give a warning if modified_rows <= 0: rtn += f'\nFailed to save anything' # If something saved, state the count else: rtn += f'\nSuccessfully added {modified_rows} words' return rtn
def test_empty_table(self): word_pairs = [ ('fish', 'hook', 1), ('bear', 'trap', 2), ('person', 'creditcard', 3), ] conn = connect_db(db_name, table_name) add_words(conn, word_pairs, table_name) # testing this empty_table(conn, table_name) cur = conn.cursor() cur.execute(f"SELECT word, word2, relation FROM {table_name}") result = cur.fetchall() close_db(conn) self.assertEqual(result, [])
def run(args, db_name='main.sqlite', table_name='main'): rtn = '' options = dealArgs(args).to_object() words = getAndFilter(options) word_pairs = getPairs(words, True) conn = connect_db(db_name, table_name) if options['clear']: removed_rows = empty_table(conn, table_name) rtn += f'\nRemoved {removed_rows} rows.' modified_rows = add_words(conn, word_pairs, table_name) close_db(conn) if modified_rows <= 0: rtn += f'\nFailed to save anything' else: rtn += f'\nSuccessfully added {modified_rows} words' return rtn
def tearDown(self): conn = connect_db(db_name, table_name) empty_table(conn, table_name) close_db(conn) remove(db_name)
def tearDown(self): empty_table(self.conn, table_name) close_db(self.conn) remove(db_name)