aggregates_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package executor
  2. import "testing"
  3. func TestGroupConcatAggregate(t *testing.T) {
  4. _, schema, table := newTestDB(t)
  5. e := newExec(schema, table)
  6. execMust(t, e, "CREATE TABLE t (id INTEGER, name TEXT)")
  7. execMust(t, e, "INSERT INTO t VALUES (1, 'a')")
  8. execMust(t, e, "INSERT INTO t VALUES (2, 'b')")
  9. execMust(t, e, "INSERT INTO t VALUES (3, NULL)")
  10. execMust(t, e, "INSERT INTO t VALUES (4, 'a')")
  11. tests := []struct {
  12. sql string
  13. want interface{}
  14. }{
  15. {"SELECT GROUP_CONCAT(name) FROM t", "a,b,a"},
  16. {"SELECT GROUP_CONCAT(name, '|') FROM t", "a|b|a"},
  17. {"SELECT GROUP_CONCAT(DISTINCT name) FROM t", "a,b"},
  18. {"SELECT GROUP_CONCAT(id) FROM t", "1,2,3,4"},
  19. {"SELECT COALESCE(GROUP_CONCAT(name), 'empty') FROM t WHERE id = 99", "empty"},
  20. {"SELECT GROUP_CONCAT(name) FROM t WHERE id = 99", nil},
  21. }
  22. for _, tc := range tests {
  23. res := execMust(t, e, tc.sql)
  24. if len(res.Rows) != 1 || len(res.Rows[0]) != 1 {
  25. t.Fatalf("%s returned %#v, want one row with one column", tc.sql, res.Rows)
  26. }
  27. if res.Rows[0][0] != tc.want {
  28. t.Fatalf("%s = %#v, want %#v", tc.sql, res.Rows[0][0], tc.want)
  29. }
  30. }
  31. }
  32. func TestTotalAggregate(t *testing.T) {
  33. _, schema, table := newTestDB(t)
  34. e := newExec(schema, table)
  35. execMust(t, e, "CREATE TABLE t (n INTEGER)")
  36. execMust(t, e, "INSERT INTO t VALUES (1)")
  37. execMust(t, e, "INSERT INTO t VALUES (2)")
  38. execMust(t, e, "INSERT INTO t VALUES (NULL)")
  39. res := execMust(t, e, "SELECT TOTAL(n) FROM t")
  40. if len(res.Rows) != 1 || res.Rows[0][0] != float64(3) {
  41. t.Fatalf("TOTAL(n) = %#v, want 3.0", res.Rows)
  42. }
  43. res = execMust(t, e, "SELECT TOTAL(n) FROM t WHERE n > 99")
  44. if len(res.Rows) != 1 || res.Rows[0][0] != float64(0) {
  45. t.Fatalf("empty TOTAL(n) = %#v, want 0.0", res.Rows)
  46. }
  47. }