Skip to content
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

Add proper return value to MockSet delete #184

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

nfantone
Copy link
Contributor

@nfantone nfantone commented Sep 6, 2024

This is a continuation of #154 which seems to have gone stale.

@nfantone nfantone force-pushed the delete-return branch 5 times, most recently from 49fbfd5 to b2dc8d3 Compare September 9, 2024 11:33
@nfantone
Copy link
Contributor Author

nfantone commented Sep 9, 2024

@stefan6419846 You think we could merge this as well, if there's any interest?

And also, after/if we do, could we consider releasing a new version of the library? Would be very useful to make recent additions available to everyone.

for item in matches(*items_to_remove, **attrs):
self.items.remove(item)
self.fire(item, self.EVENT_DELETED)
item_label = item._meta.label if hasattr(item, '_meta') else type(item).__name__
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to come up with this alternative labelling for, essentially, non-MockModel items because I noticed some unit tests were declaring MockSet out of native values.

Example:

items = [1, 2, 3]
assert MockSet(*items).count() == len(items)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken, we do not have a test for the return value in this case? Could you please add one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it, but I'm not too sure of its value. Is this something we should support even?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrating to a label-only approach, id est avoiding the fallback, might be a breaking change if I understand it correctly. But I am unsure about the actual value used here - would None be enough or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be a breaking change if I understand it correctly

Technically, yes.

But the intended use case is not really documented anywhere. I only came to the realisation that this was a problem after I saw a MockSet of int in the tests. IRL, this is not something Django supports. In fact if you try to create a QuerySet of anything that doesn't have a _meta attribute, it blows up:

from django.db.models import QuerySet

QuerySet(1)
# AttributeError: 'int' object has no attribute '_meta'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I've added the relevant unit tests and kept the fallback, just for consistency's sake. This can be addressed later, if needed.

@nfantone nfantone force-pushed the delete-return branch 2 times, most recently from d7cf759 to 1233813 Compare September 9, 2024 12:39
@stefan6419846 stefan6419846 merged commit 8736200 into stphivos:master Sep 9, 2024
14 checks passed
@nfantone nfantone deleted the delete-return branch September 9, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants