def test_find_by_search_term(self, mock_get_search_term, _): iter_num = 5 entries = [create_random_entry() for _ in range(iter_num)] for (index, entry) in enumerate(entries, start=1): for _ in range(index): Entry.create(name=entry['name'], task=entry['task'], spent_minutes=entry['spent_minutes'], notes=entry['notes']) for (index, entry) in enumerate(entries, start=1): search_term = random.choice([entry['task'], entry['notes']]).lower() length = len(search_term) start = random.randint(0, length - 1) end = random.randint(start + 1, length) substring = search_term[start:end] mock_get_search_term.return_value = substring matched_entries = worklog_db.find_by_search_term() self.assertGreaterEqual(len(matched_entries), index) for matched_entry in matched_entries: self.assertTrue( (substring in matched_entry.task.lower()) or (substring in matched_entry.notes.lower()), "{} not in {} or {}".format(substring, matched_entry.task, matched_entry.notes))
def create_entries(): Entry.create( employee_name=DATA["employee_name"], minutes=DATA["minutes"], task_name=DATA["task_name"], notes=DATA["notes"], date=DATA["date"] )
def test_print_entries(self): # Test if entries print to the screen and if the menu works [n, b]. # Eventually the script will return to the main menu [r] and quit [3]. Entry.create(**TEST_ENTRY) results = Entry.select() self.test_values = (value for value in ["n", "b", "r", "3"]) with patch("builtins.input", self.mock_input): app.print_entries(results)
def test_check_employee_name_match(self): with test_database(TEST_DB, (Entry,)): self.create_entries() Entry.create(**DATA) entries = Entry.select() with mock.patch('builtins.input', side_effect=["Mike"]): test = worklog_db.check_employee_name_match(entries) self.assertEqual(test.count(), 2)
def new(): form = EntryForm() if form.validate_on_submit(): Entry.create(title=form.title.data, date=form.date.data, timespent=form.timespent.data, body=form.body.data, resources=form.resources.data) return redirect(url_for('index')) return render_template('new.html', form=form)
def add_entry(): """Add an entry""" clear_screen() print("Add a new entry") name = get_name() task = get_task() spent_minutes = get_spent_minutes() notes = get_notes() Entry.create(name=name, task=task, spent_minutes=spent_minutes, notes=notes)
def test_creation(self): Entry.create(name='Josh', task='Fix bug', spent_minutes=20, notes='fun') entries = Entry.select() self.assertEqual(len(entries), 1) entry = entries.get() self.assertEqual(entry.name, 'Josh') self.assertEqual(entry.task, 'Fix bug') self.assertEqual(entry.spent_minutes, 20) self.assertEqual(entry.notes, 'fun')
def test_display_nav_options(self): p = "[P] - Previous entry" n = "[N] - Next entry" q = "[Q] - Return to Main Menu" with test_database(TEST_DB, (Entry,)): self.create_entries() entries = Entry.select() Entry.create(**DATA) index = 0 menu = [n, q] worklog_db.display_nav_options(index, entries) self.assertNotIn(p, menu)
def test_display_nav_menu(self): a = "A) Previous Entry" b = "B) Next Entry" e = "E) Return to Main Menu" with test_database(test_db, (Entry, )): self.entry_creator() entries = Entry.select() Entry.create(**TEST2) index = 0 menu = [b, e] work_log.display_nav_menu(index, entries) self.assertNotIn(a, menu)
def test_find_by_employee(self): # Test if an entry can be found through a single name. entry_1 = Entry.create(**TEST_ENTRY) self.test_values = (value for value in [entry_1.employee]) with patch("builtins.input", self.mock_input): results = app.find_by_employee() self.assertEqual(results[0], entry_1) # If the user types in [m] and there are two employees with that # letter in their name, test if the user can select an option [2]. entry_2 = Entry.create(**TEST_ENTRY_2) self.test_values = (value for value in ["m", "2"]) with patch("builtins.input", self.mock_input): results = app.find_by_employee() self.assertEqual(results[0], entry_2)
def update(self, text): entry = Entry.create(owner_id=self.id, content=text) entry.save() self.timeline.insert(0, (entry.id, entry.created_time)) self.save()
def test_edit_entry(self): # Test if every key can be edited (except for id). entry = Entry.create(**TEST_ENTRY) entry = { "id": entry.id, "employee": entry.employee, "task": entry.task, "time": entry.time, "notes": entry.notes, "date": entry.date, } new_values = [ "employee", "Menno", "task", "Designing", "time", 50, "notes", "VVD", "date", "01/01/2001" ] results = [] self.test_values = (value for value in new_values) for _ in range(len(entry) - 1): with patch("builtins.input", self.mock_input): results.append(app.edit_entry(entry)) self.assertEqual(results[0]["employee"], new_values[1]) self.assertEqual(results[1]["task"], new_values[3]) self.assertEqual(results[2]["time"], new_values[5]) self.assertEqual(results[3]["notes"], new_values[7]) self.assertEqual(results[4]["date"], new_values[9])
def test_find_by_term(self): # Test if an entry can be found through a title or note. entry = Entry.create(**TEST_ENTRY) self.test_values = (value for value in [entry.task]) with patch("builtins.input", self.mock_input): results = app.find_by_term() self.assertEqual(results[0], entry)
def test_find_by_date_range(self): # Test if an entry can be found through a date range. entry = Entry.create(**TEST_ENTRY) self.test_values = (value for value in ["01/01/2019", "12/31/2019"]) with patch("builtins.input", self.mock_input): results = app.find_by_date_range() self.assertEqual(results[0], entry)
def test_find_by_employee(self, mock_get_employee_name, _): iter_num = 5 entries = [create_random_entry() for _ in range(iter_num)] for (index, entry) in enumerate(entries, start=1): for _ in range(index): Entry.create(name=entry['name'], task=entry['task'], spent_minutes=entry['spent_minutes'], notes=entry['notes']) for (index, entry) in enumerate(entries, start=1): mock_get_employee_name.return_value = entry['name'] matched_entries = worklog_db.find_by_employee() self.assertGreaterEqual(len(matched_entries), index) for matched_entry in matched_entries: self.assertEqual(matched_entry.name, entry['name'])
def test_find_by_date(self, mock_get_date_string, _): iter_num = 5 random_dates = [create_random_date() for _ in range(iter_num)] for (index, date) in enumerate(random_dates, start=1): for _ in range(index): entry = create_random_entry() Entry.create(name=entry['name'], task=entry['task'], spent_minutes=entry['spent_minutes'], notes=entry['notes'], timestamp=date) for (index, date) in enumerate(random_dates, start=1): mock_get_date_string.return_value = date.strftime('%m/%d/%Y') matched_entries = worklog_db.find_by_date() self.assertGreaterEqual(len(matched_entries), index) for matched_entry in matched_entries: self.assertEqual(matched_entry.timestamp.day, date.day) self.assertEqual(matched_entry.timestamp.month, date.month) self.assertEqual(matched_entry.timestamp.year, date.year)
def test_find_by_date_range(self, mock_get_date_string, _, ignore): iter_num = 20 random_dates = [create_random_date() for _ in range(iter_num)] for date in random_dates: entry = create_random_entry() Entry.create(name=entry['name'], task=entry['task'], spent_minutes=entry['spent_minutes'], notes=entry['notes'], timestamp=date) for _ in range(iter_num): start_date = create_random_date() end_date = create_random_date() mock_get_date_string.side_effect = [ start_date.strftime('%m/%d/%Y'), end_date.strftime('%m/%d/%Y') ] matched_entries = worklog_db.find_by_date_range() for matched_entry in matched_entries: self.assertGreaterEqual(matched_entry.timestamp, start_date) self.assertLessEqual(matched_entry.timestamp, end_date)
def add_new_entry(self): """Append a new Entry to 'self.entries'""" clear_screen() new_entry = Entry.create() if new_entry is None: print("Add new entry cancelled. Returning to main menu...") time.sleep(1) return None self.entries.append(new_entry) with open(self.file_name, "a") as file: writer = csv.writer(file) writer.writerow([ new_entry.date, new_entry.name, new_entry.minutes, new_entry.note ])
def create_entry(entry): """Create entry in database.""" Entry.create(**entry) return entry
def add_random_entry(): entry = create_random_entry() return Entry.create(name=entry['name'], task=entry['task'], spent_minutes=entry['spent_minutes'], notes=entry['notes'])
def add_entry(): Entry.create(name=test_name, task=test_task, spent_minutes=test_spent_minutes, notes=test_notes)
def create_entry(entry): Entry.create(**entry) return entry
def add_entry(): """Add new entry""" entry = get_entry_values() return Entry.create(**entry)