Search in sources :

Example 1 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class ExecutableMapReduceOperationSupportUnitTests method usesMapReduceOptionsWhenPresent.

// DATAMONGO-1929
@Test
void usesMapReduceOptionsWhenPresent() {
    when(template.getCollectionName(eq(Person.class))).thenReturn(STAR_WARS);
    MapReduceOptions options = MapReduceOptions.options();
    mapReduceOpsSupport.mapReduce(Person.class).map(MAP_FUNCTION).reduce(REDUCE_FUNCTION).with(options).all();
    verify(template).mapReduce(any(Query.class), eq(Person.class), eq(STAR_WARS), eq(MAP_FUNCTION), eq(REDUCE_FUNCTION), eq(options), eq(Person.class));
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 2 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method mapReduceShouldPickUpLimitFromOptions.

// DATAMONGO-1334
@Test
void mapReduceShouldPickUpLimitFromOptions() {
    MongoCursor cursor = mock(MongoCursor.class);
    MapReduceIterable output = mock(MapReduceIterable.class);
    when(output.limit(anyInt())).thenReturn(output);
    when(output.sort(any())).thenReturn(output);
    when(output.filter(any(Document.class))).thenReturn(output);
    when(output.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(collection.mapReduce(anyString(), anyString(), eq(Document.class))).thenReturn(output);
    Query query = new BasicQuery("{'foo':'bar'}");
    template.mapReduce(query, "collection", "function(){}", "function(key,values){}", new MapReduceOptions().limit(1000), Wrapper.class);
    verify(output, times(1)).limit(1000);
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MapReduceIterable(com.mongodb.client.MapReduceIterable) MongoCursor(com.mongodb.client.MongoCursor) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 3 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MapReduceTests method testMapReduceInlineWithScope.

// DATADOC-7
@Test
public void testMapReduceInlineWithScope() {
    createMapReduceData();
    Map<String, Object> scopeVariables = new HashMap<String, Object>();
    scopeVariables.put("exclude", "a");
    String mapWithExcludeFunction = "function(){ for ( var i=0; i<this.x.length; i++ ){ if(this.x[i] != exclude) emit( this.x[i] , 1 ); } }";
    MapReduceResults<ValueObject> results = mongoTemplate.mapReduce("jmr1", mapWithExcludeFunction, REDUCE_FUNCTION, new MapReduceOptions().scopeVariables(scopeVariables).outputTypeInline(), ValueObject.class);
    // 
    assertThat(copyToMap(results)).hasSize(// 
    3).containsEntry("b", // 
    2F).containsEntry("c", // 
    2F).containsEntry("d", 1F);
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferently.

// DATAMONGO-1334
@Test
void mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferently() {
    MongoCursor cursor = mock(MongoCursor.class);
    MapReduceIterable output = mock(MapReduceIterable.class);
    when(output.limit(anyInt())).thenReturn(output);
    when(output.sort(any())).thenReturn(output);
    when(output.filter(any(Document.class))).thenReturn(output);
    when(output.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(collection.mapReduce(anyString(), anyString(), eq(Document.class))).thenReturn(output);
    Query query = new BasicQuery("{'foo':'bar'}");
    query.limit(100);
    template.mapReduce(query, "collection", "function(){}", "function(key,values){}", new MapReduceOptions().limit(1000), Wrapper.class);
    verify(output, times(1)).limit(1000);
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MapReduceIterable(com.mongodb.client.MapReduceIterable) MongoCursor(com.mongodb.client.MongoCursor) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 5 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method mapReduceShouldPickUpLimitFromOptionsWhenQueryIsNotPresent.

// DATAMONGO-1334
@Test
void mapReduceShouldPickUpLimitFromOptionsWhenQueryIsNotPresent() {
    MongoCursor cursor = mock(MongoCursor.class);
    MapReduceIterable output = mock(MapReduceIterable.class);
    when(output.limit(anyInt())).thenReturn(output);
    when(output.sort(any())).thenReturn(output);
    when(output.filter(any())).thenReturn(output);
    when(output.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(collection.mapReduce(anyString(), anyString(), eq(Document.class))).thenReturn(output);
    template.mapReduce("collection", "function(){}", "function(key,values){}", new MapReduceOptions().limit(1000), Wrapper.class);
    verify(output, times(1)).limit(1000);
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) MapReduceIterable(com.mongodb.client.MapReduceIterable) MongoCursor(com.mongodb.client.MongoCursor) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Aggregations

MapReduceOptions (org.springframework.data.mongodb.core.mapreduce.MapReduceOptions)7 Test (org.junit.jupiter.api.Test)5 Query (org.springframework.data.mongodb.core.query.Query)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)4 MapReduceIterable (com.mongodb.client.MapReduceIterable)3 MongoCursor (com.mongodb.client.MongoCursor)3 Document (org.bson.Document)3 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1