func TestTextAggregate(t *testing.T) { fm, subs, db := setupAgg(t) defer teardownAgg(t, db) t.Log("Given the need to aggregate text.") { t.Log("\tWhen starting from a form and submission fixtures") { //---------------------------------------------------------------------- // Aggregate the submissions. aggs, err := form.TextAggregate(tests.Context, db, fm.ID.Hex(), subs) if err != nil { t.Fatalf("\t%s\tShould be able to aggregate submissions : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to aggregate submissions.", tests.Success) //---------------------------------------------------------------------- // Check the aggregations. if len(aggs) != 10 { t.Fatalf("\t%s\tShould be able to get 11 aggregations : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to get 11 aggregations.", tests.Success) } } }
// SubmissionGroup retrieves submissions on a form by search criteria, // groups them then returns text aggregations. // 200 Success, 400 Bad Request, 404 Not Found, 500 Internal func (aggregationHandle) SubmissionGroup(c *web.Context) error { id := c.Params["form_id"] // Unpack the search headers and create a SearchOpts for Group Submissions. limit, err := strconv.Atoi(c.Request.URL.Query().Get("limit")) if err != nil { limit = 0 } skip, err := strconv.Atoi(c.Request.URL.Query().Get("skip")) if err != nil { skip = 0 } opts := submission.SearchOpts{ Query: c.Request.URL.Query().Get("search"), FilterBy: c.Request.URL.Query().Get("filterby"), } if c.Request.URL.Query().Get("orderby") == "dsc" { opts.DscOrder = true } groups, err := form.GroupSubmissions(c.SessionID, c.Ctx["DB"].(*db.DB), id, limit, skip, opts) if err != nil { return err } groupKey, ok := c.Params["group_id"] if !ok { c.Respond(nil, http.StatusNotFound) } ta := []form.TextAggregation{} for group, submissions := range groups { if group.ID == groupKey { ta, err = form.TextAggregate(c.SessionID, c.Ctx["DB"].(*db.DB), id, submissions) if err != nil { return err } } } c.Respond(ta, http.StatusOK) return nil }