github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/query_test.go (about) 1 // Copyright (C) 2017 The GoHBase Authors. All rights reserved. 2 // This file is part of GoHBase. 3 // Use of this source code is governed by the Apache License 2.0 4 // that can be found in the COPYING file. 5 6 package hrpc 7 8 import ( 9 "context" 10 "errors" 11 "math" 12 "reflect" 13 "testing" 14 "time" 15 16 "github.com/tsuna/gohbase/filter" 17 "github.com/tsuna/gohbase/test" 18 ) 19 20 func TestFamiliesOption(t *testing.T) { 21 f := map[string][]string{"yolo": []string{"swag", "meow"}} 22 23 g, err := NewGet(context.Background(), nil, nil, Families(f)) 24 if err != nil { 25 t.Error(err) 26 } 27 28 if !reflect.DeepEqual(f, g.families) { 29 t.Errorf("expected %v, got %v", f, g.families) 30 } 31 32 _, err = NewPutStr(context.Background(), "", "", nil, Families(f)) 33 if err == nil || err.Error() != "'Families' option can only be used with Get or Scan request" { 34 t.Error(err) 35 } 36 } 37 38 func TestFiltersOption(t *testing.T) { 39 f := filter.NewColumnCountGetFilter(1) 40 g, err := NewGet(context.Background(), nil, nil, Filters(f)) 41 if err != nil { 42 t.Error(err) 43 } 44 45 if g.filter == nil { 46 t.Error("expected filter to be set") 47 } 48 49 _, err = NewPutStr(context.Background(), "", "", nil, Filters(f)) 50 if err == nil || err.Error() != "'Filters' option can only be used with Get or Scan request" { 51 t.Error(err) 52 } 53 } 54 55 func TestTimeRangeOption(t *testing.T) { 56 now := time.Now() 57 tests := []struct { 58 from time.Time 59 to time.Time 60 err error 61 }{ 62 {from: now, to: now.Add(time.Minute)}, 63 {from: now.Add(time.Minute), to: now, err: errors.New( 64 "'from' timestamp is greater or equal to 'to' timestamp")}, 65 {from: now, to: now, err: errors.New( 66 "'from' timestamp is greater or equal to 'to' timestamp")}, 67 } 68 69 for _, tcase := range tests { 70 g, err := NewGet(context.Background(), nil, nil, TimeRange(tcase.from, tcase.to)) 71 if !test.ErrEqual(tcase.err, err) { 72 t.Fatalf("expected %v, got %v", tcase.err, err) 73 } 74 if tcase.err != nil { 75 continue 76 } 77 78 from, to := g.fromTimestamp, g.toTimestamp 79 if fromExp := uint64(tcase.from.UnixNano() / 1e6); from != fromExp { 80 t.Errorf("expected from time %d, got from time %d", fromExp, from) 81 } 82 if toExp := uint64(tcase.to.UnixNano() / 1e6); to != toExp { 83 t.Errorf("expected to time %d, got to time %d", toExp, to) 84 } 85 86 _, err = NewPutStr(context.Background(), "", "", nil, TimeRange(tcase.to, tcase.from)) 87 if err == nil || err.Error() != 88 "'TimeRange' option can only be used with Get or Scan request" { 89 t.Error(err) 90 } 91 } 92 } 93 94 func TestMaxVersions(t *testing.T) { 95 v := uint32(123456) 96 g, err := NewGet(context.Background(), nil, nil, MaxVersions(v)) 97 if err != nil { 98 t.Error(err) 99 } 100 101 if vExp, vGot := v, g.maxVersions; vExp != vGot { 102 t.Errorf("expected %d, got %d", vExp, vGot) 103 } 104 105 _, err = NewGet(context.Background(), nil, nil, MaxVersions(uint32(math.MaxUint32))) 106 if err == nil || err.Error() != "'MaxVersions' exceeds supported number of versions" { 107 t.Error(err) 108 } 109 110 _, err = NewPutStr(context.Background(), "", "", nil, MaxVersions(v)) 111 if err == nil || err.Error() != 112 "'MaxVersions' option can only be used with Get or Scan request" { 113 t.Error(err) 114 } 115 } 116 117 func TestMaxResultsPerColumnFamily(t *testing.T) { 118 r := uint32(123456) 119 g, err := NewGet(context.Background(), nil, nil, MaxResultsPerColumnFamily(r)) 120 if err != nil { 121 t.Error(err) 122 } 123 124 if rExp, rGot := r, g.storeLimit; rExp != rGot { 125 t.Errorf("expected %d, got %d", rExp, rGot) 126 } 127 128 _, err = NewGet(context.Background(), nil, nil, 129 MaxResultsPerColumnFamily(uint32(math.MaxUint32))) 130 if err == nil || err.Error() != 131 "'MaxResultsPerColumnFamily' exceeds supported number of value results" { 132 t.Error(err) 133 } 134 135 _, err = NewPutStr(context.Background(), "", "", nil, MaxResultsPerColumnFamily(r)) 136 if err == nil || err.Error() != 137 "'MaxResultsPerColumnFamily' option can only be used with Get or Scan request" { 138 t.Error(err) 139 } 140 } 141 142 func TestResultOffset(t *testing.T) { 143 r := uint32(123456) 144 g, err := NewGet(context.Background(), nil, nil, ResultOffset(r)) 145 if err != nil { 146 t.Error(err) 147 } 148 149 if rExp, rGot := r, g.storeOffset; rExp != rGot { 150 t.Errorf("expected %d, got %d", rExp, rGot) 151 } 152 153 _, err = NewGet(context.Background(), nil, nil, ResultOffset(uint32(math.MaxUint32))) 154 if err == nil || err.Error() != "'ResultOffset' exceeds supported offset value" { 155 t.Error(err) 156 } 157 158 _, err = NewPutStr(context.Background(), "", "", nil, ResultOffset(r)) 159 if err == nil || err.Error() != 160 "'ResultOffset' option can only be used with Get or Scan request" { 161 t.Error(err) 162 } 163 } 164 165 func TestCacheBlocks(t *testing.T) { 166 // set CacheBlocks to false for Get 167 g, err := NewGet(context.Background(), nil, nil, CacheBlocks(false)) 168 if err != nil { 169 t.Error(err) 170 } 171 172 if cbExp, cbGot := false, g.cacheBlocks; cbExp != cbGot { 173 t.Errorf("expected %v, got %v", cbExp, cbGot) 174 } 175 176 // check that default CacheBlocks for Get is true 177 g2, err := NewGet(context.Background(), nil, nil) 178 if err != nil { 179 t.Error(err) 180 } 181 if cbExp, cbGot := true, g2.cacheBlocks; cbExp != cbGot { 182 t.Errorf("expected %v, got %v", cbExp, cbGot) 183 } 184 185 // explicitly set CacheBlocks to true for Get 186 s, err := NewScan(context.Background(), nil, CacheBlocks(true)) 187 if err != nil { 188 t.Error(err) 189 } 190 191 if cbExp, cbGot := true, s.cacheBlocks; cbExp != cbGot { 192 t.Errorf("expected %v, got %v", cbExp, cbGot) 193 } 194 195 // check that default CacheBlocks for Scan is true 196 s2, err := NewScan(context.Background(), nil) 197 if err != nil { 198 t.Error(err) 199 } 200 201 if cbExp, cbGot := true, s2.cacheBlocks; cbExp != cbGot { 202 t.Errorf("expected %v, got %v", cbExp, cbGot) 203 } 204 205 _, err = NewPutStr(context.Background(), "", "", nil, CacheBlocks(true)) 206 if err == nil || err.Error() != 207 "'CacheBlocks' option can only be used with Get or Scan request" { 208 t.Error(err) 209 } 210 } 211 212 func TestPriority(t *testing.T) { 213 get, err := NewGet(nil, nil, nil) 214 if err != nil { 215 t.Fatal(err) 216 } 217 if got := get.Priority(); got != 0 { 218 t.Errorf("expected 0, got %d", got) 219 } 220 get, err = NewGet(nil, nil, nil, Priority(5)) 221 if err != nil { 222 t.Fatal(err) 223 } 224 if got := get.Priority(); got != 5 { 225 t.Errorf("expected priority 5, got %d", got) 226 } 227 228 scan, err := NewScan(nil, nil) 229 if err != nil { 230 t.Fatal(err) 231 } 232 if got := scan.Priority(); got != 0 { 233 t.Errorf("expected 0, got %d", got) 234 } 235 scan, err = NewScan(nil, nil, Priority(5)) 236 if err != nil { 237 t.Fatal(err) 238 } 239 if got := scan.Priority(); got != 5 { 240 t.Errorf("expected priority 5, got %d", got) 241 } 242 243 _, err = NewPut(nil, nil, nil, nil, Priority(5)) 244 if err == nil { 245 t.Errorf("expected error when creating Put with Priority, but got none") 246 } 247 }