librelist archives

« back to archive

Issues with Testing

Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 03:23
Hi all,
I've just started using Flask and am thoroughly enjoying it. However, I've
gotten myself stuck with trying to write tests under some (probably overly)
complex app and db setups.

In short, the app instance in my test case doesn't seem to know about any of
the URLs I am trying to have the test_client retrieve.

I am using a function to create my app that looks like this:

def create_app(env):
    """
    Creates the application with a config based on the passed in `env`.

    Also handles initing the DB and disconnecting before and after a request
    respectively.
    """
    if env == 'test':
        config = 'configs/test.py'
    else:
        config = 'configs/config.py'
    app = FlaskOptional(__name__)
    app.config.from_pyfile(config)
    from seeandtake.views import surveys
    app.register_module(surveys)

    @app.before_request
    def before_request():
        init_db(app.config)

    @app.after_request
    def after_request(response):
        g.db.connection.disconnect()
        return response

    return app

This works fine when I run the app using something like gunicorn or any
other WSGI server. However, in my tests, the views imported above as the
`surveys` module seem to not exist. Here's the setup for my tests:

@classmethod
def setUpClass(cls):
    """
    Set up the app and db for all of the tests.

    This takes a little time, so it's easier to do once per test run.
    Creating the app will give the context to the views for the correct
    database if the test config is used.
    """
    cls.app = create_app('test')
    connection = Connection(cls.app.config['DB_SERVER'],
cls.app.config['DB_PORT'])
    cls.db = connection[cls.app.config['DATABASE']]

@classmethod
def tearDownClass(cls):
    """Tear down the db at the end of all the tests."""
    cls.db.connection.drop_database('seeandtaketest')
    cls.db.connection.disconnect()

def setUp(self):
    """Set up the fixtures before each test."""
    self.insert_fixtures()

def tearDown(self):
    """Clear out the fixtures after each test.."""
    self.db.drop_collection('snttest')

I have several tests that just check the database to see if the fixtures are
loaded and they work fine. My test that actually use URLs look something
like this:

def test_bogus_survey_page(self):
    survey = self.db.snttest.find_one()
    url = '/survey/%s' % survey['_id']
    with self.app.test_client() as c:
        rv = c.get(url, follow_redirects=True)
        assert 'Bacon' in rv.data

The `get` call here will always result in a 404 which shouldn't be the case.
I've confirmed that the db query finds what it should and that the URL is
built correctly. I also know that the URL works because the same URL works
when the app is run as a WSGI app and not in these tests. So, that leads me
to believe that this test app just doesn't know about the routes in the
`surveys` module mentioned above.

I've tried all sorts of things to fix this including this snippet (
http://flask.pocoo.org/snippets/26/) which gives an error as written, so I'm
not sure if that might be a path to a solution.

If someone could point me in the right direction, I could sleep tonight :)

Thanks for reading this long email. All this aside, I'm really happy to be
able to use Flask in this new project. It's been really fun so far.

Alex

Re: [flask] Issues with Testing

From:
Dag Odenhall
Date:
2010-11-09 @ 07:27
On Mon, 2010-11-08 at 21:23 -0600, Alex Ezell wrote:
> ...

Why are you setting up the testcase on a classlevel? Anyway you might
have better luck with Flask-Testing, have you tried it?

http://packages.python.org/Flask-Testing/

Example app: http://bitbucket.org/danjac/newsmeme/src

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 14:22
Hi Dag,
Thanks for replying. I appreciate the help.

On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com> wrote:
>
>
> Why are you setting up the testcase on a classlevel?


There is some DB init in the setup that can take a few seconds. I didn't
really want to incur a 3 second hit for every test that gets run, so I
thought I'd do it once per class. That said, I've had other permutations
that didn't use any of the class-level setup and teardown structure that
still exhibited the problem I mentioned which was having the view URLs not
available to the test app instance.


> Anyway you might
> have better luck with Flask-Testing, have you tried it?


> http://packages.python.org/Flask-Testing/


I just found it right before sending this message. I might try it out, but
I'd still like to understand why what I've done doesn't work. It's nice if
this fixes it, but understanding the why is important to me.

Thanks again for your feedback.

Alex

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 15:26
Hi Dag,

