def test_search_navigator_list_1_and_0(self): """Test search_navigator for lists of length 1 and 0""" # Add entry to database so lists of length 1 and 0 can be tested work_log_db.add_entry(self.log_entry) input_args = [ 'b', # enter search menu 'a', # exact date search '2019/3/20', # enter date to search by 'q', # invalid input '', # enter to continue 'r', # return to search menu 'a', # exact date search '2019/3/20', # enter date to search by 'e', # edit entry self.employee_name_new, # enter employee name '2019/2/12', # enter date self.task_name_new, # enter task name self.time_spent_new, # enter time spent self.optional_notes_new, # enter optional notes 'n', # not to save '', # enter to continue 'd', # delete entry 'y', # confirm deletion '', # enter to continue '', # enter to continue 'f', # quit search 'c', # quit program ] with patch('builtins.input', side_effect=input_args) as mock: work_log.main_menu_loop()
def test_edit_entry(self): """Test entry to see if it's been edited in database""" # Add entry 1 to database and then edit it work_log_db.add_entry(self.log_entry) work_log_db.edit_entry(self.log_entry, self.new_log_entry) # Count number of entries in the database result = Entry.select().count() # Assert there is 1 entry in the database self.assertEqual(result, 1) # Query the edited entry from the database result = Entry.select().where( Entry.employee_name == self.employee_name_new and Entry.date == self.date_new and Entry.task_name == self.task_name_new and Entry.time_spent == self.time_spent_new and Entry.optional_notes == self.optional_notes_new) # Shows what's in the database is equal to the edited entry self.assertEqual(result[0].employee_name, self.employee_name_new) self.assertEqual(result[0].date, self.date_new) self.assertEqual(result[0].task_name, self.task_name_new) self.assertEqual(result[0].time_spent, self.time_spent_new) self.assertEqual(result[0].optional_notes, self.optional_notes_new)
def test_delete_entry(self): """Test if entry has been deleted from the database""" # No entries in the database # Count number of entries in the database result = Entry.select().count() # Assert no entries self.assertEqual(result, 0) # Add entry to database work_log_db.add_entry(self.log_entry) # Count number of entries in the database result = Entry.select().count() # Assert there is 1 entry in the database self.assertEqual(result, 1) # Delete entry from database work_log_db.delete_entry(self.log_entry) # Count number of entries in the database result = Entry.select().count() # Assert no entries self.assertEqual(result, 0)
def test_employee_name_search(self): """Test employee name search""" # Nothing added to database yet search_result = work_log_db.employee_name_search('John Smith') self.assertEqual(search_result, []) # Add entry to database work_log_db.add_entry(self.log_entry) # 2019/3/20 # Search by name search_result = work_log_db.employee_name_search('John Smith') # Assert search result is not [] self.assertNotEqual(search_result, []) # Query entry from database result = Entry.select().where( Entry.employee_name == self.employee_name and Entry.date == self.date and Entry.task_name == self.task_name and Entry.time_spent == self.time_spent and Entry.optional_notes == self.optional_notes) # Entry added should equal search_result self.assertEqual(result[0].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[0].date, search_result[0]["date"]) self.assertEqual(result[0].task_name, search_result[0]["task_name"]) self.assertEqual(result[0].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[0]["optional_notes"]) # Add another entry to database with a different date but # with the same name # Entries are returned back in descending order by date work_log_db.add_entry(self.log_entry_dr_3) # 2019/4/3 # Search by name search_result = work_log_db.employee_name_search('John Smith') # Query log_entry_dr_3 result = Entry.select().where(Entry.date == self.date_dr_3) # Entry added should be first in search results self.assertEqual(result[0].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[0].date, search_result[0]["date"]) self.assertEqual(result[0].task_name, search_result[0]["task_name"]) self.assertEqual(result[0].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[0]["optional_notes"])
def test_date_range_search(self): """Test date range search""" # No entries in database # Start and end date for the date range search date_1 = datetime.date(2019, 3, 1) date_2 = datetime.date(2019, 4, 3) # Query the database search_result = work_log_db.date_range_search(date_1, date_2) # Assert nothing returned back from the search self.assertEqual(search_result, []) # Add 3 entries with different dates but within the range work_log_db.add_entry(self.log_entry_dr_1) work_log_db.add_entry(self.log_entry_dr_2) work_log_db.add_entry(self.log_entry_dr_3) # Add 1 entry with a date outside of the search range work_log_db.add_entry(self.new_log_entry) # Query by database date range result = Entry.select().where(Entry.date.between(date_1, date_2)) # Should return the 3 entries that are in the search range search_result = work_log_db.date_range_search(date_1, date_2) # Assert number of entries from search result equals number # added to database minus 1 self.assertEqual(len(search_result), Entry.select().count() - 1) # Assert first entry added (earliest date) equals last entry # from search result since results are returned in descending # order from the date_range_search self.assertEqual(result[0].employee_name, search_result[2]["employee_name"]) self.assertEqual(result[0].date, search_result[2]["date"]) self.assertEqual(result[0].task_name, search_result[2]["task_name"]) self.assertEqual(result[0].time_spent, search_result[2]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[2]["optional_notes"]) # Assert 2nd entry added equals middle list item self.assertEqual(result[1].employee_name, search_result[1]["employee_name"]) self.assertEqual(result[1].date, search_result[1]["date"]) self.assertEqual(result[1].task_name, search_result[1]["task_name"]) self.assertEqual(result[1].time_spent, search_result[1]["time_spent"]) self.assertEqual(result[1].optional_notes, search_result[1]["optional_notes"]) # Assert 3rd entry added equals first list item self.assertEqual(result[2].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[2].date, search_result[0]["date"]) self.assertEqual(result[2].task_name, search_result[0]["task_name"]) self.assertEqual(result[2].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[2].optional_notes, search_result[0]["optional_notes"])
def add_entry(): """Add new entry""" employee_name = get_employee_name() date = get_date() task_name = get_task_name() time_spent = get_time_spent() optional_notes = get_optional_notes() save = None while save != 'y': save = input('Save entry? [Yn] ').lower() if save == 'y': log_entry = { "employee_name": employee_name, "date": date, "task_name": task_name, "time_spent": time_spent, "optional_notes": optional_notes } entry = work_log_db.add_entry(log_entry) input('Entry added. Enter to return to the menu') return entry elif save == 'n': input('Entry not saved. Enter to continue') log_entry = {} return log_entry else: input("Enter a valid choice 'y' or 'n'. Enter to continue") clear_screen() continue
def test_search_navigator_last_item_in_list(self): """Test last item in list and test employee name search""" # Add 2 entries to the database work_log_db.add_entry(self.log_entry_1) work_log_db.add_entry(self.log_entry_2) input_args = [ 'b', # enter search menu 'a', # exact date search '2019/3/20', # enter date to search by 'n', # proceed to target item 'q', # invalid input '', # enter to continue 'p', # previous entry 'n', # return to target item 'r', # return to search menu 'a', # exact date search '2019/3/20', # enter date to search by 'n', # proceed to target item 'e', # edit entry self.employee_name_new, # enter employee name '2019/2/12', # enter date self.task_name_new, # enter task name self.time_spent_new, # enter time spent self.optional_notes_new, # enter optional notes 'n', # not to save '', # enter to continue 'd', # delete entry 'y', # confirm deletion '', # enter to continue 'r', # return to search menu 'f', # quit search 'c', # quit program ] with patch('builtins.input', side_effect=input_args) as mock: work_log.main_menu_loop()
def test_exact_search(self): """Test exact search""" # Nothing added to database yet. Search should return [] search_result = work_log_db.exact_search('add') self.assertEqual(search_result, []) # Nothing added to database yet. Search should return [] search_result = work_log_db.exact_search('app') self.assertEqual(search_result, []) # Add entry to database work_log_db.add_entry(self.log_entry) # Entry contains word "add" in the task name search_result = work_log_db.exact_search('add') self.assertNotEqual(search_result, []) # Optional notes contain "apples" search_result = work_log_db.exact_search('app') self.assertNotEqual(search_result, []) # Returns entry that was added to the database result = Entry.select().where( Entry.employee_name == self.employee_name and Entry.date == self.date and Entry.task_name == self.task_name and Entry.time_spent == self.time_spent and Entry.optional_notes == self.optional_notes) # Entry added should equal search_result self.assertEqual(result[0].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[0].date, search_result[0]["date"]) self.assertEqual(result[0].task_name, search_result[0]["task_name"]) self.assertEqual(result[0].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[0]["optional_notes"])
def test_add_entry(self): """Test add_entry from work_log_db.py""" # No entries in database # Query database result = Entry.select().where( Entry.employee_name == self.employee_name and Entry.date == self.date and Entry.task_name == self.task_name and Entry.time_spent == self.time_spent and Entry.optional_notes == self.optional_notes) # Check to see if in database if result.exists(): result = True else: result = False # Assert no entries in the database self.assertFalse(result) # Add entry to database work_log_db.add_entry(self.log_entry) # Query database result = Entry.select().where( Entry.employee_name == self.employee_name and Entry.date == self.date and Entry.task_name == self.task_name and Entry.time_spent == self.time_spent and Entry.optional_notes == self.optional_notes) # Assert entry in database self.assertEqual(result[0].employee_name, self.employee_name) self.assertEqual(result[0].date, self.date) self.assertEqual(result[0].task_name, self.task_name) self.assertEqual(result[0].time_spent, self.time_spent) self.assertEqual(result[0].optional_notes, self.optional_notes)
def test_time_spent_search(self): """Test time spent search""" # Nothing in database yet so search result should return [] search_result = work_log_db.time_spent_search(5) self.assertEqual(search_result, []) # Add 2 entries with the same time_spent and one with a # different time spent work_log_db.add_entry(self.log_entry_1) work_log_db.add_entry(self.log_entry_2) work_log_db.add_entry(self.new_log_entry) # Has different time spent # Query database search_result = work_log_db.time_spent_search(5) # Assert that length of search result equals count from database result = Entry.select().where( Entry.time_spent == self.time_spent_1).count() self.assertEqual(len(search_result), result) # Assert that length of search result equals 2 self.assertEqual(len(search_result), 2) # Returns entries where time_spent is 5 result = Entry.select().where(Entry.time_spent == self.time_spent_1) # First entry added should equal search result's first entry self.assertEqual(result[0].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[0].date, search_result[0]["date"]) self.assertEqual(result[0].task_name, search_result[0]["task_name"]) self.assertEqual(result[0].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[0]["optional_notes"]) # Second entry added should equal search result's second entry self.assertEqual(result[1].employee_name, search_result[1]["employee_name"]) self.assertEqual(result[1].date, search_result[1]["date"]) self.assertEqual(result[1].task_name, search_result[1]["task_name"]) self.assertEqual(result[1].time_spent, search_result[1]["time_spent"]) self.assertEqual(result[1].optional_notes, search_result[1]["optional_notes"])
def test_search_navigator_list_item_in_middle(self): """Test when the item in the list is one of the middle items in the list """ # Add 3 entries to the database work_log_db.add_entry(self.log_entry_1) work_log_db.add_entry(self.log_entry_2) work_log_db.add_entry(self.log_entry_3) input_args = [ 'b', # enter search menu 'd', # exact search 'entry', # all 3 entries contain the word entry 'n', # advance to target item 'q', # invalid input '', # enter to continue 'n', # next 'p', # return to target entry 'p', # previous 'n', # return to target entry 'r', # return to search menu 'd', # exact search 'entry', # all 3 entries contain the word entry 'n', # advance to target item 'e', # edit entry self.employee_name_new, # enter employee name '2019/2/12', # enter date self.task_name_new, # enter task name self.time_spent_new, # enter time spent self.optional_notes_new, # enter optional notes 'n', # not to save '', # enter to continue 'd', # delete entry 'y', # confirm deletion '', # enter to continue 'r', # return to search menu 'f', # quit search 'c', # quit program ] with patch('builtins.input', side_effect=input_args) as mock: work_log.main_menu_loop()
def test_exact_date_search(self): """Test exact date search""" # Add 2 log entries with the same date and one that differs by date work_log_db.add_entry(self.log_entry_1) work_log_db.add_entry(self.log_entry_2) work_log_db.add_entry(self.new_log_entry) # This log entry differs # Query the database for entries with that date result = Entry.select().where(Entry.date == self.date_1) # Assert there are 2 entries with the date queried self.assertEqual(result.count(), 2) # Use exact_date_search search_result = work_log_db.exact_date_search(self.date_1) # Assert count from database equals count from exact_date_search self.assertEqual(result.count(), len(search_result)) # Entry 1 in database equals entry 1 from exact_date_search self.assertEqual(result[0].employee_name, search_result[0]["employee_name"]) self.assertEqual(result[0].date, search_result[0]["date"]) self.assertEqual(result[0].task_name, search_result[0]["task_name"]) self.assertEqual(result[0].time_spent, search_result[0]["time_spent"]) self.assertEqual(result[0].optional_notes, search_result[0]["optional_notes"]) # Entry 2 in database equals entry 2 from exact_date_search self.assertEqual(result[1].employee_name, search_result[1]["employee_name"]) self.assertEqual(result[1].date, search_result[1]["date"]) self.assertEqual(result[1].task_name, search_result[1]["task_name"]) self.assertEqual(result[1].time_spent, search_result[1]["time_spent"]) self.assertEqual(result[1].optional_notes, search_result[1]["optional_notes"])