New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rollback test data #15
Comments
Have you taken a look at the example in the wiki? There is an entire laravel example there (includes rolling back the database). |
Yes i do. Maybe i should explain my problem further. I have a create new user function, when i run dredd it will take the example in my apiary.apib and create the new user, so the second time i run dredd again it said email already used. So i was thinking maybe hook files db transaction only rollback any data created within the hook files only? So i do another test, instead of rollback the created data, i tried to delete the user "foo@bar.com" in hook files so that the email are not duplicated when i run dredd test, and rollback deletion after dredd, however foo@bar.com doesn't not deleted and my dredd still give error saying email already used, but it works if i remove the db begin transaction and roll back in aftereach and beforeeach. Below are my hook file
|
So here is what you need to be careful about. Anything that happens in a database transaction can only be seen by inside the same database connection that the transaction was created. If you use migrations for rolling back your issues will go away. Otherwise you need to make sure that your hooks file and your application code all have access to the same database connection. |
Let me know if that makes sense. If not I can clarify. |
I change transaction db to mysql now which is the one i use in the application, and also put a user creating in the hook files when api Users > create new user is call, so should have 2 user being created when dredd is run, one is the one created in hook files "warrence@warrence.com" and the one from the application "foo@bar.com", but only "warrence@warrence.com" that created in hook files being roll back, and "foo@bar.com" still there. |
So what is happening is your hooks are using a database connection that is using transactions, which is why the warrence@warrence.com is being rolled back. However, your app is NOT using the same connection, hence it is not in the database transaction, which means that the data never gets rolled back. But essentially Laravel's base database class So the project that I have done this previously was not a laravel one so I used the If you are still having trouble after that I can tweak your code to do what I just described, however, I probably wouldn't have time to help until the weekend. |
@ddelnano Thanks for your advise, i tried capsule manager but i dunno how to make it works with dredd test, please show me some code sample how to make it works in rollback transaction in application code in the hook files when you free. Thanks. |
Ok I will try to have that sample for you on Saturday or Sunday. |
In the mean time if you run your migrations in the before each and rollback the migrations in the after each that could work so that you can atleast get something working. |
@ddelnano i was wondering is it possible to use Illuminate\Foundation\Testing\DatabaseTransactions or Illuminate\Foundation\Testing\DatabaseMigrations inside hook files? |
Thanks @ddelnano |
Actually now that I think about this more, you will not be able to use transactions for this. There is no way that your application and the hooks can share the same database connection. I apologize for misleading you. The hooks run in a separate server (completely different php execution context) than your app. So you must use migrations. |
@warrence Did you try using migrations? |
@ddelnano yes it works well. For now I only have around 10 API function to test so every time it will research database and apply seed, it definitely slow down the test becoz I put it in the aftereach. I'm thinking how if I run seeding only without migrate in its own function using "after", and only seed the effected table, wonder if it works or not, but this gonna be lots of writing in the hook files. |
@ddelnano my mistake, seeding doesn't reset data, so still have to use migrate:refresh --seed. |
Ok I'm closing this since the resolution is you need to use migrations. |
Hi, may i know how should i rollback the created data during dread test?
The text was updated successfully, but these errors were encountered: