Copy all the data from an existing database when create the testing database.

408 views
Skip to first unread message

Tianyi Wang

unread,
Oct 4, 2013, 9:48:45 AM10/4/13
to django...@googlegroups.com
Hi,

When I run my tests, I'd like it to copy an existing database with all the data when create the testing database.

I never thought about this till one of my colleague uses POSTGIS_TEMPLATE = DATABASES['default']['NAME'] for his tests. 
Because we use GeoDjango for our project, so POSTGIS_TEMPLATE exists for letting the test runner know which postgis database template to use.
And when he define it as mentioned above, it actually creates the test database with all the data as well. It's kinda handy for us, so we don't need to create the fixture files.
But if we can not do the same with the projects which do not use GeoDjango. 

So I have two questions here:
  1. Is it good idea to use the data already exists in the project database? (For some tests, I may need to delete all the data first for a certain table, but in many cases, we test against non empty data set.)
  2. If it's not a bad idea, for the project which does not use GeoDjango, is there anyway I can set it to copy the data from the existing database?
Thanks

Tianyi

C. Kirby

unread,
Oct 4, 2013, 12:08:02 PM10/4/13
to django...@googlegroups.com
Do you always want all of the data from the existing database, or is this just a quick way to have "real data" for testing.
If it is the latter I would use manage.py dumpdata to generate test fixtures. You can load the fixtures in you tests as needed.

Chaim

Tianyi Wang

unread,
Oct 4, 2013, 12:17:51 PM10/4/13
to django...@googlegroups.com
Yes, it'd be nice to have all the data from the existing database.
I am aware the loading fixtures way, but I will need to do that every time I know the existing database has changed a lot. 
So just copy the data when creating the test database seems more convenient. 

Tianyi

Kelvin Wong

unread,
Oct 5, 2013, 7:51:49 PM10/5/13
to django...@googlegroups.com
Consider using model factories (you can roll your own) or FactoryBoy


K

Tianyi Wang

unread,
Oct 7, 2013, 7:13:32 AM10/7/13
to django...@googlegroups.com
Thanks Kelvin, I will have a look at this. It seems interesting.
>>>> 1. Is it good idea to use the data already exists in the project
>>>> database? (For some tests, I may need to delete all the data first for a
>>>> certain table, but in many cases, we test against non empty data set.)
>>>> 2. If it's not a bad idea, for the project which does not use
>>>> GeoDjango, is there anyway I can set it to copy the data from the existing
>>>> database?
>>>>
>>>> Thanks
>>>>
>>>> Tianyi
>>>>
>>>


--
Tianyi
Reply all
Reply to author
Forward
0 new messages