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

Issue with unique index - Mongodb #84

Closed
regis-leray opened this issue Oct 1, 2013 · 1 comment
Closed

Issue with unique index - Mongodb #84

regis-leray opened this issue Oct 1, 2013 · 1 comment

Comments

@regis-leray
Copy link

Im using nosqlunit-mongodb-0.7.7, mongodb java driver 2.10.1, with remote mongodb-10gen 2.4.6 con ubuntu

And when im usign this sample

{
"user": {
    "indexes": [
            {
                "index": {"code": 1},
                "options": {"unique" : "true"}
            }
    ],
    "data":[
            {"id":1,"code":"JSON dataset"},
            {"id":2,"code":"Another row"}
    ]
   }
}

Here my unit test

  @Rule
    public MongoDbRule remoteMongoDbRule = new MongoDbRule(mongoDb()
            .databaseName("portfolio_test")
            .host("localhost")
            .port(27017)
            .username("portfolio")
            .password("portfolio")
            .build());

    @Test
    @UsingDataSet(locations = "UserDocument.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
    public void shouldNotCreateDuplicateCodeUser() {

     }

Im getting this stacktrace

java.lang.IllegalArgumentException: Unexpected error reading data set file.
    at com.lordofthejars.nosqlunit.mongodb.MongoOperation.insertData(MongoOperation.java:63)
    at com.lordofthejars.nosqlunit.mongodb.MongoOperation.insert(MongoOperation.java:46)
    at com.lordofthejars.nosqlunit.core.CleanInsertLoadStrategyOperation.executeInsert(CleanInsertLoadStrategyOperation.java:33)
    at com.lordofthejars.nosqlunit.core.CleanInsertLoadStrategyOperation.executeScripts(CleanInsertLoadStrategyOperation.java:27)
    at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.loadDataSet(AbstractNoSqlTestRule.java:273)
    at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.evaluate(AbstractNoSqlTestRule.java:70)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
Caused by: com.mongodb.MongoException: invalid ns to index
    at com.mongodb.CommandResult.getException(CommandResult.java:100)
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
    at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:142)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:183)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
    at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:365)
    at com.mongodb.DBCollection.createIndex(DBCollection.java:484)
    at com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.insertIndexes(DefaultInsertionStrategy.java:84)
    at com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.insertParsedData(DefaultInsertionStrategy.java:53)
    at com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.insert(DefaultInsertionStrategy.java:29)
    at com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.insert(DefaultInsertionStrategy.java:15)
    at com.lordofthejars.nosqlunit.core.AbstractCustomizableDatabaseOperation.executeInsertion(AbstractCustomizableDatabaseOperation.java:23)
    at com.lordofthejars.nosqlunit.mongodb.MongoOperation.insertData(MongoOperation.java:54)
    ... 23 more

PS: if im removing the "unique" options no error.

@regis-leray
Copy link
Author

I changed the code based on the mongo db java documentation

com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.insertIndexes(DefaultInsertionStrategy.java:84)

if(index.containsField(INDEX_OPTIONS)) {
                DBObject indexOptions = (DBObject)index.get(INDEX_OPTIONS);
                indexedCollection.ensureIndex(indexKeys, indexOptions);
            } else {
                indexedCollection.ensureIndex(indexKeys);
            }

http://docs.mongodb.org/manual/tutorial/create-a-unique-index/

And everything is working.

PS we should use ensureIndex fro the default index creation, Based on the documentation and the feeback i read on stackoverflow.

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

2 participants