Liam Staskawicz avatar Liam Staskawicz committed 4b421ac

* fix collection group test

Comments (0)

Files changed (2)

fan/Collection.fan

   
   **
   ** 'key' - Str is key name, 
-  ** 
   **
   ** See http://www.mongodb.org/display/DOCS/Aggregation
   **
   ** TODO - support $keyf option
   **
-  [Str:Obj?][] group(Str[] keys, Str:Obj? initial, Str reduce, Str:Obj? query := [:], Str? finalize := null)
+  [Str:Obj?][] group(Str[] keys, Str:Obj? initial, Code reduce, Str:Obj? query := [:], Str? finalize := null)
   {
     Str:Bool keymap := [:]
     keys.each |s| { keymap[s] = true }
     if (finalize != null) args["finalize"] = finalize
     
     res := db.command(["group": args])
-    if(!DB.cmdOk(res)) throw MongoOpErr("group failed - ${res}")
+    if (!DB.cmdOk(res)) throw MongoOpErr("group failed - ${res}")
     return res["retval"]
   }
   
   }
 
 }
-
-
-

test/CollectionTest.fan

     c := db["grouptest"]
     c.drop
     
-    c.save(["a": 1])
-    c.save(["b": 1])
-    initial := ["count": 0]
+    // save items to the coll
+    c.save(["x":"a"])
+    c.save(["x":"a"])
+    c.save(["x":"a"])
+    c.save(["x":"b"])
+    initial := ["count": 0.0]
+    f := "function (obj, prev) { prev.count += inc_value; }"
     
-    verifyEq(1f, c.group(Str[,], initial, "function (obj, prev) { prev.count += 0.5; }")[0]["count"])
-    verifyEq(2f, c.group(Str[,], initial, "function (obj, prev) { prev.count += 1; }")[0]["count"])
-    verifyEq(4f, c.group(Str[,], initial, "function (obj, prev) { prev.count += 2; }")[0]["count"])
+    g := c.group(["x"], initial, Code(f, ["inc_value":1]))
+    verifyEq(3f, g[0]["count"])
+    
+    g2 := c.group(["x"], initial, Code(f, ["inc_value":0.5]))
+    verifyEq(1.5f, g2[0]["count"])
     
     // with finalize
     fin := "function(doc) {doc.f = doc.count + 200; }"
-    verifyEq(202f, c.group(Str[,], initial, "function (obj, prev) { prev.count += 1; }", [:], fin)[0]["f"])
+    g3 := c.group(Str[,], initial, Code(f, ["inc_value":1]), [:], fin)
+    verifyEq(204f, g3[0]["f"])
     
     c.drop
   }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.