On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com> wrote:

Anyway you might have better luck with Flask-Testing, have you tried it?
>
> http://packages.python.org/Flask-Testing/


I gave this a try and my tests still don't seem to know anything about my
routes. They URLs still return a 404 when I can paste the same URL into the
browser and it works as expected. Here's the code I'm currently trying:
http://pastebin.com/Z1BeNT9w

Alex

Re: [flask] Issues with Testing

From:
danjac354@gmail.com
Date:
2010-11-09 @ 15:29
Where are your routes defined ?

On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
> Hi Dag,
> On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com> wrote:
>>
>> Anyway you might have better luck with Flask-Testing, have you tried it?
>>
>> http://packages.python.org/Flask-Testing/
>
> I gave this a try and my tests still don't seem to know anything about my
> routes. They URLs still return a 404 when I can paste the same URL into the
> browser and it works as expected. Here's the code I'm currently
> trying: http://pastebin.com/Z1BeNT9w
> Alex

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 15:43
Hi Dan,
I have them in a views.py file like this:

surveys = Module(__name__, url_prefix='/survey')

@surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
@surveys.route('/<survey_id>', methods=['GET', 'POST'])
def survey(survey_id, page = 1):
    # view code here

In the `create_app` function, I do this:

from seeandtake.views import surveys
app.register_module(surveys)

Alex

On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com <danjac354@gmail.com>wrote:

> Where are your routes defined ?
>
> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
> > Hi Dag,
> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com>
> wrote:
> >>
> >> Anyway you might have better luck with Flask-Testing, have you tried it?
> >>
> >> http://packages.python.org/Flask-Testing/
> >
> > I gave this a try and my tests still don't seem to know anything about my
> > routes. They URLs still return a 404 when I can paste the same URL into
> the
> > browser and it works as expected. Here's the code I'm currently
> > trying: http://pastebin.com/Z1BeNT9w
> > Alex
>

Re: [flask] Issues with Testing

From:
danjac354@gmail.com
Date:
2010-11-09 @ 15:45
Just a guess - but could it be to do with the trailing slash ?

i.e. instead of "/survey/10" should be "/survey/10/"

On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
> Hi Dan,
> I have them in a views.py file like this:
> surveys = Module(__name__, url_prefix='/survey')
> @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
> @surveys.route('/<survey_id>', methods=['GET', 'POST'])
> def survey(survey_id, page = 1):
>     # view code here
> In the `create_app` function, I do this:
> from seeandtake.views import surveys
> app.register_module(surveys)
> Alex
> On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com <danjac354@gmail.com>
> wrote:
>>
>> Where are your routes defined ?
>>
>> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
>> > Hi Dag,
>> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com>
>> > wrote:
>> >>
>> >> Anyway you might have better luck with Flask-Testing, have you tried
>> >> it?
>> >>
>> >> http://packages.python.org/Flask-Testing/
>> >
>> > I gave this a try and my tests still don't seem to know anything about
>> > my
>> > routes. They URLs still return a 404 when I can paste the same URL into
>> > the
>> > browser and it works as expected. Here's the code I'm currently
>> > trying: http://pastebin.com/Z1BeNT9w
>> > Alex
>
>

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 15:49
I sort of wish it were that easy, but I'm kind of glad it's not, because I
would feel like an idiot.

I tried adding the trailing slashes and I still get a 404. Using the
trailing slashed on the code that works via the app in the browser also
fails. That is, the working code does not require the trailing slash. I need
to read up on how Flask handles those slashes, but I don't think it's the
issue in this case.

Alex

On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com <danjac354@gmail.com>wrote:

> Just a guess - but could it be to do with the trailing slash ?
>
> i.e. instead of "/survey/10" should be "/survey/10/"
>
> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
> > Hi Dan,
> > I have them in a views.py file like this:
> > surveys = Module(__name__, url_prefix='/survey')
> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
> > def survey(survey_id, page = 1):
> >     # view code here
> > In the `create_app` function, I do this:
> > from seeandtake.views import surveys
> > app.register_module(surveys)
> > Alex
> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com <danjac354@gmail.com
> >
> > wrote:
> >>
> >> Where are your routes defined ?
> >>
> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
> >> > Hi Dag,
> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com>
> >> > wrote:
> >> >>
> >> >> Anyway you might have better luck with Flask-Testing, have you tried
> >> >> it?
> >> >>
> >> >> http://packages.python.org/Flask-Testing/
> >> >
> >> > I gave this a try and my tests still don't seem to know anything about
> >> > my
> >> > routes. They URLs still return a 404 when I can paste the same URL
> into
> >> > the
> >> > browser and it works as expected. Here's the code I'm currently
> >> > trying: http://pastebin.com/Z1BeNT9w
> >> > Alex
> >
> >
>

