croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.
MIT License
63 stars 30 forks source link

Missing FixturesMixin #12

Closed yoanisgil closed 9 years ago

yoanisgil commented 9 years ago

Hi,

I cannot seem to find the FixturesMixin mentioned in the doc. Maybe it's something evident and I'm just missing it ...

Can you please let me know how is that supposed to work?

Bests,

Yoanis.

croach commented 9 years ago

Sorry Yoanis, I'm in between versions of the library now and some of the documentation for the new version made it's way into the README file for the old version. Rather than trying to rewrite the documentation for the old version, I just decided to merge in the latest code. Do a pull on the master branch and try walking through the updated README and let me know if you run into any problems.

yoanisgil commented 9 years ago

Is this newer version already available on pypi?

croach commented 9 years ago

Sorry Yoanis, but not yet. If you just want to use the current version that is on PyPI, you simply need to ignore the FixturesMixin completely. Just decorate your class with the fixtures decorator function and don't worry about inheriting from the mixin class. Now, if you're curious as to why the change...

So, the current version that is on PyPI is perfectly usable, but it has some limitations as to what I can do with it, hence the need to change it. Specifically, I want to be able to use the the decorator to mark individual methods with fixtures data to be setup and torn down before and after the test method runs. With the current version of the library, I can't do that, and so I'm changing it to use a metaclass instead of a decorator for all of the internal class manipulation that it's doing. I originally set out on this path to begin with, but I had this brilliant idea that I could do what I wanted without metaclasses, but unfortunately, I was wrong. In the new version, you'll inherit from the mixin class and identify the fixtures files you want to load through a pair of class variable's: fixtures and class_fixtures. For method specific fixtures, you'll then be able to use the decorator to mark the method as using a specific set of fixtures files. So, it will be slightly backwards incompatible, but it will be much more flexible and powerful going forward, and the changes that you'll need to make to your code when switching between versions is fairly trivial.

If you'd like to use the new version, you can do so, though it's not as thoroughly tested yet as the one in PyPI, but it should be fairly reliable. To use it, you'll have to pull it directly from github. You can do so with the following command:

pip install git+https://github.com/croach/Flask-Fixtures.git

I hope that helps. I should have the new version published to PyPI within the next few weeks, I just have a few more TODO's to check off before it goes live.

Thanks for your patience.

yoanisgil commented 9 years ago

Hi Christopher,

Thanks for the long and insightful explanation. I kinda have the seem needs you do so I am going to move ahead and give it a try to the latest version. I will let you know how that goes.

Bests,

Yoanis.

yoanisgil commented 9 years ago

Hi Christopher,

Why do I get this?

If you are using JSON for your fixtures, consider installing the dateutil library for more flexible parsing of dates and times.

when I run my tests with nose:

TEST=1 nosetests --with-coverage --cover-package pysage --cover-html project/tests/api/session_test.py

croach commented 9 years ago

Hi Yoanis,

It's just informational, nothing to be worried about. Basically, I use the following command to parse dates that you might have in your fixtures files:

datetime.strptime(dtstring, '%Y-%m-%d')

This is very limited, but as long as you only need to specify simple dates in your fixtures, you're fine. If, however, you'd like to specify dates/times in a different format, you'll need to install the dateutils library to do so. That's all that message is telling you. By the way, this isn't an issue at all if you use YAML, since it has very rich date/time parsing built in. I personally use YAML for all of my fixtures. I feel it's cleaner and easier to read and I like the fact that it allows me to add comments to my fixtures files.

yoanisgil commented 9 years ago

Thanks. Just wanted to let you know that I have migrated fixtures to the latest commit on master and everything seems to be working this far ;).

yoanisgil commented 9 years ago

Hi Christopher,

I was just wondering if with the latest release was possible to have a particular test method to use a specific fixture?

Bests,

Yoanis.

croach commented 9 years ago

Hi Yoanis,

So, the answer to your questions is, not yet. Though, the functionality you're asking for is the reason that I made the backwards incompatible switch from the last version of the library to the current one. I'm nearly finished with the functionality you mentioned, and I hope to have it in the library in the next weak or two. If you're curious as to what the problem was and why I switched from decorators to a combination of decorators and metaclasses, I've described the whole thing in the paragraphs below.

With the earlier approach you could decorate a test method with the fixtures decorator and that decorator would wrap the test method in a function that would setup the DB, and tear it down again, all around calling your test method. The problem is that if you add a custom setUp or tearDown method, both of those would be run before the test method is called. Since the test method runs after the custom setUp method, the DB fixtures setup method runs after it as well. What this means to you is that, if you wanted to do some additional work with the DB in your setup method, you couldn't since it hadn't been created yet.

In the new version, you will decorate a method with a function yet to be named, and it will mark the method as "special". Then when the class is being created, the metaclass will grab all of the special methods that you decorated and do a bit of magic to make sure that the fixtures setup will get called first, then the custom setUp function, and then the test method.

Hope that makes sense.

yoanisgil commented 9 years ago

Hi Christopher,

Yes this makes sense. In the mean time team I will figure something out ;). Let me know if there is anything I can do to help.

Bests,

Yoanis.

sylwit commented 9 years ago

Hi Christopher,

maybe it would be nice to also add a yaml fixtures file in the project ?

Thanks

croach commented 9 years ago

Hi @sylwit,

I added a sample YAML file (authors.yaml) to the documentation and I included the file in the test project as well. Hopefully, that'll help you out.

Thanks for the suggestion.

Christopher

sylwit commented 9 years ago

Hi @croach

thank you, you did it before me, I had a PR for you about this :)

Thanks for your work

Sylvain