Skip to content

DATAMONGO-1404 DATAMONGO-1412 Add support for $min and $max update operators. #359

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.core.query.Update.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.bson.types.ObjectId;
Expand Down Expand Up @@ -94,14 +97,15 @@

/**
* Integration test for {@link MongoTemplate}.
*
*
* @author Oliver Gierke
* @author Thomas Risberg
* @author Amol Nayak
* @author Patryk Wasik
* @author Thomas Darimont
* @author Komi Innocent
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
Expand Down Expand Up @@ -3164,6 +3168,194 @@ public void updateShouldWorkForTypesContainingGeoJsonTypes() {
assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point)));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesDateValueCorrectlyWhenUsingMinOperator() {

Calendar cal = Calendar.getInstance(Locale.US);
cal.set(2013, 10, 13, 0, 0, 0);

TypeWithDate twd = new TypeWithDate();
twd.date = new Date();
template.save(twd);
template.updateFirst(query(where("id").is(twd.id)), new Update().min("date", cal.getTime()), TypeWithDate.class);

TypeWithDate loaded = template.find(query(where("id").is(twd.id)), TypeWithDate.class).get(0);
assertThat(loaded.date, equalTo(cal.getTime()));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesNumericValueCorrectlyWhenUsingMinOperator() {

TypeWithNumbers twn = new TypeWithNumbers();
twn.byteVal = 100;
twn.doubleVal = 200D;
twn.floatVal = 300F;
twn.intVal = 400;
twn.longVal = 500L;

// Note that $min operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
twn.bigIntegerVal = new BigInteger("600");
twn.bigDeciamVal = new BigDecimal("700.0");

template.save(twn);

byte byteVal = 90;
Update update = new Update()//
.min("byteVal", byteVal) //
.min("doubleVal", 190D) //
.min("floatVal", 290F) //
.min("intVal", 390) //
.min("longVal", 490) //
.min("bigIntegerVal", new BigInteger("590")) //
.min("bigDeciamVal", new BigDecimal("690")) //
;

template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);

TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
assertThat(loaded.byteVal, equalTo(byteVal));
assertThat(loaded.doubleVal, equalTo(190D));
assertThat(loaded.floatVal, equalTo(290F));
assertThat(loaded.intVal, equalTo(390));
assertThat(loaded.longVal, equalTo(490L));
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("590")));
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("690")));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesDateValueCorrectlyWhenUsingMaxOperator() {

Calendar cal = Calendar.getInstance(Locale.US);
cal.set(2013, 10, 13, 0, 0, 0);

TypeWithDate twd = new TypeWithDate();
twd.date = cal.getTime();
template.save(twd);

cal.set(2019, 10, 13, 0, 0, 0);
template.updateFirst(query(where("id").is(twd.id)), new Update().max("date", cal.getTime()), TypeWithDate.class);

TypeWithDate loaded = template.find(query(where("id").is(twd.id)), TypeWithDate.class).get(0);
assertThat(loaded.date, equalTo(cal.getTime()));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesNumericValueCorrectlyWhenUsingMaxOperator() {

TypeWithNumbers twn = new TypeWithNumbers();
twn.byteVal = 100;
twn.doubleVal = 200D;
twn.floatVal = 300F;
twn.intVal = 400;
twn.longVal = 500L;

// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
twn.bigIntegerVal = new BigInteger("600");
twn.bigDeciamVal = new BigDecimal("700.0");

template.save(twn);

byte byteVal = 101;
Update update = new Update()//
.max("byteVal", byteVal) //
.max("doubleVal", 290D) //
.max("floatVal", 390F) //
.max("intVal", 490) //
.max("longVal", 590) //
.max("bigIntegerVal", new BigInteger("690")) //
.max("bigDeciamVal", new BigDecimal("790")) //
;

template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);

TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
assertThat(loaded.byteVal, equalTo(byteVal));
assertThat(loaded.doubleVal, equalTo(290D));
assertThat(loaded.floatVal, equalTo(390F));
assertThat(loaded.intVal, equalTo(490));
assertThat(loaded.longVal, equalTo(590L));
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("690")));
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("790")));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesBigNumberValueUsingStringComparisonWhenUsingMaxOperator() {

TypeWithNumbers twn = new TypeWithNumbers();

// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
// Therefore "80" is considered greater than "700"
twn.bigIntegerVal = new BigInteger("600");
twn.bigDeciamVal = new BigDecimal("700.0");

template.save(twn);

Update update = new Update()//
.max("bigIntegerVal", new BigInteger("70")) //
.max("bigDeciamVal", new BigDecimal("80")) //
;

template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);

TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("70")));
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("80")));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void updatesBigNumberValueUsingStringComparisonWhenUsingMinOperator() {

TypeWithNumbers twn = new TypeWithNumbers();

// Note that $max operator uses String comparison for BigDecimal/BigInteger comparison according to BSON sort rules.
// Therefore "80" is considered greater than "700"
twn.bigIntegerVal = new BigInteger("80");
twn.bigDeciamVal = new BigDecimal("90.0");

template.save(twn);

Update update = new Update()//
.min("bigIntegerVal", new BigInteger("700")) //
.min("bigDeciamVal", new BigDecimal("800")) //
;

template.updateFirst(query(where("id").is(twn.id)), update, TypeWithNumbers.class);

TypeWithNumbers loaded = template.find(query(where("id").is(twn.id)), TypeWithNumbers.class).get(0);
assertThat(loaded.bigIntegerVal, equalTo(new BigInteger("700")));
assertThat(loaded.bigDeciamVal, equalTo(new BigDecimal("800")));
}

static class TypeWithNumbers {

@Id String id;
Integer intVal;
Float floatVal;
Long longVal;
Double doubleVal;
BigDecimal bigDeciamVal;
BigInteger bigIntegerVal;
Byte byteVal;
}

static class DoucmentWithNamedIdField {

@Id String someIdKey;
Expand Down Expand Up @@ -3215,11 +3407,11 @@ static class DocumentWithDBRefCollection {

@Id public String id;

@Field("db_ref_list")/** @see DATAMONGO-1058 */
@org.springframework.data.mongodb.core.mapping.DBRef//
@Field("db_ref_list") /** @see DATAMONGO-1058 */
@org.springframework.data.mongodb.core.mapping.DBRef //
public List<Sample> dbRefAnnotatedList;

@org.springframework.data.mongodb.core.mapping.DBRef//
@org.springframework.data.mongodb.core.mapping.DBRef //
public Sample dbRefProperty;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,10 +63,11 @@

/**
* Unit tests for {@link UpdateMapper}.
*
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Thomas Darimont
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class UpdateMapperUnitTests {
Expand Down Expand Up @@ -687,10 +688,8 @@ public void mappingShouldRetainTypeInformationOfNestedListWhenUpdatingConcreteyP
context.getPersistentEntity(DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes.class));

assertThat(mappedUpdate, isBsonObject().notContaining("$set.concreteTypeWithListAttributeOfInterfaceType._class"));
assertThat(
mappedUpdate,
isBsonObject().containing("$set.concreteTypeWithListAttributeOfInterfaceType.models.[0]._class",
ModelImpl.class.getName()));
assertThat(mappedUpdate, isBsonObject()
.containing("$set.concreteTypeWithListAttributeOfInterfaceType.models.[0]._class", ModelImpl.class.getName()));
}

/**
Expand Down Expand Up @@ -757,8 +756,8 @@ public void mappingShouldRetrainTypeInformationWhenValueTypeOfMapDoesNotMatchIts
@Test
public void mappingShouldNotContainTypeInformationWhenValueTypeOfMapMatchesDeclaration() {

Map<Object, NestedDocument> map = Collections.<Object, NestedDocument> singletonMap("jasnah", new NestedDocument(
"kholin"));
Map<Object, NestedDocument> map = Collections.<Object, NestedDocument> singletonMap("jasnah",
new NestedDocument("kholin"));

Update update = new Update().set("concreteMap", map);
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
Expand Down Expand Up @@ -887,6 +886,32 @@ public void mapsAtomicIntegerToPrimitiveIntegerCorrectly() {
assertThat($set.get("primIntValue"), Is.<Object> is(10));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void mapsMinCorrectly() {

Update update = new Update().min("minfield", 10);
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(SimpleValueHolder.class));

assertThat(mappedUpdate, isBsonObject().containing("$min", new BasicDBObject("minfield", 10)));
}

/**
* @see DATAMONGO-1404
*/
@Test
public void mapsMaxCorrectly() {

Update update = new Update().max("maxfield", 999);
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(SimpleValueHolder.class));

assertThat(mappedUpdate, isBsonObject().containing("$max", new BasicDBObject("maxfield", 999)));
}

static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}
Expand Down
Loading