Re: [flask] Issues with Testing

From:
danjac354@gmail.com
Date:
2010-11-09 @ 16:18
I also wonder if the order of the two routes is important - it could
be that the first one checks for an int value and fails before
reaching the second.

Try instead:

@surveys.route('/<survey_id>/', methods=['GET', 'POST'])
@surveys.route('/<survey_id>/<int:page>/', methods=['GET', 'POST'])

On 9 November 2010 15:49, Alex Ezell <aezell@gmail.com> wrote:
> I sort of wish it were that easy, but I'm kind of glad it's not, because I
> would feel like an idiot.
> I tried adding the trailing slashes and I still get a 404. Using the
> trailing slashed on the code that works via the app in the browser also
> fails. That is, the working code does not require the trailing slash. I need
> to read up on how Flask handles those slashes, but I don't think it's the
> issue in this case.
> Alex
> On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com <danjac354@gmail.com>
> wrote:
>>
>> Just a guess - but could it be to do with the trailing slash ?
>>
>> i.e. instead of "/survey/10" should be "/survey/10/"
>>
>> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
>> > Hi Dan,
>> > I have them in a views.py file like this:
>> > surveys = Module(__name__, url_prefix='/survey')
>> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
>> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
>> > def survey(survey_id, page = 1):
>> >     # view code here
>> > In the `create_app` function, I do this:
>> > from seeandtake.views import surveys
>> > app.register_module(surveys)
>> > Alex
>> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com
>> > <danjac354@gmail.com>
>> > wrote:
>> >>
>> >> Where are your routes defined ?
>> >>
>> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
>> >> > Hi Dag,
>> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <dag.odenhall@gmail.com>
>> >> > wrote:
>> >> >>
>> >> >> Anyway you might have better luck with Flask-Testing, have you tried
>> >> >> it?
>> >> >>
>> >> >> http://packages.python.org/Flask-Testing/
>> >> >
>> >> > I gave this a try and my tests still don't seem to know anything
>> >> > about
>> >> > my
>> >> > routes. They URLs still return a 404 when I can paste the same URL
>> >> > into
>> >> > the
>> >> > browser and it works as expected. Here's the code I'm currently
>> >> > trying: http://pastebin.com/Z1BeNT9w
>> >> > Alex
>> >
>> >
>
>

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 16:27
Thanks Dan.

Actually, the routes work just fine as written when I run the app with
something like gunicorn. As I understand it, you'd want the more specific
match first, such that you can use a single view regardless of whether
someone gives you a page number. As written if someone requests
'/survey/<survey_id>/10' then they'd get the correct view. If someone
requests '/survey/<survey_id>', that first match would fail, but the second
would not, so they'd get the correct view. Of course, all this is based on
the fact that it works (except during the tests) and that I assume it's
somewhat similar to the routing in Django insofar as how it matches a list
of routes to a view.

The issue here is that no routes work at all in my tests. Every call to
every possible permutation of those routes results in a 404. That is, the
`app` instance created in the tests just doesn't seem to know that those
routes exist at all, despite the fact that it comes from the same
`create_app` function.

Alex

On Tue, Nov 9, 2010 at 10:18 AM, danjac354@gmail.com <danjac354@gmail.com>wrote:

