@Test public void testIn() { coll.save(new MockObject()); String id = coll.findOne().id; assertThat( coll.find().in("_id", id, new org.bson.types.ObjectId().toString()).toArray(), hasSize(1)); }
@Test public void testLessThan() { MockObject o = new MockObject(); o.i = 5; coll.save(o); // Ensure that the serializer actually worked assertThat((Integer) coll.getDbCollection().findOne().get("i"), equalTo(15)); assertThat(coll.find().lessThan("i", 12).toArray(), hasSize(1)); }
@Test public void testArrayEquals() { MockObject o = new MockObject(); MockObject o1 = new MockObject(); o1.id = new org.bson.types.ObjectId().toString(); o.items = Arrays.asList(o1); coll.save(o); assertThat(coll.find().is("items", Arrays.asList(o1)).toArray(), hasSize(1)); }
@Test public void testAll() { MockObject o = new MockObject(); MockObject o1 = new MockObject(); o1.id = new org.bson.types.ObjectId().toString(); o.items = Arrays.asList(o1); coll.save(o); // Ensure that the serializer actually worked assertThat(coll.find().all("items", o1).toArray(), hasSize(1)); }
@Test public void testAnd() { MockObject o = new MockObject(); o.i = 5; coll.save(o); // Ensure that the serializer actually worked assertThat( coll.find().and(DBQuery.lessThan("i", 12), DBQuery.greaterThan("i", 4)).toArray(), hasSize(1)); assertThat( coll.find().and(DBQuery.lessThan("i", 12), DBQuery.greaterThan("i", 9)).toArray(), hasSize(0)); }
@Test public void testRegexPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().regex("string", Pattern.compile("h.llo")); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testSerializationFromInFind() throws Exception { MockObject mockObject = insertMockObjectWithEmbedded(); DBCursor<MockObject> cursor = coll.find(DBQuery.in("object", mockObject.object)); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testNotExistsPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().notExists("integerfun"); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testInCollectionPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().in("integer", Arrays.asList(9, 10, 11)); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testGreaterThanPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().greaterThan("integer", 9); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testOrNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().or(DBQuery.is("integer", 9), DBQuery.lessThan("integer", 9)); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testWherePositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().where("this.integer > 9"); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testLessThanEqPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().lessThanEquals("integer", 10); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
@Test public void testElemMatchNegative() throws Exception { insertMockObjectWithComplexList(); DBCursor<MockObject> cursor = coll.find().elemMatch("complexList", DBQuery.in("value", "foo", "la").size("list", 2)); assertThat(cursor.hasNext(), equalTo(false)); }
@Test(expected = MongoException.class) public void testQueryAfterExecution() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find(); cursor.hasNext(); cursor.in("blah", "blah"); }
@Test public void testAllPositive() throws Exception { MockObject mockObject = insertMockObject(); DBCursor<MockObject> cursor = coll.find().all("simpleList", "a", "b"); assertThat(cursor.hasNext(), equalTo(true)); assertThat(cursor.next(), equalTo(mockObject)); }
private MockObject insertMockObjectWithEmbedded() { MockObject mockObject = new MockObject("someid", "hello", 10); MockEmbeddedObject embeddedObject = new MockEmbeddedObject(); embeddedObject.value = "hello"; embeddedObject.list = Arrays.asList("a", "b", "c"); mockObject.object = embeddedObject; coll.insert(mockObject); return mockObject; }
@Test public void testNorNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find() .and( DBQuery.lessThan("integer", 9), DBQuery.is("string", "hello"), DBQuery.greaterThan("integer", 11)); assertThat(cursor.hasNext(), equalTo(false)); }
private MockObject insertMockObjectWithComplexList() { MockObject mockObject = new MockObject("someid", "hello", 10); MockEmbeddedObject embeddedObject1 = new MockEmbeddedObject(); embeddedObject1.value = "foo"; embeddedObject1.list = Arrays.asList("a", "b", "c"); MockEmbeddedObject embeddedObject2 = new MockEmbeddedObject(); embeddedObject2.value = "bar"; embeddedObject2.list = Arrays.asList("d", "e"); mockObject.complexList = Arrays.asList(embeddedObject1, embeddedObject2); coll.insert(mockObject); return mockObject; }
com.mongodb.MapReduceCommand build(JacksonDBCollection<?, ?> collection) { DBObject query = null; if (this.query != null) { query = collection.serializeQuery(this.query); } com.mongodb.MapReduceCommand command = new com.mongodb.MapReduceCommand( collection.getDbCollection(), map, reduce, this.collection, outputType.getDriverType(), query); if (finalize != null) { command.setFinalize(finalize); } if (readPreference != null) { command.setReadPreference(readPreference); } if (outputDB != null) { command.setOutputDB(outputDB); } if (sort != null) { command.setSort(sort); } command.setLimit(limit); if (scope != null) { command.setScope(scope); } command.setVerbose(verbose); if (extra != null) { for (String key : extra.keySet()) { command.addExtraOption(key, extra.get(key)); } } return command; }
@Test public void testLessThanNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().lessThan("integer", 10); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testSizeNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().size("simpleList", 4); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testSimpleEquals() { coll.save(new MockObject()); String id = coll.findOne().id; assertNotNull(coll.findOne(DBQuery.is("_id", id))); }
@Test public void testGreaterThanEqNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().greaterThanEquals("integer", 11); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testNotExistsNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().notExists("integer"); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testRegexNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().regex("string", Pattern.compile("hllo")); assertThat(cursor.hasNext(), equalTo(false)); }
private MockObject insertMockObject() { MockObject mockObject = new MockObject("someid", "hello", 10); mockObject.simpleList = Arrays.asList("a", "b", "c"); coll.insert(mockObject); return mockObject; }
@Test public void testAllNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().all("simpleList", "a", "banana", "b"); assertThat(cursor.hasNext(), equalTo(false)); }
@Test public void testWhereNegative() throws Exception { insertMockObject(); DBCursor<MockObject> cursor = coll.find().where("this.integer < 9"); assertThat(cursor.hasNext(), equalTo(false)); }