-
Notifications
You must be signed in to change notification settings - Fork 22
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
CreateAPITestCaseMixin using 2 saves on 2 different objects #29
Comments
Hey @lukeaus, thanks for reporting! I agree that the extra object creation seems redundant in this case, but what I can't figure is why your test is failing on primary key violation. Why should the object created in |
Just to be clear, it's not failing on the same primary key, its failing on the same foreign key on this model. The models.OneToOneField means that only one entry in WorkReady can have that foreignKey.
Here is my failing test implementation
Thanks |
So the problem here is your logic in
And that should solve it (: |
Thanks for the reply @ydaniv Unfortunately that doesn't solve it :( WorkerFactory() is not what is creating the object under test. WorkReadyFactory() is doing that. My Worker instance created by WorkerFactory() needs to be created then passed to WorkReadyFactory(). Using the code you suggested gives me this error:
Because my WorkReadyFactory requires WorkReady() to have already been run, I have to overwrite setUp()
In addition, it feels like a code smell to be setting self.xxx outside of init or setUp() methods. Thanks |
@lukeaus ok, that was just a general idea. So instead try setting Shouldn't this solve it? |
This works
However only works as a work around to the main issue. I only want one worker under test, not 2. So the main issue still stands:
Possible solution:
Could make backwards compatible by looking for Thanks |
@lukeaus still sounds to me like a minor optimization, but if you could put this together into a PR I'll gladly review and consider (: Thanks! |
Problem
I am getting an error that a django models.OneToOneField must be unique.
The cause of this error is saving 2 different model instances with the same primary key.
What is happening is that in my model I have a django models.OneToOneField:
Using CreateAPITestCaseMixin requires:
In BaseRESTAPITestCase.setUp() the object is getting created:
So an object gets created in BaseRESTAPITestCase.setUp(), then another object gets created in CreateAPITestCaseMixin.test_create()
You also can't not create an object in setUp() as CreateAPITestCaseMixin.test_create() requires self.object, which is set in BaseRESTAPITestCase.setUp().
Possible solution
Thanks for making django-rest-assured!
Happy to submit a PR for this issue.
The text was updated successfully, but these errors were encountered: