github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/base/params/list_test.go (about) 1 package params 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/google/go-cmp/cmp" 10 ) 11 12 func TestListParamsFromRequest(t *testing.T) { 13 // should work for nil list params & request with no list information 14 r, err := http.NewRequest("POST", "/test", nil) 15 if err != nil { 16 t.Fatal(err) 17 } 18 19 // should not error if there is an already populated params.List and no list 20 // information in the request 21 expect := List{Offset: 10} 22 got := List{Offset: 10} 23 err = got.ListParamsFromRequest(r) 24 if err != nil { 25 t.Fatalf("ListParamsFromRequest should not error if there is no list information in the request: %s", err) 26 } 27 if diff := cmp.Diff(expect, got); diff != "" { 28 t.Errorf("params.List mismatch (-want,+got):\n%s", diff) 29 } 30 31 // add list queries to the request 32 q := r.URL.Query() 33 q.Add("offset", "10") 34 q.Add("limit", "10") 35 q.Add("filter", "username:peer") 36 q.Add("orderby", "+name,-updated") 37 r.URL.RawQuery = q.Encode() 38 39 got = List{} 40 expect = List{ 41 Offset: 10, 42 Limit: 10, 43 Filter: []string{"username:peer"}, 44 OrderBy: OrderBy{{Key: "name", Direction: OrderASC}, {Key: "updated", Direction: OrderDESC}}, 45 } 46 err = got.ListParamsFromRequest(r) 47 if err != nil { 48 t.Fatalf("ListParamsFromRequest unexpected error: %s", err) 49 } 50 if diff := cmp.Diff(expect, got); diff != "" { 51 t.Errorf("params.List mismatch (-want,+got):\n%s", diff) 52 } 53 54 // should error if list params are not empty & there is list info in the request 55 got = List{Filter: []string{"name:test"}} 56 err = got.ListParamsFromRequest(r) 57 if !errors.Is(err, ErrListParamsNotEmpty) { 58 t.Errorf("expected error to be %q, got %s", ErrListParamsNotEmpty, err) 59 } 60 } 61 62 func TestIsEmpty(t *testing.T) { 63 cases := []struct { 64 description string 65 lp List 66 expect bool 67 }{ 68 {"empty list params", List{}, true}, 69 {"each field empty", List{Offset: 0, Limit: 0, Filter: []string{}, OrderBy: OrderBy{}}, true}, 70 {"has offset", List{Offset: 1}, false}, 71 {"has limit", List{Limit: 1}, false}, 72 {"has filter", List{Filter: []string{"public:true"}}, false}, 73 {"has orderby", List{OrderBy: OrderBy{{Key: "", Direction: ""}}}, false}, 74 } 75 for _, c := range cases { 76 got := c.lp.IsEmpty() 77 if got != c.expect { 78 t.Errorf("error in case %q, expected %t, got %t", c.description, c.expect, got) 79 } 80 } 81 } 82 83 func TestParamsWith(t *testing.T) { 84 expect := List{ 85 OrderBy: OrderBy{{Key: "1", Direction: OrderASC}, {Key: "2", Direction: OrderASC}, {Key: "3", Direction: OrderASC}}, 86 Filter: []string{"a", "b", "c"}, 87 Offset: 200, 88 Limit: 100, 89 } 90 91 got := ListAll.WithFilters("a", "b", "c").WithOrderBy("1,2,3").WithOffsetLimit(200, 100) 92 93 if diff := cmp.Diff(expect, got); diff != "" { 94 t.Errorf("result mismatch (-want +got):\n%s", diff) 95 } 96 } 97 98 func TestParamsValidate(t *testing.T) { 99 good := []List{ 100 {Limit: -1, Offset: 0}, 101 } 102 103 for i, c := range good { 104 t.Run(fmt.Sprintf("good_case_%d", i), func(t *testing.T) { 105 if err := c.Validate(); err != nil { 106 t.Errorf("unexpected error: %s", err) 107 } 108 }) 109 } 110 111 bad := []List{ 112 {Offset: -1}, 113 {Limit: -2}, 114 } 115 116 for i, c := range bad { 117 t.Run(fmt.Sprintf("bad_case_%d", i), func(t *testing.T) { 118 if err := c.Validate(); err == nil { 119 t.Errorf("expected error, got none") 120 } 121 }) 122 } 123 } 124 125 func TestListParamsAll(t *testing.T) { 126 cases := []struct { 127 p List 128 expect bool 129 }{ 130 {List{Limit: -1, Offset: 0}, true}, 131 {List{OrderBy: OrderBy{{Key: "date", Direction: OrderDESC}}, Limit: -1, Offset: 0}, true}, 132 {List{Filter: []string{"user:noodles"}, Limit: -1, Offset: 0}, true}, 133 134 {List{Limit: 0, Offset: 0}, false}, 135 {List{Limit: -1, Offset: 100000}, false}, 136 {List{OrderBy: OrderBy{{Key: "time", Direction: OrderASC}}, Limit: 0, Offset: 0}, false}, 137 } 138 139 for i, c := range cases { 140 got := c.p.All() 141 if c.expect != got { 142 t.Errorf("case %d result mismatch. want: %t got: %t", i, c.expect, got) 143 } 144 } 145 }