def test_delete_pass(self): """ test_delete_pass """ from todolist.controllers.todos import delete todo = DBSession.query(Todo).first() request = testing.DummyRequest(params={}, matchdict={'id': todo.id}, post={}) response = delete(request) todo_count = DBSession.query(Todo).count() self.assertIsNot(todo, DBSession.query(Todo).first()) self.assertEqual(response['messages'], '%s has been deleted' % todo.task) self.assertEqual(todo_count, 3)
def test_update_pass(self): """ test_update_pass """ from todolist.controllers.todos import update params = {'task': 'Updated task', 'priority': 1} # provide the todo with id todo = DBSession.query(Todo).first() request = testing.DummyRequest(params=params, matchdict={'id': todo.id}, post=params) response = update(request) updated_todo = DBSession.query(Todo).filter_by(id=todo.id).one() self.assertEqual(response['task'], params['task']) self.assertEqual(updated_todo.task, params['task'])
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine #engine = create_engine('sqlite:///todolist.sqlite') engine = create_engine( 'postgresql://*****:*****@localhost:5432/todolist_test') DBSession.configure(bind=engine) Base.metadata.create_all(engine) # the transaction commited after added so rolling back doesnt work on Postgres #with transaction.manager: instances = (Todo(task='Second task', priority=0), Todo(task='Thrid task', priority=5), Todo(task='First task', priority=10), Todo(task="Done task", priority=5, done_at=datetime.now())) DBSession.add_all(instances)
def delete(request): todo_id = request.matchdict['id'] try: todo = DBSession.query(Todo).filter_by(id=todo_id).one() except NoResultFound: return {'errors': "No todo id: %s" % todo_id} if not todo.delete(): return {'errors': "%s can't be deleted" % todo.task} #todos, done_todos = get_todo_set() return {'id': todo.id, 'messages': '%s has been deleted' % todo.task}#, \ # 'todos':todos, 'done_todos':done_todos}
def update(request): todo_id = request.matchdict['id'] try: todo = DBSession.query(Todo).filter_by(id=todo_id).one() except NoResultFound: return {'errors': "No todo id: %s" % todo_id} todo.task=request.POST.get('task', None) todo.priority=request.POST.get('priority', None) # big bug here! if not todo.update(): return {'errors': todo.errors} # todos, done_todos = get_todo_set() return {'task': todo.task, 'priority':todo.priority, 'messages': '%s has been updated' % todo.task} #, \
def test_update_fail(self): """ test_update_fail """ from todolist.controllers.todos import update # test query not found request = testing.DummyRequest(params={}, matchdict={'id': 1}, post={}) response = update(request) self.assertEqual(response['errors'], "No todo id: 1") # test validation params = {'task': "", 'priority': "low"} todo = DBSession.query(Todo).first() request = testing.DummyRequest(params=params, matchdict={'id': todo.id}, post=params) response = update(request) self.assertTrue(response['errors']) self.assertEqual(len(response['errors']['priority']), 1) self.assertEqual(response['errors']['task'], ['Please enter a value']) self.assertEqual(response['errors']['priority'], ['Please enter an integer value'])
def tearDown(self): DBSession.remove() testing.tearDown()
def get_todo_set(): todos = DBSession.query(Todo).filter(Todo.done_at==None).order_by(Todo.priority.desc()).all() done_todos = DBSession.query(Todo).filter(Todo.done_at!=None).order_by(Todo.priority.desc()).all() return todos, done_todos