| 48 | } |
| 49 | |
| 50 | func TestInvalidQuery(t *testing.T) { |
| 51 | ctx := context.Background() |
| 52 | // We detect that these queries are invalid before they reach the driver. |
| 53 | c := &Collection{} |
| 54 | |
| 55 | for _, test := range []struct { |
| 56 | desc string |
| 57 | appliesToGet bool |
| 58 | q *Query |
| 59 | contains string // error text must contain this string |
| 60 | }{ |
| 61 | {"negative Limit", true, c.Query().Limit(-1), "limit"}, |
| 62 | {"zero Limit", true, c.Query().Limit(0), "limit"}, |
| 63 | {"two Limits", true, c.Query().Limit(1).Limit(2), "limit"}, |
| 64 | {"negative Offset", true, c.Query().Offset(-1), "offset"}, |
| 65 | {"two Offsets", true, c.Query().Offset(1).Offset(2), "offset"}, |
| 66 | {"zero Offset and Limit", true, c.Query().Offset(0).Limit(0), "limit"}, |
| 67 | {"negative Offset and Limit", true, c.Query().Offset(-1).Limit(-1), "offset"}, |
| 68 | {"zero Offset and negative Limit", true, c.Query().Offset(0).Limit(-1), "limit"}, |
| 69 | {"empty OrderBy field", true, c.Query().OrderBy("", Ascending), "empty field"}, |
| 70 | {"bad OrderBy direction", true, c.Query().OrderBy("x", "y"), "direction"}, |
| 71 | {"two OrderBys", true, c.Query().OrderBy("x", Ascending).OrderBy("y", Descending), "orderby"}, |
| 72 | {"OrderBy not in Where", true, c.Query().OrderBy("x", Ascending).Where("y", ">", 1), "orderby"}, |
| 73 | {"any Limit", false, c.Query().Limit(1), "limit"}, |
| 74 | {"any Offset", false, c.Query().Offset(1), "offset"}, |
| 75 | {"any OrderBy", false, c.Query().OrderBy("x", Descending), "orderby"}, |
| 76 | } { |
| 77 | check := func(err error) { |
| 78 | if gcerrors.Code(err) != gcerrors.InvalidArgument { |
| 79 | t.Errorf("%s: got %v, want InvalidArgument", test.desc, err) |
| 80 | return |
| 81 | } |
| 82 | if !strings.Contains(strings.ToLower(err.Error()), test.contains) { |
| 83 | t.Errorf("%s: got %q, wanted it to contain %q", test.desc, err.Error(), test.contains) |
| 84 | } |
| 85 | } |
| 86 | if test.appliesToGet { |
| 87 | check(test.q.Get(ctx).Next(ctx, nil)) |
| 88 | } |
| 89 | } |
| 90 | } |