-
Notifications
You must be signed in to change notification settings - Fork 392
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
Allow SQLAlchemy factories to be committed on create #309
Comments
I think this was solved by #262 I'm going to close this, but if for some reason you're looking for something different leave a comment and we can re-open. |
Hey @jeffwidman thanks for the quick response! enabling The situation I am running into is that I have server-side defaults on all of my models for This means that if I only ever call I am hoping to test that when I update an object through an api, the For this reason I want |
Oh my bad and you even mentioned I'm not opposed to adding a So if you call one of those, as long as the statement is flushed to the DB, it should get the actual timestamp as of right now. IIRC, you can call them from SQLAlchemy via Lastly, this is an aside from the actual issue but I'm curious how you're setting server-side defaults for |
Hey! No worries. Thanks for the tip on As for my |
Sounds fine, I'm certainly open to the PR. For the implementation, I'd like to deprecate the At the next major version bump we can drop the |
@jeffwidman indeed, if we want to support multiple post-creation behaviors, a unique parameters seems to be the way to go. What about |
Hey y'all! I agree, moving to a single flag makes a lot of sense. I propose Also definitely excited that you plan on deprecating I just opened a pr at #310. I suggest we move this conversation over there. |
What about Agree that it's generally weird to mix types, but
FYI: Let's keep the general design discussion here in the issue. Comments on the specific code implementation can go in the PR. Sometimes in the world of open source PRs get abandoned and since they can't be re-assigned to another user, we as maintainers have to close them eventually... it's annoying when another person comes along to fix later to say "please look at this issue, plus the other notes over in this rejected PR". Better thing is keep the general design discussion in the issue, then everything is in one place for someone else to take a stab at later if necessary. |
Good point on keeping the conversation isolated in one location, I like that idea a lot. I also agree that
@rbarrois do you have a strong opinion between |
I just want to confirm that this will end up changing the behavior of all the |
Yes, the default |
Exactly, the default persistence strategy will not be changing, so honestly this doesn't change the behavior of constructor calls. Of course anyone relying on 'MyFactory(...)' to flush the session via 'force_flush' will have to switch to 'sqlalchemy_session_persistence' (or what ever we settle on) but other than that this change shouldn't change any existing behavior |
Just wanted to point it out - I'm totally fine with it. We're actually using |
@zyskowsk it seems that Regarding the deprecation path, what do you think about the following plan:
Beyond this, I'm unsure as to the impact on
Final question: I'm not familiar with |
Yes, definitely. There might be some edge cases where you only want to set it on a specific factory, or override the global setting in a specific factory, but most common case you'll want to set it globally. Interested in hearing other opinions on this. |
Hey y'all! Sorry i got side tracked for a bit there. @rbarrois as @jeffwidman mentioned, yes it is very useful to be able to set I plan on updating the PRq in the next few days so we can get this thing merged. |
Yo, I just pushed an update on #310 that uses Personally I actually don't mind creating a base class for my app and setting global options there, but if we were to start supporting a more standard way of setting global options I would be in favor of having a config file that If there are more options that would be helpful to set globally, and there is not a current way of doing this I vote for doing that all at once and making it a new PR. |
@zyskowsk thanks for the update, it looks pretty well! I don't mind handling the deprecation path myself, it's quite complex to get warnings to show up in the proper place ;) As for the global option, it would be interesting to have such a mechanism — we already have a custom way to prime However, it's not strictly required for this feature, although a "nice to have" :) So, I'm 👍 on merging this, and including it in the next release — let's plan for "in the next few weeks"? |
Awesome! Thanks @rbarrois, i just fixed a few small things on the branch. Are you going to commit the warning changes to the same branch? I would love to follow along |
@zyskowsk I've added the warnings change; but messed up with github's UI so the changes arrived in zyskowsk#1 ... I also improved the mocking code, based on https://docs.python.org/3/library/unittest.mock.html#auto-speccing. Let me know what you think of it :-) |
@rbarrios, looks great to me, thanks for the improvements, I merged your changes into my branch |
@zyskowsk I've been following this thread because I am experiencing a similar issue: class MyModel(Base):
created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
class MyFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = MyModel
sqlalchemy_session_persistence = "commit"
def my_test():
a = MyFactory()
b = MyFactory()
c = MyFactory()
assert not a.created_at == b.created_at # Fails All those objects will have the same value for |
It seems like the general consensus is that one should only flush the current session during a test case, and roll back on each tear down. Due to the nature of my testing setup it is more convenient to commit instead of flush on create. Have you considered adding a meta flag similar to
force_flush
that would allow the session to be committed?for example:
overriding
SQLAlchemyModelFactory._create
is my current solution. Any suggestions on how to properly have Factories be committed on creation is welcome! Thanks!The text was updated successfully, but these errors were encountered: