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

Support duplicate validation on index: { unique: true} } #58

Closed
ydaniv opened this issue Apr 27, 2014 · 11 comments
Closed

Support duplicate validation on index: { unique: true} } #58

ydaniv opened this issue Apr 27, 2014 · 11 comments

Comments

@ydaniv
Copy link

ydaniv commented Apr 27, 2014

For now Mockgoose requires setting unique: true explicitly on the field.
mongod will throw 11000 error for index: { unique: true} } but Mockgoose ignores it and test relying on this error occurring will fail.

@ydaniv
Copy link
Author

ydaniv commented Apr 27, 2014

Managed to hack this with the following:

my_model.schema.path('field').options.unique = true;

@mccormicka
Copy link
Owner

@ydaniv can you supply an example?

mccormicka added a commit that referenced this issue May 3, 2014
@mccormicka
Copy link
Owner

@ydaniv added a test for this
c8ecfbb

Is that what you meant?

@ydaniv
Copy link
Author

ydaniv commented May 4, 2014

@mccormicka thanks for the response!

I think it's not quite what I meant.
What I expect is that mockgoose will throw a "duplicate key" validation error for a Schema with a field that has index: { unique: true }, which implicitly also means (if I'm not mistaken) that that field will also become unique: true.

In any case, mongod does raise a "duplicate key" error, so I expect it to also be supported for consistency.

I think currently it's not supported in mockgoose, so it requires also extra implementation.

Would you like me to try and tackle this?

@mccormicka
Copy link
Owner

Hi @ydaniv can you add a test case? Then I would be able to understand the issue a bit better.

Thanks

ydaniv added a commit to ydaniv/Mockgoose that referenced this issue May 4, 2014
mccormicka added a commit that referenced this issue May 4, 2014
@mccormicka
Copy link
Owner

@ydaniv I merged your branch but these tests also fail when I run them against a real MongoDB instance. Do you see the same if you run it against mongodb without mockgoose?

@ydaniv
Copy link
Author

ydaniv commented May 5, 2014

@mccormicka yes, for me they also fail. The error I see in the mongod console is not propagated for some reason through Mongoose. Did you you notice that error in the console?
However, in a plain Mongoose-over-MongoDB app I do get an error.
Something in the config here is different but I can't say what it is.

@nivthefox
Copy link

Just as a note, compound uniques are also not being caught by mockgoose.

Example:
MySchema.index({name: 1, server: 1}, {unique: true});

I can create two instances of MySchema model with the same name/server property.

@jaseeey
Copy link

jaseeey commented Nov 9, 2015

Has anyone had any luck with this issue? I'm finding that my unique indexes are being ignored when performing an update() using Mockgoose. The only way around this is using a save() which does throw a duplicate key error, however update() simply updates and allows a duplicate to exist whilst the actual MongoDB throws an error.

@winfinit
Copy link
Collaborator

5.0 honors indexes

@jaseeey
Copy link

jaseeey commented Nov 22, 2015

Thank you for following this up, has this been applied to the latest 5.0rc?

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

No branches or pull requests

5 participants