Commits

David Carr  committed 9d5c0ef

tests: add additional test coverage for AggregateBuilder

  • Participants
  • Parent commits afc16b5

Comments (0)

Files changed (7)

File src/test/java/org/bitbucket/davidm24/mongodb/aggregate/AggregateBuilderIT.java

  * @author David M. Carr
  */
 public class AggregateBuilderIT extends AggregationTestCase {
+    @SuppressWarnings("deprecation")
+    @Test
+    public void aggregateByCollectionNameAndGetTest() {
+        String testName = "aggregateByCollectionNameAndGetTest";
+        DBObject cmdBody = AggregateBuilder.aggregate(testName).group(
+                GroupBuilder.start().put("_id").sum("$val").get()
+                ).get();
+        assertCommandStructureFromResource(
+                testName + ".expected.command.json", cmdBody);
+    }
+
     @Test
     public void articleTagToAuthorTest() {
         String testName = "articleTagToAuthorTest";
         verifyAggregationOutputFromResources(testName, output);
     }
 
-    // TODO: test group with aggregate id
+    @Test
+    public void groupWithMultiFieldIdTest() {
+        String testName = "groupWithMultiFieldIdTest";
+        DBCollection collection = getCollection(testName);
+        insertArticles(collection);
+        AggregationOutput output = AggregateBuilder.aggregate(collection)
+                .group(GroupBuilder.start()
+                        .put("_id")
+                        .to(newMap("author", "$author", "title", "$title"))
+                        .put("docsPerAuthor").sum(1)
+                        .put("viewsPerAuthor").sum("$pageViews")
+                        .get()
+                )
+                .sort(newMap("_id", 1)) // ensure the results are in a
+                                        // predictable order
+                .run();
+        verifyAggregationOutputFromResources(testName, output);
+    }
+
+    @Test
+    public void groupShortcutTest() {
+        String testName = "groupShortcutTest";
+        DBCollection collection = getCollection(testName);
+        insertArticles(collection);
+        AggregationOutput output = AggregateBuilder.aggregate(collection)
+                .group("_id", "$author")
+                .sort(newMap("_id", 1))
+                .run();
+        verifyAggregationOutputFromResources(testName, output);
+    }
+
+    @SuppressWarnings("deprecation")
+    @Test(expectedExceptions = AggregateBuilder.AggregateBuilderException.class,
+            expectedExceptionsMessageRegExp = ".*DBCollection instance.*")
+    public void runWithoutCollectionTest() {
+        AggregateBuilder.aggregate("someCollection").run();
+    }
+
+    @Test(expectedExceptions = AggregateBuilder.AggregateBuilderException.class,
+            expectedExceptionsMessageRegExp = ".*requires a non-empty pipeline.*")
+    public void runWithoutPipelineTest() {
+        String testName = "runWithoutPipelineTest";
+        DBCollection collection = getCollection(testName);
+        AggregateBuilder.aggregate(collection).run();
+    }
 
     @Test
     public void sortTest() {

File src/test/java/org/bitbucket/davidm24/mongodb/aggregate/AggregationTestCase.java

         return new ArrayList<Object>(Arrays.asList(objects));
     }
 
-    private void assertCommandStructureFromResource(String resourceName,
+    protected void assertCommandStructureFromResource(String resourceName,
             DBObject actual) {
         debugPrint("Actual for %s:\n%s", resourceName, actual);
         DBObject expected = (DBObject) readResourceFullyAsJSON(resourceName);
+        if (expected == null) {
+            fail("No expectation in resource " + resourceName);
+        }
         assertEquals(expected.toString(), actual.toString());
     }
 
 
     private void assertResultsMatchFromResource(String resourceName,
             List<DBObject> actual) {
+        debugPrint("Actual for %s:\n%s", resourceName, actual);
         List<DBObject> expected = getResultsFromResource(resourceName);
-        debugPrint("Actual for %s:\n%s", resourceName, actual);
+        if (expected == null) {
+            fail("No expectation in resource " + resourceName);
+        }
         assertEquals(expected.size(), actual.size());
         Comparator<DBObject> comparator = new DBObjectComparator();
         Collections.sort(expected, comparator);
     private String readResourceFully(String resourceName) {
         StringBuilder sb = new StringBuilder();
         InputStream is = getClass().getResourceAsStream(resourceName);
+        if (is == null) {
+            fail("Could not find resource " + resourceName);
+        }
         try {
             BufferedReader reader = new BufferedReader(new InputStreamReader(
                     is, "UTF-8"));

File src/test/resources/org/bitbucket/davidm24/mongodb/aggregate/aggregateByCollectionNameAndGetTest.expected.command.json

+{ "aggregate" : "aggregateByCollectionNameAndGetTest" , "pipeline" : [ { "$group" : { "_id" : { "$sum" : "$val"}}}]}

File src/test/resources/org/bitbucket/davidm24/mongodb/aggregate/groupShortcutTest.expected.command.json

+{ "aggregate" : "groupShortcutTest" , "pipeline" : [ { "$group" : { "_id" : "$author"}} , { "$sort" : { "_id" : 1}}]}

File src/test/resources/org/bitbucket/davidm24/mongodb/aggregate/groupShortcutTest.expected.results.json

+[ { "_id" : "bob"} , { "_id" : "dave"}]

File src/test/resources/org/bitbucket/davidm24/mongodb/aggregate/groupWithMultiFieldIdTest.expected.command.json

+{ "aggregate" : "groupWithMultiFieldIdTest" , "pipeline" : [ { "$group" : { "_id" : { "author" : "$author" , "title" : "$title"} , "docsPerAuthor" : { "$sum" : 1} , "viewsPerAuthor" : { "$sum" : "$pageViews"}}} , { "$sort" : { "_id" : 1}}]}

File src/test/resources/org/bitbucket/davidm24/mongodb/aggregate/groupWithMultiFieldIdTest.expected.results.json

+[ { "_id" : { "author" : "bob" , "title" : "this is my title"} , "docsPerAuthor" : 1 , "viewsPerAuthor" : 5} , { "_id" : { "author" : "bob" , "title" : "today's ramblings"} , "docsPerAuthor" : 1 , "viewsPerAuthor" : 3} , { "_id" : { "author" : "dave" , "title" : "great article"} , "docsPerAuthor" : 1 , "viewsPerAuthor" : 47}]