> I also wonder if the order of the two routes is important - it could
> be that the first one checks for an int value and fails before
> reaching the second.
>
> Try instead:
>
> @surveys.route('/<survey_id>/', methods=['GET', 'POST'])
> @surveys.route('/<survey_id>/<int:page>/', methods=['GET', 'POST'])
>
> On 9 November 2010 15:49, Alex Ezell <aezell@gmail.com> wrote:
> > I sort of wish it were that easy, but I'm kind of glad it's not, because
> I
> > would feel like an idiot.
> > I tried adding the trailing slashes and I still get a 404. Using the
> > trailing slashed on the code that works via the app in the browser also
> > fails. That is, the working code does not require the trailing slash. I
> need
> > to read up on how Flask handles those slashes, but I don't think it's the
> > issue in this case.
> > Alex
> > On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com <danjac354@gmail.com
> >
> > wrote:
> >>
> >> Just a guess - but could it be to do with the trailing slash ?
> >>
> >> i.e. instead of "/survey/10" should be "/survey/10/"
> >>
> >> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
> >> > Hi Dan,
> >> > I have them in a views.py file like this:
> >> > surveys = Module(__name__, url_prefix='/survey')
> >> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
> >> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
> >> > def survey(survey_id, page = 1):
> >> >     # view code here
> >> > In the `create_app` function, I do this:
> >> > from seeandtake.views import surveys
> >> > app.register_module(surveys)
> >> > Alex
> >> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com
> >> > <danjac354@gmail.com>
> >> > wrote:
> >> >>
> >> >> Where are your routes defined ?
> >> >>
> >> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
> >> >> > Hi Dag,
> >> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall <
> dag.odenhall@gmail.com>
> >> >> > wrote:
> >> >> >>
> >> >> >> Anyway you might have better luck with Flask-Testing, have you
> tried
> >> >> >> it?
> >> >> >>
> >> >> >> http://packages.python.org/Flask-Testing/
> >> >> >
> >> >> > I gave this a try and my tests still don't seem to know anything
> >> >> > about
> >> >> > my
> >> >> > routes. They URLs still return a 404 when I can paste the same URL
> >> >> > into
> >> >> > the
> >> >> > browser and it works as expected. Here's the code I'm currently
> >> >> > trying: http://pastebin.com/Z1BeNT9w
> >> >> > Alex
> >> >
> >> >
> >
> >
>

Re: [flask] Issues with Testing

From:
danjac354@gmail.com
Date:
2010-11-09 @ 16:28
Check the url_map in your test app to see what routes are actually available.

On 9 November 2010 16:27, Alex Ezell <aezell@gmail.com> wrote:
> Thanks Dan.
> Actually, the routes work just fine as written when I run the app with
> something like gunicorn. As I understand it, you'd want the more specific
> match first, such that you can use a single view regardless of whether
> someone gives you a page number. As written if someone requests
> '/survey/<survey_id>/10' then they'd get the correct view. If someone
> requests '/survey/<survey_id>', that first match would fail, but the second
> would not, so they'd get the correct view. Of course, all this is based on
> the fact that it works (except during the tests) and that I assume it's
> somewhat similar to the routing in Django insofar as how it matches a list
> of routes to a view.
> The issue here is that no routes work at all in my tests. Every call to
> every possible permutation of those routes results in a 404. That is, the
> `app` instance created in the tests just doesn't seem to know that those
> routes exist at all, despite the fact that it comes from the same
> `create_app` function.
> Alex
>
> On Tue, Nov 9, 2010 at 10:18 AM, danjac354@gmail.com <danjac354@gmail.com>
> wrote:
>>
>> I also wonder if the order of the two routes is important - it could
>> be that the first one checks for an int value and fails before
>> reaching the second.
>>
>> Try instead:
>>
>> @surveys.route('/<survey_id>/', methods=['GET', 'POST'])
>> @surveys.route('/<survey_id>/<int:page>/', methods=['GET', 'POST'])
>>
>> On 9 November 2010 15:49, Alex Ezell <aezell@gmail.com> wrote:
>> > I sort of wish it were that easy, but I'm kind of glad it's not, because
>> > I
>> > would feel like an idiot.
>> > I tried adding the trailing slashes and I still get a 404. Using the
>> > trailing slashed on the code that works via the app in the browser also
>> > fails. That is, the working code does not require the trailing slash. I
>> > need
>> > to read up on how Flask handles those slashes, but I don't think it's
>> > the
>> > issue in this case.
>> > Alex
>> > On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com
>> > <danjac354@gmail.com>
>> > wrote:
>> >>
>> >> Just a guess - but could it be to do with the trailing slash ?
>> >>
>> >> i.e. instead of "/survey/10" should be "/survey/10/"
>> >>
>> >> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
>> >> > Hi Dan,
>> >> > I have them in a views.py file like this:
>> >> > surveys = Module(__name__, url_prefix='/survey')
>> >> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
>> >> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
>> >> > def survey(survey_id, page = 1):
>> >> >     # view code here
>> >> > In the `create_app` function, I do this:
>> >> > from seeandtake.views import surveys
>> >> > app.register_module(surveys)
>> >> > Alex
>> >> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com
>> >> > <danjac354@gmail.com>
>> >> > wrote:
>> >> >>
>> >> >> Where are your routes defined ?
>> >> >>
>> >> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
>> >> >> > Hi Dag,
>> >> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall
>> >> >> > <dag.odenhall@gmail.com>
>> >> >> > wrote:
>> >> >> >>
>> >> >> >> Anyway you might have better luck with Flask-Testing, have you
>> >> >> >> tried
>> >> >> >> it?
>> >> >> >>
>> >> >> >> http://packages.python.org/Flask-Testing/
>> >> >> >
>> >> >> > I gave this a try and my tests still don't seem to know anything
>> >> >> > about
>> >> >> > my
>> >> >> > routes. They URLs still return a 404 when I can paste the same URL
>> >> >> > into
>> >> >> > the
>> >> >> > browser and it works as expected. Here's the code I'm currently
>> >> >> > trying: http://pastebin.com/Z1BeNT9w
>> >> >> > Alex
>> >> >
>> >> >
>> >
>> >
>
>

