Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(560)

Side by Side Diff: service/datastore/datastore_test.go

Issue 1574353004: GitHub #8: Seed indexes from index.yml (Closed) Base URL: https://github.com/luci/gae@master
Patch Set: Fix missing parallel call in test Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // adapted from github.com/golang/appengine/datastore 5 // adapted from github.com/golang/appengine/datastore
6 6
7 package datastore 7 package datastore
8 8
9 import ( 9 import (
10 "bytes"
10 "fmt" 11 "fmt"
12 "io/ioutil"
13 "os"
14 "path/filepath"
15 "runtime"
11 "testing" 16 "testing"
12 17
13 "github.com/luci/gae/service/info" 18 "github.com/luci/gae/service/info"
14 "github.com/luci/luci-go/common/errors" 19 "github.com/luci/luci-go/common/errors"
15 . "github.com/luci/luci-go/common/testing/assertions" 20 . "github.com/luci/luci-go/common/testing/assertions"
16 . "github.com/smartystreets/goconvey/convey" 21 . "github.com/smartystreets/goconvey/convey"
17 "golang.org/x/net/context" 22 "golang.org/x/net/context"
18 ) 23 )
19 24
20 func fakeDatastoreFactory(c context.Context, wantTxn bool) RawInterface { 25 func fakeDatastoreFactory(c context.Context, wantTxn bool) RawInterface {
(...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 Convey("extra field with bad type", func() { 1289 Convey("extra field with bad type", func() {
1285 type Bad struct { 1290 type Bad struct {
1286 A int64 `gae:",extra"` 1291 A int64 `gae:",extra"`
1287 } 1292 }
1288 So(func() { GetPLS(&Bad{}) }, ShouldPanicLike, 1293 So(func() { GetPLS(&Bad{}) }, ShouldPanicLike,
1289 "struct 'extra' field has invalid type i nt64") 1294 "struct 'extra' field has invalid type i nt64")
1290 }) 1295 })
1291 }) 1296 })
1292 }) 1297 })
1293 } 1298 }
1299
1300 func TestParseIndexYAML(t *testing.T) {
1301 t.Parallel()
1302
1303 Convey("parses properly formatted YAML", t, func() {
1304 yaml := `
1305 indexes:
1306
1307 - kind: Cat
1308 ancestor: no
1309 properties:
1310 - name: name
1311 - name: age
1312 direction: desc
1313
1314 - kind: Cat
1315 properties:
1316 - name: name
1317 direction: asc
1318 - name: whiskers
1319 direction: desc
1320
1321 - kind: Store
1322 ancestor: yes
1323 properties:
1324 - name: business
1325 direction: asc
1326 - name: owner
1327 direction: asc
1328 `
1329 ids, err := ParseIndexYAML(bytes.NewBuffer([]byte(yaml)))
1330 So(err, ShouldBeNil)
1331
1332 expected := []*IndexDefinition{
1333 {
1334 Kind: "Cat",
1335 Ancestor: false,
1336 SortBy: []IndexColumn{
1337 {
1338 Property: "name",
1339 Descending: false,
1340 },
1341 {
1342 Property: "age",
1343 Descending: true,
1344 },
1345 },
1346 },
1347 {
1348 Kind: "Cat",
1349 Ancestor: false,
1350 SortBy: []IndexColumn{
1351 {
1352 Property: "name",
1353 Descending: false,
1354 },
1355 {
1356 Property: "whiskers",
1357 Descending: true,
1358 },
1359 },
1360 },
1361 {
1362 Kind: "Store",
1363 Ancestor: true,
1364 SortBy: []IndexColumn{
1365 {
1366 Property: "business",
1367 Descending: false,
1368 },
1369 {
1370 Property: "owner",
1371 Descending: false,
1372 },
1373 },
1374 },
1375 }
1376 So(ids, ShouldResembleV, expected)
1377 })
1378
1379 Convey("returns non-nil error for incorrectly formatted YAML", t, func() {
1380
1381 Convey("missing top level `indexes` key", func() {
1382 yaml := `
1383 - kind: Cat
1384 properties:
1385 - name: name
1386 - name: age
1387 direction: desc
1388 `
1389 _, err := ParseIndexYAML(bytes.NewBuffer([]byte(yaml)))
1390 So(err, ShouldNotBeNil)
1391 })
1392
1393 Convey("missing `name` key in property", func() {
1394 yaml := `
1395 indexes:
1396
1397 - kind: Cat
1398 ancestor: no
1399 properties:
1400 - name: name
1401 - direction: desc
1402 `
1403 _, err := ParseIndexYAML(bytes.NewBuffer([]byte(yaml)))
1404 So(err, ShouldNotBeNil)
1405 })
1406 })
1407 }
1408
1409 func TestFindAndParseIndexYAML(t *testing.T) {
1410 t.Parallel()
1411
1412 Convey("returns parsed index definitions for existing index YAML files", t, func() {
1413 // YAML content to write temporarily to disk
1414 yaml1 := `
1415 indexes:
1416
1417 - kind: Same Level
1418 properties:
1419 - name: name
1420 - name: age
1421 direction: desc
1422 `
1423 yaml2 := `
1424 indexes:
1425
1426 - kind: Higher Level
1427 properties:
1428 - name: name
1429 - name: age
1430 direction: desc
1431 `
1432 // determine the directory of this test file
1433 _, path, _, ok := runtime.Caller(0)
1434 if !ok {
1435 panic(fmt.Errorf("failed to determine test file path"))
1436 }
1437 sameLevelDir := filepath.Dir(path)
1438
1439 Convey("picks YAML file at same level as test file", func() {
1440 writePath1 := filepath.Join(sameLevelDir, "index.yml")
1441 writePath2 := filepath.Join(filepath.Dir(sameLevelDir), "index.yaml")
1442
1443 setup := func() {
1444 ioutil.WriteFile(writePath1, []byte(yaml1), 0600 )
1445 ioutil.WriteFile(writePath2, []byte(yaml2), 0600 )
1446 }
1447
1448 cleanup := func() {
1449 os.Remove(writePath1)
1450 os.Remove(writePath2)
1451 }
1452
1453 setup()
1454 defer cleanup()
1455 ids, err := FindAndParseIndexYAML()
1456 So(err, ShouldBeNil)
1457 So(ids[0].Kind, ShouldEqual, "Same Level")
1458 })
1459
1460 Convey("picks YAML file two levels up", func() {
1461 writePath := filepath.Join(filepath.Dir(filepath.Dir(sam eLevelDir)), "index.yaml")
1462
1463 setup := func() {
1464 ioutil.WriteFile(writePath, []byte(yaml2), 0600)
1465 }
1466
1467 cleanup := func() {
1468 os.Remove(writePath)
1469 }
1470
1471 setup()
1472 defer cleanup()
1473 ids, err := FindAndParseIndexYAML()
1474 So(err, ShouldBeNil)
1475 So(ids[0].Kind, ShouldEqual, "Higher Level")
1476 })
1477 })
1478 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698