def add_todo_item(self, **kwargs): # create a new item, maybe set from kwargs newitem = TodoItem(**kwargs) # attach that item to this user newitem.users_pk = self.pk # save the new item newitem.save()
def test_save_insert(self): vals = { 'title': 'Read A Book', 'description': 'Read It', 'complete': 1, 'users_pk': 999 } read_book = TodoItem(**vals) read_book.save() with sqlite3.connect(DBPATH) as conn: cur = conn.cursor() cur.execute(""" SELECT COUNT(*) FROM todoitems WHERE title='Read A Book' AND description='Read It' AND 'complete'=1 AND 'users_pk'=999; """ ) nresults = len(cur.fetchall()) self.assertEqual(nresults, 1, 'save creates a db entry with filled values') with sqlite3.connect(DBPATH) as conn: cur = conn.cursor() cur.execute(""" SELECT pk FROM todoitems WHERE title='Read A Book';""") new_pk = cur.fetchone()[0] self.assertGreaterEqual(new_pk, 1, 'save fills new pk value')
def testSelectAll(self): todo = TodoItem(title="t", description="d", complete=0) todo.save() todo1 = TodoItem(title="title", description="desc", complete=0) todo1.save() saved_todo = TodoItem.select_all() self.assertEqual(3, len(saved_todo))
def test_one_from_pk(self): item1 = TodoItem.one_from_pk(1) self.assertEqual(item1.pk, 1, "pk is set") self.assertEqual(item1.title, "Item 1", "title is set") self.assertEqual(item1.description, "Do A Thing", "description is set") self.assertEqual(item1.complete, 0, "complete is set") self.assertEqual(item1.users_pk, 1, "users_pk is set")
def test_save_update(self): return # remove this to run the test vals = { 'title': 'Clean up room', 'description': 'Clean it up', 'complete': 0, 'users_pk': 1000 } clean_room = TodoItem(**vals) clean_room.description = 'At least put away your clothes' clean_room.complete = 1 clean_room.save() with sqlite3.connect(DBPATH) as conn: cur = conn.cursor() cur.execute(""" SELECT pk FROM todoitems WHERE title='Clean up room';""") nitems = len(cur.fetchall()) self.assertEqual(nitems, 1, 'save only inserts once') with sqlite3.connect(DBPATH) as conn: cur = conn.cursor() cur.execute(""" SELECT description, complete FROM todoitems WHERE title='Clean up room';""") desc_complete = cur.fetchone() self.assertEqual(desc_complete, ('At least put away your clothes', 1), 'save updates values in existing item')
def testSave(self): todo = TodoItem(title="t", description="d", complete=0) todo.save() saved_todo = TodoItem.select_one(2) self.assertEqual("t", saved_todo.title) change = TodoItem.select_one(1) change.title = "Nottitle" change.save() saved_todo = TodoItem.select_one(1) self.assertEqual("Nottitle", saved_todo.title)
def seed(dbpath=DBPATH): TodoItem.dbpath = dbpath td1 = TodoItem(title="Item 1", description="Do A Thing", complete=0, users_pk=1) td2 = TodoItem(title="Item 2", description="Another Thing", complete=1, users_pk=1) td1.save() td2.save()
def testRepr(self): todo = TodoItem.select_one(1) statement = todo.__repr__ self.assertIn("<TodoItem: pk=1>", str(statement))
def testDelete(self): todo = TodoItem.select_one(1) todo.delete() saved_todo = TodoItem.select_all() self.assertEqual(0, len(saved_todo))
def testUpdate(self): change = TodoItem.select_one(1) change.title = "NotNottitle" change._update() saved_todo = TodoItem.select_one(1) self.assertEqual("NotNottitle", saved_todo.title)
def testInsert(self): todo = TodoItem(title="title", description="desc", complete=0) todo._insert() saved_todo = TodoItem.select_one(2) self.assertEqual("title", saved_todo.title)
def testSelectOne(self): todo = TodoItem.select_one(1) self.assertIsInstance(todo, TodoItem, "select_one returns a TodoItem") self.assertEqual("test.db", todo.title)
def testCreation(self): todo = TodoItem(pk=1, title="title", description="desc", complete=0) self.assertIsInstance(todo, TodoItem, "__init__ returns a TodoItem") self.assertEqual("title", todo.title)
def main_loop(): while True: choice = views.main_menu() if choice is None: views.bad_input() elif choice == "6": views.goodbye() break elif choice == "1": #incomplete items items = TodoItem.select_all(complete=0) for item in items: views.show_item(item) change_choice = views.change_status() if change_choice: item_choice = views.select_item() changed_item = TodoItem.select_one(item_choice) change = TodoItem(pk=changed_item[0], title=changed_item[1], description=changed_item[2], complete=changed_item[3]) if changed_item: change.complete = 1 change.save() else: views.bad_input() views.enter_to_continue() elif choice == "2": items = TodoItem.select_all(complete=1) for item in items: views.show_item(item) change_choice = views.change_status() if change_choice: item_choice = views.select_item() changed_item = TodoItem.select_one(item_choice) change = TodoItem(pk=changed_item[0], title=changed_item[1], description=changed_item[2], complete=changed_item[3]) if changed_item: change.complete = 0 change.save() else: views.bad_input() views.enter_to_continue() elif choice == "3": # show all items items = TodoItem.select_all() for item in items: views.show_item(item) views.enter_to_continue elif choice == "4": title, descrip, complete = views.add() item = TodoItem(title=title, description=descrip, complete=complete) TodoItem.save(item) elif choice == "5": #show all items, choose one, then delete it items = TodoItem.select_all() for item in items: views.show_item(item) delete = views.delete_one() d = TodoItem.select_one(delete) item = TodoItem(pk=d[0][0]) item.delete()
def get_todo_items(self): return TodoItem.all_for_users_pk(self.pk)