-1

I have a delete Route which I would like to Test it But Got this error which I think its coz of Route problem I will be really appreciate if you could help me So when I delete a user it will direct to page"//127.0.0.1:5000/user/1?sortby=normal&id=1" and says "delete done!." I do really need Help I aslo comment the @login_required in delete route But still doesn't work

ps: layout is blueprints

here is the error:

Traceback (most recent call last):
  File "/home/peg/flask-Alembic/test/test_app.py", line 47, in test_user_delete
    response = self.client.get(url_for('/user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))
  File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 312, in url_for
    return appctx.app.handle_url_build_error(error, endpoint, values)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in handle_url_build_error
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 305, in url_for
    force_external=external)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1616, in build
    raise BuildError(endpoint, values, method)
BuildError: ('layout.user/1?sort=normal&monkey=&id=1', {'follow_redirects': True}, None)

test_app.py:

 def test_user_delete(self):
        e1 = Users(name='admine2', email='admine2@gmail.com', age=25)
        db.session.add(e1)
        db.session.commit()
        u1= Users.query.get(e1.id)
        print('{0}').format(u1.id)
        response = self.client.get(url_for('layout.user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))
        self.assertEqual('delete done !.', response.data)

route.py:

@layout.route('/delete/<int:id>')
@login_required
def delete(id):
  """remove monkey"""
  user = Users.query.get(id)
  if g.user.id == user.id:
           flash('You are not allow to delete yourself.')

  else:
      db.session.delete(user)
      db.session.commit()
      flash('delete done !.')
  return redirect(url_for('layout.user',id=id, page=1,sortby='normal'))
LiLi
  • 301
  • 2
  • 7
  • 21
  • Have you had a look at your post from yesterday? You are asking the same question again! [link](http://stackoverflow.com/questions/28670041/test-coverage-for-flask-application-doesnt-work/28675412#28675412) – philmaweb Feb 24 '15 at 15:15
  • I didn't ask the same question change the delete in test and Got the error @philmaweb and yes I check all the command in include ur comment But doesn't work – LiLi Feb 24 '15 at 16:14

1 Answers1

0

You are getting the error because of your call to

response = self.client.get(url_for('layout.user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))

EDIT

You should change that call to

response = self.client.get(url_for('layout.delete', id=u1.id), follow_redirects=True)

see http://flask.pocoo.org/docs/0.10/blueprints/#building-urls for how to use blueprints and url_for

Zyber
  • 1,034
  • 8
  • 19
  • AssertionError: 'delete done !.' != ' \n404 Not Found\n

    Not Found

    \n

    The requested URL was not found on the server. If you entered the URL manually please check your spelling @Zyber got this error

    – LiLi Feb 24 '15 at 14:48
  • If you are using url_for you need to specify the blueprint to use. See my edit @LiLi – Zyber Feb 25 '15 at 09:25