miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.53k stars 4.21k forks source link

I met a problem in Chapter Test Web Application,new test code of git tag 15b. #59

Closed alexli0707 closed 9 years ago

alexli0707 commented 9 years ago

Thank you for sharing your knowledge,I have a nice flask tutorial with your book. But I got a problem when I do the test of test_client.py. I got "RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable." when goes in url_for('main.index). After I added SERVER_NAME='http://localhost:5000' to TestingConfig I got Page not found error.Can you help me ?tks

alexli0707 commented 9 years ago

After I add
self._ctx = self.app.test_request_context() self._ctx.push() after self.client = self.app.test_client(use_cookies=True) it works perfect!

miguelgrinberg commented 9 years ago

Can you give me the output of pip freeze?

alexli0707 commented 9 years ago

Here it is walembic==0.7.7 amqp==1.4.6 anyjson==0.3.3 app==0.0.2 appscript==1.0.1 argh==0.26.1 Babel==1.3 billiard==3.3.0.20 biplist==0.9 bleach==1.4.1 blinker==1.3 celery==3.1.17 click==4.0 colorama==0.2.7 config==0.3.9 coverage==3.7.1 Creoleparser==0.7.4 decorator==3.4.2 dominate==2.1.12 ecdsa==0.13 envoy==0.0.3 Fabric==1.10.2 Flask==0.10.1 Flask-Babel==0.9 Flask-Bootstrap==3.3.5.6 Flask-DebugToolbar==0.10.0 Flask-Email==1.4.4 Flask-HTTPAuth==2.5.0 Flask-Login==0.2.11 Flask-Mail==0.9.0 Flask-Migrate==1.5.0 Flask-Moment==0.5.0 Flask-OpenID==1.2.4 Flask-PageDown==0.2.1 Flask-Script==0.6.6 Flask-SQLAlchemy==2.0 Flask-WhooshAlchemy==0.56 Flask-WTF==0.11 flipflop==1.0 ForgeryPy==0.1 Genshi==0.7 gevent==1.0.1 greenlet==0.4.5 guess-language==0.2 gunicorn==19.3.0 html5lib==0.999999 httpie==0.9.2 itsdangerous==0.24 Jinja2==2.7.3 kombu==3.0.24 Mako==1.0.1 Markdown==2.6.2 MarkupSafe==0.23 mercurial==3.1.1 msgpack-python==0.4.6 mysql==0.0.1 mysql-connector-python==2.0.4 MySQL-python==1.2.5 osascript==0.0.4 paramiko==1.15.2 pathtools==0.1.2 pbr==0.11.0 Pillow==2.8.1 py==1.4.30 pycrypto==2.6.1 Pygments==2.0.2 pytest==2.7.2 python-openid==2.2.5 pytz==2015.2 PyYAML==3.11 raven==5.2.0 requests==2.7.0 simplejson==3.6.5 six==1.9.0 speaklater==1.3 SQLAlchemy==0.9.9 sqlalchemy-migrate==0.9.6 sqlparse==0.1.15 Tempita==0.5.2 umysql==2.61 virtualenv==12.1.1 watchdog==0.8.3 Werkzeug==0.10.4 Whoosh==2.7.0 WTForms==2.0.2

miguelgrinberg commented 9 years ago

Are you running the original code from this repository, or is this your own application? The virtualenv has a lot of packages that are not used by Flasky.

alexli0707 commented 9 years ago

Thanks for your time.I had reused virtualenv created by last project, maybe different version of flask cause this problem. By the way,I met another problem that I am trying hard to fixed it:After studying the whole book I deploy flasky on aws's ec2 instance with nginx and gunicorn.Before done this I ran

python manage runserver 

in localhost env it works perfect.But when I run it with

gunicorn  -b 0.0.0.0 manage:app  

I found SQLAlchemy cannot auto commit .I logged sqlalchemy.engine to file I find it always call Rollback after a SELECT perform and couldn't found INSERT perform without a commit() transfer .If I added

db.session.commit(self)

after

db.session.add(User) 

it worked fine.

miguelgrinberg commented 9 years ago

I found SQLAlchemy cannot auto commit

Did you get any errors regarding this commit failure?

alexli0707 commented 9 years ago

No errors ,it also flash 'You have confirmed your account. Thanks!' When I start with

python manage.py runserver

after every sql it log COMMIT.Everything goes fine. When I start with

gunicorn manage:app

after every sql it log ROLLBACK. I thought in gunicorn SQLALCHEMY_COMMIT_ON_TEARDOWN=True not worked.

miguelgrinberg commented 9 years ago

What's your database URL for the working and not working commits?

alexli0707 commented 9 years ago

Same as your demo project,Here is log:SQLALCHEMY_DATABASE_URI:sqlite:////Users/walker/Documents/python/github/flasky/data.sqlite

alexli0707 commented 9 years ago

Here is a part of sqlalchemy's log,only start server with gunicorn will cause this. SELECT roles.id AS roles_id, roles.name AS roles_name, roles."default" AS roles_default, roles.permissions AS roles_permissions FROM roles WHERE roles.id = ? (2,) SELECT count(*) AS count_1 FROM (SELECT comments.id AS comments_id, comments.body AS comments_body, comments.body_html AS comments_body_html, comments.timestamp AS comments_timestamp, comments.disabled AS comments_disabled, comments.author_id AS comments_author_id, comments.post_id AS comments_post_id FROM comments WHERE ? = comments.post_id) AS anon_1 (1,) ROLLBACK

sphkan commented 9 years ago

@miguelgrinberg : I also ran into the same problem, and I used @alexli0707 's fix. It affected both my test_api.py and my test_client.py files, and I fixed them by adding:

self._ctx = self.app.test_request_context()
self._ctx.push()

at the end of my setUp() functions.

My pip freeze is:

Flask-Bootstrap==3.0.3.1
Flask-HTTPAuth==2.2.0
Flask-Login==0.3.1
Flask-Mail==0.9.0
Flask-Migrate==1.1.0
Flask-Moment==0.2.1
Flask-PageDown==0.1.4
Flask-SQLAlchemy==1.0
Flask-Script==0.6.6
Flask-WTF==0.9.4
ForgeryPy==0.1
Jinja2==2.7.1
Mako==0.9.1
Markdown==2.3.1
MarkupSafe==0.18
Pygments==1.6
SQLAlchemy==0.9.9
WTForms==1.0.5
Werkzeug==0.10.4
alembic==0.6.2
argparse==1.2.1
bleach==1.4
blinker==1.3
colorama==0.2.7
coverage==3.7.1
html5lib==1.0b3
httpie==0.7.2
itsdangerous==0.23
requests==2.1.0
selenium==2.45.0
six==1.4.1
wsgiref==0.1.2
miguelgrinberg commented 9 years ago

This is a Flask bug that has been fixed already, but won't be available until a new release is made. See the PR for the issue.

sphkan commented 9 years ago

Thanks for the information, @miguelgrinberg!