Re: [flask] Issues with Testing

From:
Alex Ezell
Date:
2010-11-09 @ 18:26
I didn't know that existed. Thanks.

It turns out a owe you a huge apology for having wasted your time. Once I
used `url_map`, I saw that the routes were indeed correct. Then, I started
looking more closely at the view code and the fixtures. Turns out that all
my tests to ensure the fixtures were loaded passed just fine. The code that
was getting the survey id to use in the URL was working fine as well.
However, the view code was using a different collection in mongo. Because of
error handling in the view, it was returning a 404.

So, in short, I'm a victim of my own error-checking and sloppy collection
naming.

I really appreciate the time and help. I also appreciate your Flask-WTF and
Flask-Testing. They've been really nice to work with.

Alex

On Tue, Nov 9, 2010 at 10:28 AM, danjac354@gmail.com <danjac354@gmail.com>wrote:

> Check the url_map in your test app to see what routes are actually
> available.
>
> On 9 November 2010 16:27, Alex Ezell <aezell@gmail.com> wrote:
> > Thanks Dan.
> > Actually, the routes work just fine as written when I run the app with
> > something like gunicorn. As I understand it, you'd want the more specific
> > match first, such that you can use a single view regardless of whether
> > someone gives you a page number. As written if someone requests
> > '/survey/<survey_id>/10' then they'd get the correct view. If someone
> > requests '/survey/<survey_id>', that first match would fail, but the
> second
> > would not, so they'd get the correct view. Of course, all this is based
> on
> > the fact that it works (except during the tests) and that I assume it's
> > somewhat similar to the routing in Django insofar as how it matches a
> list
> > of routes to a view.
> > The issue here is that no routes work at all in my tests. Every call to
> > every possible permutation of those routes results in a 404. That is, the
> > `app` instance created in the tests just doesn't seem to know that those
> > routes exist at all, despite the fact that it comes from the same
> > `create_app` function.
> > Alex
> >
> > On Tue, Nov 9, 2010 at 10:18 AM, danjac354@gmail.com <
> danjac354@gmail.com>
> > wrote:
> >>
> >> I also wonder if the order of the two routes is important - it could
> >> be that the first one checks for an int value and fails before
> >> reaching the second.
> >>
> >> Try instead:
> >>
> >> @surveys.route('/<survey_id>/', methods=['GET', 'POST'])
> >> @surveys.route('/<survey_id>/<int:page>/', methods=['GET', 'POST'])
> >>
> >> On 9 November 2010 15:49, Alex Ezell <aezell@gmail.com> wrote:
> >> > I sort of wish it were that easy, but I'm kind of glad it's not,
> because
> >> > I
> >> > would feel like an idiot.
> >> > I tried adding the trailing slashes and I still get a 404. Using the
> >> > trailing slashed on the code that works via the app in the browser
> also
> >> > fails. That is, the working code does not require the trailing slash.
> I
> >> > need
> >> > to read up on how Flask handles those slashes, but I don't think it's
> >> > the
> >> > issue in this case.
> >> > Alex
> >> > On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com
> >> > <danjac354@gmail.com>
> >> > wrote:
> >> >>
> >> >> Just a guess - but could it be to do with the trailing slash ?
> >> >>
> >> >> i.e. instead of "/survey/10" should be "/survey/10/"
> >> >>
> >> >> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
> >> >> > Hi Dan,
> >> >> > I have them in a views.py file like this:
> >> >> > surveys = Module(__name__, url_prefix='/survey')
> >> >> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
> >> >> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
> >> >> > def survey(survey_id, page = 1):
> >> >> >     # view code here
> >> >> > In the `create_app` function, I do this:
> >> >> > from seeandtake.views import surveys
> >> >> > app.register_module(surveys)
> >> >> > Alex
> >> >> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com
> >> >> > <danjac354@gmail.com>
> >> >> > wrote:
> >> >> >>
> >> >> >> Where are your routes defined ?
> >> >> >>
> >> >> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
> >> >> >> > Hi Dag,
> >> >> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall
> >> >> >> > <dag.odenhall@gmail.com>
> >> >> >> > wrote:
> >> >> >> >>
> >> >> >> >> Anyway you might have better luck with Flask-Testing, have you
> >> >> >> >> tried
> >> >> >> >> it?
> >> >> >> >>
> >> >> >> >> http://packages.python.org/Flask-Testing/
> >> >> >> >
> >> >> >> > I gave this a try and my tests still don't seem to know anything
> >> >> >> > about
> >> >> >> > my
> >> >> >> > routes. They URLs still return a 404 when I can paste the same
> URL
> >> >> >> > into
> >> >> >> > the
> >> >> >> > browser and it works as expected. Here's the code I'm currently
> >> >> >> > trying: http://pastebin.com/Z1BeNT9w
> >> >> >> > Alex
> >> >> >
> >> >> >
> >> >
> >> >
> >
> >
>

