| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package memdocstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"io" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"slices" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/google/go-cmp/cmp" |
| 26 |
"gocloud.dev/docstore" |
| 27 |
"gocloud.dev/docstore/driver" |
| 28 |
"gocloud.dev/docstore/drivertest" |
| 29 |
) |
| 30 |
|
| 31 |
type harness struct{} |
| 32 |
|
| 33 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 34 |
t.Helper() |
| 35 |
|
| 36 |
return &harness{}, nil |
| 37 |
} |
| 38 |
|
| 39 |
func (h *harness) MakeCollection(_ context.Context, kind drivertest.CollectionKind) (driver.Collection, error) { |
| 40 |
switch kind { |
| 41 |
case drivertest.SingleKey, drivertest.NoRev: |
| 42 |
return newCollection(drivertest.KeyField, nil, nil) |
| 43 |
case drivertest.TwoKey: |
| 44 |
return newCollection("", drivertest.HighScoreKey, nil) |
| 45 |
case drivertest.AltRev: |
| 46 |
return newCollection(drivertest.KeyField, nil, &Options{RevisionField: drivertest.AlternateRevisionField}) |
| 47 |
default: |
| 48 |
panic("bad kind") |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func (*harness) BeforeDoTypes() []any { return nil } |
| 53 |
func (*harness) BeforeQueryTypes() []any { return nil } |
| 54 |
|
| 55 |
func (*harness) RevisionsEqual(rev1, rev2 any) bool { return rev1 == rev2 } |
| 56 |
|
| 57 |
func (*harness) SupportsAtomicWrites() bool { return true } |
| 58 |
|
| 59 |
func (*harness) Close() {} |
| 60 |
|
| 61 |
func TestConformance(t *testing.T) { |
| 62 |
// CodecTester is nil because memdocstore has no native representation. |
| 63 |
drivertest.RunConformanceTests(t, newHarness, nil, nil) |
| 64 |
} |
| 65 |
|
| 66 |
type docmap = map[string]any |
| 67 |
|
| 68 |
// memdocstore-specific tests. |
| 69 |
|
| 70 |
// The following tests test memdocstore's backend implementation. |
| 71 |
|
| 72 |
func TestUpdateEncodesValues(t *testing.T) { |
| 73 |
// Check that update encodes the values in mods. |
| 74 |
ctx := context.Background() |
| 75 |
dc, err := newCollection(drivertest.KeyField, nil, nil) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
coll := docstore.NewCollection(dc) |
| 80 |
defer coll.Close() |
| 81 |
doc := docmap{drivertest.KeyField: "testUpdateEncodes", "a": 1, dc.RevisionField(): nil} |
| 82 |
if err := coll.Put(ctx, doc); err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
if err := coll.Update(ctx, doc, docstore.Mods{"a": 2}); err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
got := docmap{drivertest.KeyField: doc[drivertest.KeyField]} |
| 89 |
// This Get will fail if the int value 2 in the above mod was not encoded to an int64. |
| 90 |
if err := coll.Get(ctx, got); err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
want := docmap{ |
| 94 |
drivertest.KeyField: doc[drivertest.KeyField], |
| 95 |
"a": int64(2), |
| 96 |
dc.RevisionField(): got[dc.RevisionField()], |
| 97 |
} |
| 98 |
if !cmp.Equal(got, want) { |
| 99 |
t.Errorf("got %v, want %v", got, want) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestUpdateAtomic(t *testing.T) { |
| 104 |
// Check that update is atomic. |
| 105 |
ctx := context.Background() |
| 106 |
dc, err := newCollection(drivertest.KeyField, nil, nil) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
coll := docstore.NewCollection(dc) |
| 111 |
defer coll.Close() |
| 112 |
doc := docmap{drivertest.KeyField: "testUpdateAtomic", "a": "A", "b": "B", dc.RevisionField(): nil} |
| 113 |
|
| 114 |
mods := docstore.Mods{"a": "Y", "b.c": "Z"} // "b" is not a map, so "b.c" is an error |
| 115 |
if err := coll.Put(ctx, doc); err != nil { |
| 116 |
t.Fatal(err) |
| 117 |
} |
| 118 |
if errs := coll.Actions().Update(doc, mods).Do(ctx); errs == nil { |
| 119 |
t.Fatal("got nil, want errors") |
| 120 |
} |
| 121 |
got := docmap{drivertest.KeyField: doc[drivertest.KeyField]} |
| 122 |
if err := coll.Get(ctx, got); err != nil { |
| 123 |
t.Fatal(err) |
| 124 |
} |
| 125 |
want := docmap{ |
| 126 |
drivertest.KeyField: doc[drivertest.KeyField], |
| 127 |
dc.RevisionField(): got[dc.RevisionField()], |
| 128 |
"a": "A", |
| 129 |
"b": "B", |
| 130 |
} |
| 131 |
if !cmp.Equal(got, want) { |
| 132 |
t.Errorf("got %v, want %v", got, want) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestQueryNested(t *testing.T) { |
| 137 |
ctx := context.Background() |
| 138 |
|
| 139 |
dc, err := newCollection(drivertest.KeyField, nil, &Options{AllowNestedSliceQueries: true}) |
| 140 |
if err != nil { |
| 141 |
t.Fatal(err) |
| 142 |
} |
| 143 |
coll := docstore.NewCollection(dc) |
| 144 |
defer coll.Close() |
| 145 |
|
| 146 |
// Set up test documents |
| 147 |
testDocs := []docmap{{ |
| 148 |
drivertest.KeyField: "TestQueryNested", |
| 149 |
"list": []any{docmap{"a": "A"}}, |
| 150 |
"map": docmap{"b": "B"}, |
| 151 |
"listOfMaps": []any{docmap{"id": "1"}, docmap{"id": "2"}, docmap{"id": "3"}}, |
| 152 |
"mapOfLists": docmap{"ids": []any{"1", "2", "3"}}, |
| 153 |
"deep": []any{docmap{"nesting": []any{docmap{"of": docmap{"elements": "yes"}}}}}, |
| 154 |
"listOfLists": []any{docmap{"items": []any{docmap{"price": 10}, docmap{"price": 20}}}}, |
| 155 |
dc.RevisionField(): nil, |
| 156 |
}, { |
| 157 |
drivertest.KeyField: "CheapItems", |
| 158 |
"items": []any{docmap{"price": 10}, docmap{"price": 1}}, |
| 159 |
dc.RevisionField(): nil, |
| 160 |
}, { |
| 161 |
drivertest.KeyField: "ExpensiveItems", |
| 162 |
"items": []any{docmap{"price": 50}, docmap{"price": 100}}, |
| 163 |
dc.RevisionField(): nil, |
| 164 |
}} |
| 165 |
|
| 166 |
for _, testDoc := range testDocs { |
| 167 |
err = coll.Put(ctx, testDoc) |
| 168 |
if err != nil { |
| 169 |
t.Fatal(err) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
tests := []struct { |
| 174 |
name string |
| 175 |
where []any |
| 176 |
wantKeys []string |
| 177 |
}{ |
| 178 |
{ |
| 179 |
name: "list field match", |
| 180 |
where: []any{"list.a", "=", "A"}, |
| 181 |
wantKeys: []string{"TestQueryNested"}, |
| 182 |
}, { |
| 183 |
name: "list field no match", |
| 184 |
where: []any{"list.a", "=", "missing"}, |
| 185 |
}, { |
| 186 |
name: "map field match", |
| 187 |
where: []any{"map.b", "=", "B"}, |
| 188 |
wantKeys: []string{"TestQueryNested"}, |
| 189 |
}, { |
| 190 |
name: "list of maps field match", |
| 191 |
where: []any{"listOfMaps.id", "=", "2"}, |
| 192 |
wantKeys: []string{"TestQueryNested"}, |
| 193 |
}, { |
| 194 |
name: "map of lists field match", |
| 195 |
where: []any{"mapOfLists.ids", "=", "1"}, |
| 196 |
wantKeys: []string{"TestQueryNested"}, |
| 197 |
}, { |
| 198 |
name: "deep nested field match", |
| 199 |
where: []any{"deep.nesting.of.elements", "=", "yes"}, |
| 200 |
wantKeys: []string{"TestQueryNested"}, |
| 201 |
}, { |
| 202 |
name: "list of lists exact price 10", |
| 203 |
where: []any{"listOfLists.items.price", "=", 10}, |
| 204 |
wantKeys: []string{"TestQueryNested"}, |
| 205 |
}, { |
| 206 |
name: "list of lists exact price 20", |
| 207 |
where: []any{"listOfLists.items.price", "=", 20}, |
| 208 |
wantKeys: []string{"TestQueryNested"}, |
| 209 |
}, { |
| 210 |
name: "list of lists price less than or equal to 20", |
| 211 |
where: []any{"listOfLists.items.price", "<=", 20}, |
| 212 |
wantKeys: []string{"TestQueryNested"}, |
| 213 |
}, { |
| 214 |
name: "items price equals 1", |
| 215 |
where: []any{"items.price", "=", 1}, |
| 216 |
wantKeys: []string{"CheapItems"}, |
| 217 |
}, { |
| 218 |
name: "items price equals 5 (no match)", |
| 219 |
where: []any{"items.price", "=", 5}, |
| 220 |
}, { |
| 221 |
name: "items price greater than or equal to 1", |
| 222 |
where: []any{"items.price", ">=", 1}, |
| 223 |
wantKeys: []string{"CheapItems", "ExpensiveItems"}, |
| 224 |
}, { |
| 225 |
name: "items price greater than or equal to 5", |
| 226 |
where: []any{"items.price", ">=", 5}, |
| 227 |
wantKeys: []string{"CheapItems", "ExpensiveItems"}, |
| 228 |
}, { |
| 229 |
name: "items price greater than or equal to 10", |
| 230 |
where: []any{"items.price", ">=", 10}, |
| 231 |
wantKeys: []string{"CheapItems", "ExpensiveItems"}, |
| 232 |
}, { |
| 233 |
name: "items price less than or equal to 50", |
| 234 |
where: []any{"items.price", "<=", 50}, |
| 235 |
wantKeys: []string{"CheapItems", "ExpensiveItems"}, |
| 236 |
}, |
| 237 |
} |
| 238 |
|
| 239 |
for _, tc := range tests { |
| 240 |
t.Run(tc.name, func(t *testing.T) { |
| 241 |
iter := coll.Query().Where(docstore.FieldPath(tc.where[0].(string)), tc.where[1].(string), tc.where[2]).Get(ctx) |
| 242 |
var got []docmap |
| 243 |
for { |
| 244 |
doc := docmap{} |
| 245 |
err := iter.Next(ctx, doc) |
| 246 |
if err != nil { |
| 247 |
if err == io.EOF { |
| 248 |
break |
| 249 |
} |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
got = append(got, doc) |
| 253 |
} |
| 254 |
|
| 255 |
// Extract keys from results |
| 256 |
var gotKeys []string |
| 257 |
for _, d := range got { |
| 258 |
if key, ok := d[drivertest.KeyField].(string); ok { |
| 259 |
gotKeys = append(gotKeys, key) |
| 260 |
} |
| 261 |
} |
| 262 |
slices.Sort(gotKeys) |
| 263 |
|
| 264 |
diff := cmp.Diff(gotKeys, tc.wantKeys) |
| 265 |
if diff != "" { |
| 266 |
t.Errorf("query results mismatch (-got +want):\n%s", diff) |
| 267 |
} |
| 268 |
}) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
func TestSortDocs(t *testing.T) { |
| 273 |
newDocs := func() []storedDoc { |
| 274 |
return []storedDoc{ |
| 275 |
{"a": int64(1), "b": "1", "c": 3.0}, |
| 276 |
{"a": int64(2), "b": "2", "c": 4.0}, |
| 277 |
{"a": int64(3), "b": "3"}, // missing "c" |
| 278 |
} |
| 279 |
} |
| 280 |
inorder := newDocs() |
| 281 |
reversed := newDocs() |
| 282 |
for i := 0; i < len(reversed)/2; i++ { |
| 283 |
j := len(reversed) - i - 1 |
| 284 |
reversed[i], reversed[j] = reversed[j], reversed[i] |
| 285 |
} |
| 286 |
|
| 287 |
for _, test := range []struct { |
| 288 |
field string |
| 289 |
ascending bool |
| 290 |
want []storedDoc |
| 291 |
}{ |
| 292 |
{"a", true, inorder}, |
| 293 |
{"a", false, reversed}, |
| 294 |
{"b", true, inorder}, |
| 295 |
{"b", false, reversed}, |
| 296 |
{"c", true, inorder}, |
| 297 |
{"c", false, []storedDoc{inorder[1], inorder[0], inorder[2]}}, |
| 298 |
} { |
| 299 |
got := newDocs() |
| 300 |
sortDocs(got, test.field, test.ascending) |
| 301 |
if diff := cmp.Diff(got, test.want); diff != "" { |
| 302 |
t.Errorf("%q, asc=%t:\n%s", test.field, test.ascending, diff) |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
func TestSaveAndLoad(t *testing.T) { |
| 308 |
dir := t.TempDir() |
| 309 |
|
| 310 |
// Load from nonexistent file should return empty data. |
| 311 |
f := filepath.Join(dir, "saveAndLoad") |
| 312 |
got, err := loadDocs(f) |
| 313 |
if err != nil { |
| 314 |
t.Fatalf("loading from nonexistent file, got %v, want nil", err) |
| 315 |
} |
| 316 |
if len(got) != 0 { |
| 317 |
t.Fatalf("loading from nonexistent file, got %v, want empty map", got) |
| 318 |
} |
| 319 |
|
| 320 |
// Save some data into the file. |
| 321 |
docs := map[any]storedDoc{ |
| 322 |
"k1": {"key": "k1", "a": 1}, |
| 323 |
"k2": {"key": "k2", "b": 2}, |
| 324 |
} |
| 325 |
if err := saveDocs(f, docs); err != nil { |
| 326 |
t.Fatal(err) |
| 327 |
} |
| 328 |
// File should exist now. |
| 329 |
if _, err := os.Lstat(f); err != nil { |
| 330 |
t.Fatal(err) |
| 331 |
} |
| 332 |
|
| 333 |
// Reload the data. |
| 334 |
got, err = loadDocs(f) |
| 335 |
if err != nil { |
| 336 |
t.Fatal(err) |
| 337 |
} |
| 338 |
if !cmp.Equal(got, docs) { |
| 339 |
t.Errorf("\ngot %v\nwant %v", got, docs) |
| 340 |
} |
| 341 |
} |