fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 155 forks source link

Aggregated count is not working when using nested objects #258

Open tvk opened 7 years ago

tvk commented 7 years ago

Hello,

we use fongo together with nosqlunit-mongodb and spring-data and found a problem. Please see the attached junit test.

We group by firstName and address, the grouping works correct, only the counting is wrong. When using the same logic on a real mongodb, the counting works. Also we found out that this only happens when we're using embedded objects. When firstName and address are part of the person class, the counting works as expected.

Thanks for your help and best regards, Thomas

import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.mapping.Document;

import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb;
import com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder;
import com.lordofthejars.nosqlunit.mongodb.MongoDbRule;
import com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder;
import com.mongodb.BasicDBObject;

public class NosqlUnitTest {

  public static class UserData {

    private String firstName;
    private String lastName;
    private String address;

    public UserData(String firstName, String lastName, String address) {
      this.firstName = firstName;
      this.lastName = lastName;
      this.address = address;
    }

    public String getFirstName() {
      return firstName;
    }

    public void setFirstName(String firstName) {
      this.firstName = firstName;
    }

    public String getLastName() {
      return lastName;
    }

    public void setLastName(String lastName) {
      this.lastName = lastName;
    }

    public void setAddress(String address) {
      this.address = address;
    }

    public String getAddress() {
      return address;
    }
  }

  @Document(collection = "test_db_collection")
  public static class Person {

    @Id
    private String id;
    private UserData userData;

    public Person(String firstName, String lastName, String address) {
      this.userData = new UserData(firstName, lastName, address);
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getId() {
      return id;
    }

    public UserData getUserData() {
      return userData;
    }
  }

  @ClassRule
  public static InMemoryMongoDb inMemoryMongoDb = InMemoryMongoRuleBuilder.newInMemoryMongoDbRule().build();

  @Rule
  public MongoDbRule embeddedMongoDbRule = MongoDbRuleBuilder.newMongoDbRule().defaultEmbeddedMongoDb("test_db");

  @Test
  public void test() {

    final TypedAggregation<Person> agg = TypedAggregation.newAggregation(Person.class,
        Aggregation.group("userData.firstName", "userData.address").count().as("count"));

    final MongoTemplate mongoTemplate = new MongoTemplate(
        embeddedMongoDbRule.getDatabaseOperation().connectionManager(), "test_db");

    mongoTemplate.insert(new Person("Bill", "Gates", "California"));
    mongoTemplate.insert(new Person("Bill", "Clinton", "Washington"));
    mongoTemplate.insert(new Person("Bill", "Smith", "California"));
    mongoTemplate.insert(new Person("Steve", "Jobs", "California"));

    mongoTemplate.aggregate(agg, BasicDBObject.class).getMappedResults().stream().forEach(
        object -> Assert.assertTrue(object.getInt("count") > 0));

    /*
     * Fails! Result was:
     * [
     * { "firstName" : "Steve" , "address" : "California" , "count" : 0},
     * { "firstName" : "Bill" , "address" : "California" , "count" : 0},
     * { "firstName" : "Bill" , "address" : "Washington" , "count" : 0}
     * ]
     */
  }
}
lukas-krecan commented 7 years ago

Simplified test to reproduce the same issue https://github.com/lukas-krecan/fongo/commit/f3fa9a995527a6501b34ddad63ab4371acb54af7

lukas-krecan commented 7 years ago

Should get fixed by https://github.com/fakemongo/fongo/pull/277

karlitos commented 7 years ago

Hello,

it appears to me, that i am affected by this bug. I'd like to ask when I should be expecting release of the fix for this issue.