Re: [flask] Issues with Testing

From:
danjac354@gmail.com
Date:
2010-11-09 @ 18:28
Glad it's working OK. Don't worry, we all make mistakes like this from
time to time :-)

On 9 November 2010 18:26, Alex Ezell <aezell@gmail.com> wrote:
> I didn't know that existed. Thanks.
> It turns out a owe you a huge apology for having wasted your time. Once I
> used `url_map`, I saw that the routes were indeed correct. Then, I started
> looking more closely at the view code and the fixtures. Turns out that all
> my tests to ensure the fixtures were loaded passed just fine. The code that
> was getting the survey id to use in the URL was working fine as well.
> However, the view code was using a different collection in mongo. Because of
> error handling in the view, it was returning a 404.
> So, in short, I'm a victim of my own error-checking and sloppy collection
> naming.
> I really appreciate the time and help. I also appreciate your Flask-WTF and
> Flask-Testing. They've been really nice to work with.
> Alex
> On Tue, Nov 9, 2010 at 10:28 AM, danjac354@gmail.com <danjac354@gmail.com>
> wrote:
>>
>> Check the url_map in your test app to see what routes are actually
>> available.
>>
>> On 9 November 2010 16:27, Alex Ezell <aezell@gmail.com> wrote:
>> > Thanks Dan.
>> > Actually, the routes work just fine as written when I run the app with
>> > something like gunicorn. As I understand it, you'd want the more
>> > specific
>> > match first, such that you can use a single view regardless of whether
>> > someone gives you a page number. As written if someone requests
>> > '/survey/<survey_id>/10' then they'd get the correct view. If someone
>> > requests '/survey/<survey_id>', that first match would fail, but the
>> > second
>> > would not, so they'd get the correct view. Of course, all this is based
>> > on
>> > the fact that it works (except during the tests) and that I assume it's
>> > somewhat similar to the routing in Django insofar as how it matches a
>> > list
>> > of routes to a view.
>> > The issue here is that no routes work at all in my tests. Every call to
>> > every possible permutation of those routes results in a 404. That is,
>> > the
>> > `app` instance created in the tests just doesn't seem to know that those
>> > routes exist at all, despite the fact that it comes from the same
>> > `create_app` function.
>> > Alex
>> >
>> > On Tue, Nov 9, 2010 at 10:18 AM, danjac354@gmail.com
>> > <danjac354@gmail.com>
>> > wrote:
>> >>
>> >> I also wonder if the order of the two routes is important - it could
>> >> be that the first one checks for an int value and fails before
>> >> reaching the second.
>> >>
>> >> Try instead:
>> >>
>> >> @surveys.route('/<survey_id>/', methods=['GET', 'POST'])
>> >> @surveys.route('/<survey_id>/<int:page>/', methods=['GET', 'POST'])
>> >>
>> >> On 9 November 2010 15:49, Alex Ezell <aezell@gmail.com> wrote:
>> >> > I sort of wish it were that easy, but I'm kind of glad it's not,
>> >> > because
>> >> > I
>> >> > would feel like an idiot.
>> >> > I tried adding the trailing slashes and I still get a 404. Using the
>> >> > trailing slashed on the code that works via the app in the browser
>> >> > also
>> >> > fails. That is, the working code does not require the trailing slash.
>> >> > I
>> >> > need
>> >> > to read up on how Flask handles those slashes, but I don't think it's
>> >> > the
>> >> > issue in this case.
>> >> > Alex
>> >> > On Tue, Nov 9, 2010 at 9:45 AM, danjac354@gmail.com
>> >> > <danjac354@gmail.com>
>> >> > wrote:
>> >> >>
>> >> >> Just a guess - but could it be to do with the trailing slash ?
>> >> >>
>> >> >> i.e. instead of "/survey/10" should be "/survey/10/"
>> >> >>
>> >> >> On 9 November 2010 15:43, Alex Ezell <aezell@gmail.com> wrote:
>> >> >> > Hi Dan,
>> >> >> > I have them in a views.py file like this:
>> >> >> > surveys = Module(__name__, url_prefix='/survey')
>> >> >> > @surveys.route('/<survey_id>/<int:page>', methods=['GET', 'POST'])
>> >> >> > @surveys.route('/<survey_id>', methods=['GET', 'POST'])
>> >> >> > def survey(survey_id, page = 1):
>> >> >> >     # view code here
>> >> >> > In the `create_app` function, I do this:
>> >> >> > from seeandtake.views import surveys
>> >> >> > app.register_module(surveys)
>> >> >> > Alex
>> >> >> > On Tue, Nov 9, 2010 at 9:29 AM, danjac354@gmail.com
>> >> >> > <danjac354@gmail.com>
>> >> >> > wrote:
>> >> >> >>
>> >> >> >> Where are your routes defined ?
>> >> >> >>
>> >> >> >> On 9 November 2010 15:26, Alex Ezell <aezell@gmail.com> wrote:
>> >> >> >> > Hi Dag,
>> >> >> >> > On Tue, Nov 9, 2010 at 1:27 AM, Dag Odenhall
>> >> >> >> > <dag.odenhall@gmail.com>
>> >> >> >> > wrote:
>> >> >> >> >>
>> >> >> >> >> Anyway you might have better luck with Flask-Testing, have you
>> >> >> >> >> tried
>> >> >> >> >> it?
>> >> >> >> >>
>> >> >> >> >> http://packages.python.org/Flask-Testing/
>> >> >> >> >
>> >> >> >> > I gave this a try and my tests still don't seem to know
>> >> >> >> > anything
>> >> >> >> > about
>> >> >> >> > my
>> >> >> >> > routes. They URLs still return a 404 when I can paste the same
>> >> >> >> > URL
>> >> >> >> > into
>> >> >> >> > the
>> >> >> >> > browser and it works as expected. Here's the code I'm currently
>> >> >> >> > trying: http://pastebin.com/Z1BeNT9w
>> >> >> >> > Alex
>> >> >> >
>> >> >> >
>> >> >
>> >> >
>> >
>> >
>
>