integration_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. // Copyright 2014 Google LLC
  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. // http://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. package datastore
  15. import (
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "flag"
  20. "fmt"
  21. "log"
  22. "os"
  23. "reflect"
  24. "sort"
  25. "strings"
  26. "sync"
  27. "testing"
  28. "time"
  29. "cloud.google.com/go/internal/testutil"
  30. "cloud.google.com/go/rpcreplay"
  31. "google.golang.org/api/iterator"
  32. "google.golang.org/api/option"
  33. "google.golang.org/grpc"
  34. "google.golang.org/grpc/codes"
  35. "google.golang.org/grpc/status"
  36. )
  37. // TODO(djd): Make test entity clean up more robust: some test entities may
  38. // be left behind if tests are aborted, the transport fails, etc.
  39. var timeNow = time.Now()
  40. // suffix is a timestamp-based suffix which is appended to key names,
  41. // particularly for the root keys of entity groups. This reduces flakiness
  42. // when the tests are run in parallel.
  43. var suffix string
  44. const replayFilename = "datastore.replay"
  45. type replayInfo struct {
  46. ProjectID string
  47. Time time.Time
  48. }
  49. var (
  50. record = flag.Bool("record", false, "record RPCs")
  51. newTestClient = func(ctx context.Context, t *testing.T) *Client {
  52. return newClient(ctx, t, nil)
  53. }
  54. )
  55. func TestMain(m *testing.M) {
  56. os.Exit(testMain(m))
  57. }
  58. func testMain(m *testing.M) int {
  59. flag.Parse()
  60. if testing.Short() {
  61. if *record {
  62. log.Fatal("cannot combine -short and -record")
  63. }
  64. if testutil.CanReplay(replayFilename) {
  65. initReplay()
  66. }
  67. } else if *record {
  68. if testutil.ProjID() == "" {
  69. log.Fatal("must record with a project ID")
  70. }
  71. b, err := json.Marshal(replayInfo{
  72. ProjectID: testutil.ProjID(),
  73. Time: timeNow,
  74. })
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. rec, err := rpcreplay.NewRecorder(replayFilename, b)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. defer func() {
  83. if err := rec.Close(); err != nil {
  84. log.Fatalf("closing recorder: %v", err)
  85. }
  86. }()
  87. newTestClient = func(ctx context.Context, t *testing.T) *Client {
  88. return newClient(ctx, t, rec.DialOptions())
  89. }
  90. log.Printf("recording to %s", replayFilename)
  91. }
  92. suffix = fmt.Sprintf("-t%d", timeNow.UnixNano())
  93. return m.Run()
  94. }
  95. func initReplay() {
  96. rep, err := rpcreplay.NewReplayer(replayFilename)
  97. if err != nil {
  98. log.Fatal(err)
  99. }
  100. defer rep.Close()
  101. var ri replayInfo
  102. if err := json.Unmarshal(rep.Initial(), &ri); err != nil {
  103. log.Fatalf("unmarshaling initial replay info: %v", err)
  104. }
  105. timeNow = ri.Time.In(time.Local)
  106. conn, err := rep.Connection()
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. newTestClient = func(ctx context.Context, t *testing.T) *Client {
  111. client, err := NewClient(ctx, ri.ProjectID, option.WithGRPCConn(conn))
  112. if err != nil {
  113. t.Fatalf("NewClient: %v", err)
  114. }
  115. return client
  116. }
  117. log.Printf("replaying from %s", replayFilename)
  118. }
  119. func newClient(ctx context.Context, t *testing.T, dialOpts []grpc.DialOption) *Client {
  120. if testing.Short() {
  121. t.Skip("Integration tests skipped in short mode")
  122. }
  123. ts := testutil.TokenSource(ctx, ScopeDatastore)
  124. if ts == nil {
  125. t.Skip("Integration tests skipped. See CONTRIBUTING.md for details")
  126. }
  127. opts := []option.ClientOption{option.WithTokenSource(ts)}
  128. for _, opt := range dialOpts {
  129. opts = append(opts, option.WithGRPCDialOption(opt))
  130. }
  131. client, err := NewClient(ctx, testutil.ProjID(), opts...)
  132. if err != nil {
  133. t.Fatalf("NewClient: %v", err)
  134. }
  135. return client
  136. }
  137. func TestBasics(t *testing.T) {
  138. ctx, _ := context.WithTimeout(context.Background(), time.Second*20)
  139. client := newTestClient(ctx, t)
  140. defer client.Close()
  141. type X struct {
  142. I int
  143. S string
  144. T time.Time
  145. }
  146. x0 := X{66, "99", timeNow.Truncate(time.Millisecond)}
  147. k, err := client.Put(ctx, IncompleteKey("BasicsX", nil), &x0)
  148. if err != nil {
  149. t.Fatalf("client.Put: %v", err)
  150. }
  151. x1 := X{}
  152. err = client.Get(ctx, k, &x1)
  153. if err != nil {
  154. t.Errorf("client.Get: %v", err)
  155. }
  156. err = client.Delete(ctx, k)
  157. if err != nil {
  158. t.Errorf("client.Delete: %v", err)
  159. }
  160. if !testutil.Equal(x0, x1) {
  161. t.Errorf("compare: x0=%v, x1=%v", x0, x1)
  162. }
  163. }
  164. func TestTopLevelKeyLoaded(t *testing.T) {
  165. ctx, _ := context.WithTimeout(context.Background(), time.Second*20)
  166. client := newTestClient(ctx, t)
  167. defer client.Close()
  168. completeKey := NameKey("EntityWithKey", "myent", nil)
  169. type EntityWithKey struct {
  170. I int
  171. S string
  172. K *Key `datastore:"__key__"`
  173. }
  174. in := &EntityWithKey{
  175. I: 12,
  176. S: "abcd",
  177. }
  178. k, err := client.Put(ctx, completeKey, in)
  179. if err != nil {
  180. t.Fatalf("client.Put: %v", err)
  181. }
  182. var e EntityWithKey
  183. err = client.Get(ctx, k, &e)
  184. if err != nil {
  185. t.Fatalf("client.Get: %v", err)
  186. }
  187. // The two keys should be absolutely identical.
  188. if !testutil.Equal(e.K, k) {
  189. t.Fatalf("e.K not equal to k; got %#v, want %#v", e.K, k)
  190. }
  191. }
  192. func TestListValues(t *testing.T) {
  193. ctx := context.Background()
  194. client := newTestClient(ctx, t)
  195. defer client.Close()
  196. p0 := PropertyList{
  197. {Name: "L", Value: []interface{}{int64(12), "string", true}},
  198. }
  199. k, err := client.Put(ctx, IncompleteKey("ListValue", nil), &p0)
  200. if err != nil {
  201. t.Fatalf("client.Put: %v", err)
  202. }
  203. var p1 PropertyList
  204. if err := client.Get(ctx, k, &p1); err != nil {
  205. t.Errorf("client.Get: %v", err)
  206. }
  207. if !testutil.Equal(p0, p1) {
  208. t.Errorf("compare:\np0=%v\np1=%#v", p0, p1)
  209. }
  210. if err = client.Delete(ctx, k); err != nil {
  211. t.Errorf("client.Delete: %v", err)
  212. }
  213. }
  214. func TestGetMulti(t *testing.T) {
  215. ctx := context.Background()
  216. client := newTestClient(ctx, t)
  217. defer client.Close()
  218. type X struct {
  219. I int
  220. }
  221. p := NameKey("X", "x"+suffix, nil)
  222. cases := []struct {
  223. key *Key
  224. put bool
  225. }{
  226. {key: NameKey("X", "item1", p), put: true},
  227. {key: NameKey("X", "item2", p), put: false},
  228. {key: NameKey("X", "item3", p), put: false},
  229. {key: NameKey("X", "item3", p), put: false},
  230. {key: NameKey("X", "item4", p), put: true},
  231. }
  232. var src, dst []*X
  233. var srcKeys, dstKeys []*Key
  234. for _, c := range cases {
  235. dst = append(dst, &X{})
  236. dstKeys = append(dstKeys, c.key)
  237. if c.put {
  238. src = append(src, &X{})
  239. srcKeys = append(srcKeys, c.key)
  240. }
  241. }
  242. if _, err := client.PutMulti(ctx, srcKeys, src); err != nil {
  243. t.Error(err)
  244. }
  245. err := client.GetMulti(ctx, dstKeys, dst)
  246. if err == nil {
  247. t.Errorf("client.GetMulti got %v, expected error", err)
  248. }
  249. e, ok := err.(MultiError)
  250. if !ok {
  251. t.Errorf("client.GetMulti got %T, expected MultiError", err)
  252. }
  253. for i, err := range e {
  254. got, want := err, (error)(nil)
  255. if !cases[i].put {
  256. got, want = err, ErrNoSuchEntity
  257. }
  258. if got != want {
  259. t.Errorf("MultiError[%d] == %v, want %v", i, got, want)
  260. }
  261. }
  262. }
  263. type Z struct {
  264. S string
  265. T string `datastore:",noindex"`
  266. P []byte
  267. K []byte `datastore:",noindex"`
  268. }
  269. func (z Z) String() string {
  270. var lens []string
  271. v := reflect.ValueOf(z)
  272. for i := 0; i < v.NumField(); i++ {
  273. if l := v.Field(i).Len(); l > 0 {
  274. lens = append(lens, fmt.Sprintf("len(%s)=%d", v.Type().Field(i).Name, l))
  275. }
  276. }
  277. return fmt.Sprintf("Z{ %s }", strings.Join(lens, ","))
  278. }
  279. func TestUnindexableValues(t *testing.T) {
  280. ctx := context.Background()
  281. client := newTestClient(ctx, t)
  282. defer client.Close()
  283. x1500 := strings.Repeat("x", 1500)
  284. x1501 := strings.Repeat("x", 1501)
  285. testCases := []struct {
  286. in Z
  287. wantErr bool
  288. }{
  289. {in: Z{S: x1500}, wantErr: false},
  290. {in: Z{S: x1501}, wantErr: true},
  291. {in: Z{T: x1500}, wantErr: false},
  292. {in: Z{T: x1501}, wantErr: false},
  293. {in: Z{P: []byte(x1500)}, wantErr: false},
  294. {in: Z{P: []byte(x1501)}, wantErr: true},
  295. {in: Z{K: []byte(x1500)}, wantErr: false},
  296. {in: Z{K: []byte(x1501)}, wantErr: false},
  297. }
  298. for _, tt := range testCases {
  299. _, err := client.Put(ctx, IncompleteKey("BasicsZ", nil), &tt.in)
  300. if (err != nil) != tt.wantErr {
  301. t.Errorf("client.Put %s got err %v, want err %t", tt.in, err, tt.wantErr)
  302. }
  303. }
  304. }
  305. func TestNilKey(t *testing.T) {
  306. ctx := context.Background()
  307. client := newTestClient(ctx, t)
  308. defer client.Close()
  309. testCases := []struct {
  310. in K0
  311. wantErr bool
  312. }{
  313. {in: K0{K: testKey0}, wantErr: false},
  314. {in: K0{}, wantErr: false},
  315. }
  316. for _, tt := range testCases {
  317. _, err := client.Put(ctx, IncompleteKey("NilKey", nil), &tt.in)
  318. if (err != nil) != tt.wantErr {
  319. t.Errorf("client.Put %s got err %v, want err %t", tt.in, err, tt.wantErr)
  320. }
  321. }
  322. }
  323. type SQChild struct {
  324. I, J int
  325. T, U int64
  326. }
  327. type SQTestCase struct {
  328. desc string
  329. q *Query
  330. wantCount int
  331. wantSum int
  332. }
  333. func testSmallQueries(ctx context.Context, t *testing.T, client *Client, parent *Key, children []*SQChild,
  334. testCases []SQTestCase, extraTests ...func()) {
  335. keys := make([]*Key, len(children))
  336. for i := range keys {
  337. keys[i] = IncompleteKey("SQChild", parent)
  338. }
  339. keys, err := client.PutMulti(ctx, keys, children)
  340. if err != nil {
  341. t.Fatalf("client.PutMulti: %v", err)
  342. }
  343. defer func() {
  344. err := client.DeleteMulti(ctx, keys)
  345. if err != nil {
  346. t.Errorf("client.DeleteMulti: %v", err)
  347. }
  348. }()
  349. for _, tc := range testCases {
  350. count, err := client.Count(ctx, tc.q)
  351. if err != nil {
  352. t.Errorf("Count %q: %v", tc.desc, err)
  353. continue
  354. }
  355. if count != tc.wantCount {
  356. t.Errorf("Count %q: got %d want %d", tc.desc, count, tc.wantCount)
  357. continue
  358. }
  359. }
  360. for _, tc := range testCases {
  361. var got []SQChild
  362. _, err := client.GetAll(ctx, tc.q, &got)
  363. if err != nil {
  364. t.Errorf("client.GetAll %q: %v", tc.desc, err)
  365. continue
  366. }
  367. sum := 0
  368. for _, c := range got {
  369. sum += c.I + c.J
  370. }
  371. if sum != tc.wantSum {
  372. t.Errorf("sum %q: got %d want %d", tc.desc, sum, tc.wantSum)
  373. continue
  374. }
  375. }
  376. for _, x := range extraTests {
  377. x()
  378. }
  379. }
  380. func TestFilters(t *testing.T) {
  381. ctx := context.Background()
  382. client := newTestClient(ctx, t)
  383. defer client.Close()
  384. parent := NameKey("SQParent", "TestFilters"+suffix, nil)
  385. now := timeNow.Truncate(time.Millisecond).Unix()
  386. children := []*SQChild{
  387. {I: 0, T: now, U: now},
  388. {I: 1, T: now, U: now},
  389. {I: 2, T: now, U: now},
  390. {I: 3, T: now, U: now},
  391. {I: 4, T: now, U: now},
  392. {I: 5, T: now, U: now},
  393. {I: 6, T: now, U: now},
  394. {I: 7, T: now, U: now},
  395. }
  396. baseQuery := NewQuery("SQChild").Ancestor(parent).Filter("T=", now)
  397. testSmallQueries(ctx, t, client, parent, children, []SQTestCase{
  398. {
  399. "I>1",
  400. baseQuery.Filter("I>", 1),
  401. 6,
  402. 2 + 3 + 4 + 5 + 6 + 7,
  403. },
  404. {
  405. "I>2 AND I<=5",
  406. baseQuery.Filter("I>", 2).Filter("I<=", 5),
  407. 3,
  408. 3 + 4 + 5,
  409. },
  410. {
  411. "I>=3 AND I<3",
  412. baseQuery.Filter("I>=", 3).Filter("I<", 3),
  413. 0,
  414. 0,
  415. },
  416. {
  417. "I=4",
  418. baseQuery.Filter("I=", 4),
  419. 1,
  420. 4,
  421. },
  422. }, func() {
  423. got := []*SQChild{}
  424. want := []*SQChild{
  425. {I: 0, T: now, U: now},
  426. {I: 1, T: now, U: now},
  427. {I: 2, T: now, U: now},
  428. {I: 3, T: now, U: now},
  429. {I: 4, T: now, U: now},
  430. {I: 5, T: now, U: now},
  431. {I: 6, T: now, U: now},
  432. {I: 7, T: now, U: now},
  433. }
  434. _, err := client.GetAll(ctx, baseQuery.Order("I"), &got)
  435. if err != nil {
  436. t.Errorf("client.GetAll: %v", err)
  437. }
  438. if !testutil.Equal(got, want) {
  439. t.Errorf("compare: got=%v, want=%v", got, want)
  440. }
  441. }, func() {
  442. got := []*SQChild{}
  443. want := []*SQChild{
  444. {I: 7, T: now, U: now},
  445. {I: 6, T: now, U: now},
  446. {I: 5, T: now, U: now},
  447. {I: 4, T: now, U: now},
  448. {I: 3, T: now, U: now},
  449. {I: 2, T: now, U: now},
  450. {I: 1, T: now, U: now},
  451. {I: 0, T: now, U: now},
  452. }
  453. _, err := client.GetAll(ctx, baseQuery.Order("-I"), &got)
  454. if err != nil {
  455. t.Errorf("client.GetAll: %v", err)
  456. }
  457. if !testutil.Equal(got, want) {
  458. t.Errorf("compare: got=%v, want=%v", got, want)
  459. }
  460. })
  461. }
  462. type ckey struct{}
  463. func TestLargeQuery(t *testing.T) {
  464. ctx := context.Background()
  465. client := newTestClient(ctx, t)
  466. defer client.Close()
  467. parent := NameKey("LQParent", "TestFilters"+suffix, nil)
  468. now := timeNow.Truncate(time.Millisecond).Unix()
  469. // Make a large number of children entities.
  470. const n = 800
  471. children := make([]*SQChild, 0, n)
  472. keys := make([]*Key, 0, n)
  473. for i := 0; i < n; i++ {
  474. children = append(children, &SQChild{I: i, T: now, U: now})
  475. keys = append(keys, IncompleteKey("SQChild", parent))
  476. }
  477. // Store using PutMulti in batches.
  478. const batchSize = 500
  479. for i := 0; i < n; i = i + 500 {
  480. j := i + batchSize
  481. if j > n {
  482. j = n
  483. }
  484. fullKeys, err := client.PutMulti(ctx, keys[i:j], children[i:j])
  485. if err != nil {
  486. t.Fatalf("PutMulti(%d, %d): %v", i, j, err)
  487. }
  488. defer func() {
  489. err := client.DeleteMulti(ctx, fullKeys)
  490. if err != nil {
  491. t.Errorf("client.DeleteMulti: %v", err)
  492. }
  493. }()
  494. }
  495. q := NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Order("I")
  496. // Wait group to allow us to run query tests in parallel below.
  497. var wg sync.WaitGroup
  498. // Check we get the expected count and results for various limits/offsets.
  499. queryTests := []struct {
  500. limit, offset, want int
  501. }{
  502. // Just limit.
  503. {limit: 0, want: 0},
  504. {limit: 100, want: 100},
  505. {limit: 501, want: 501},
  506. {limit: n, want: n},
  507. {limit: n * 2, want: n},
  508. {limit: -1, want: n},
  509. // Just offset.
  510. {limit: -1, offset: 100, want: n - 100},
  511. {limit: -1, offset: 500, want: n - 500},
  512. {limit: -1, offset: n, want: 0},
  513. // Limit and offset.
  514. {limit: 100, offset: 100, want: 100},
  515. {limit: 1000, offset: 100, want: n - 100},
  516. {limit: 500, offset: 500, want: n - 500},
  517. }
  518. for _, tt := range queryTests {
  519. q := q.Limit(tt.limit).Offset(tt.offset)
  520. wg.Add(1)
  521. go func(limit, offset, want int) {
  522. defer wg.Done()
  523. // Check Count returns the expected number of results.
  524. count, err := client.Count(ctx, q)
  525. if err != nil {
  526. t.Errorf("client.Count(limit=%d offset=%d): %v", limit, offset, err)
  527. return
  528. }
  529. if count != want {
  530. t.Errorf("Count(limit=%d offset=%d) returned %d, want %d", limit, offset, count, want)
  531. }
  532. var got []SQChild
  533. _, err = client.GetAll(ctx, q, &got)
  534. if err != nil {
  535. t.Errorf("client.GetAll(limit=%d offset=%d): %v", limit, offset, err)
  536. return
  537. }
  538. if len(got) != want {
  539. t.Errorf("GetAll(limit=%d offset=%d) returned %d, want %d", limit, offset, len(got), want)
  540. }
  541. for i, child := range got {
  542. if got, want := child.I, i+offset; got != want {
  543. t.Errorf("GetAll(limit=%d offset=%d) got[%d].I == %d; want %d", limit, offset, i, got, want)
  544. break
  545. }
  546. }
  547. }(tt.limit, tt.offset, tt.want)
  548. }
  549. // Also check iterator cursor behaviour.
  550. cursorTests := []struct {
  551. limit, offset int // Query limit and offset.
  552. count int // The number of times to call "next"
  553. want int // The I value of the desired element, -1 for "Done".
  554. }{
  555. // No limits.
  556. {count: 0, limit: -1, want: 0},
  557. {count: 5, limit: -1, want: 5},
  558. {count: 500, limit: -1, want: 500},
  559. {count: 1000, limit: -1, want: -1}, // No more results.
  560. // Limits.
  561. {count: 5, limit: 5, want: 5},
  562. {count: 500, limit: 5, want: 5},
  563. {count: 1000, limit: 1000, want: -1}, // No more results.
  564. // Offsets.
  565. {count: 0, offset: 5, limit: -1, want: 5},
  566. {count: 5, offset: 5, limit: -1, want: 10},
  567. {count: 200, offset: 500, limit: -1, want: 700},
  568. {count: 200, offset: 1000, limit: -1, want: -1}, // No more results.
  569. }
  570. for _, tt := range cursorTests {
  571. wg.Add(1)
  572. go func(count, limit, offset, want int) {
  573. defer wg.Done()
  574. ctx := context.WithValue(ctx, ckey{}, fmt.Sprintf("c=%d,l=%d,o=%d", count, limit, offset))
  575. // Run iterator through count calls to Next.
  576. it := client.Run(ctx, q.Limit(limit).Offset(offset).KeysOnly())
  577. for i := 0; i < count; i++ {
  578. _, err := it.Next(nil)
  579. if err == iterator.Done {
  580. break
  581. }
  582. if err != nil {
  583. t.Errorf("count=%d, limit=%d, offset=%d: it.Next failed at i=%d", count, limit, offset, i)
  584. return
  585. }
  586. }
  587. // Grab the cursor.
  588. cursor, err := it.Cursor()
  589. if err != nil {
  590. t.Errorf("count=%d, limit=%d, offset=%d: it.Cursor: %v", count, limit, offset, err)
  591. return
  592. }
  593. // Make a request for the next element.
  594. it = client.Run(ctx, q.Limit(1).Start(cursor))
  595. var entity SQChild
  596. _, err = it.Next(&entity)
  597. switch {
  598. case want == -1:
  599. if err != iterator.Done {
  600. t.Errorf("count=%d, limit=%d, offset=%d: it.Next from cursor %v, want Done", count, limit, offset, err)
  601. }
  602. case err != nil:
  603. t.Errorf("count=%d, limit=%d, offset=%d: it.Next from cursor: %v, want nil", count, limit, offset, err)
  604. case entity.I != want:
  605. t.Errorf("count=%d, limit=%d, offset=%d: got.I = %d, want %d", count, limit, offset, entity.I, want)
  606. }
  607. }(tt.count, tt.limit, tt.offset, tt.want)
  608. }
  609. wg.Wait()
  610. }
  611. func TestEventualConsistency(t *testing.T) {
  612. // TODO(jba): either make this actually test eventual consistency, or
  613. // delete it. Currently it behaves the same with or without the
  614. // EventualConsistency call.
  615. ctx := context.Background()
  616. client := newTestClient(ctx, t)
  617. defer client.Close()
  618. parent := NameKey("SQParent", "TestEventualConsistency"+suffix, nil)
  619. now := timeNow.Truncate(time.Millisecond).Unix()
  620. children := []*SQChild{
  621. {I: 0, T: now, U: now},
  622. {I: 1, T: now, U: now},
  623. {I: 2, T: now, U: now},
  624. }
  625. query := NewQuery("SQChild").Ancestor(parent).Filter("T =", now).EventualConsistency()
  626. testSmallQueries(ctx, t, client, parent, children, nil, func() {
  627. got, err := client.Count(ctx, query)
  628. if err != nil {
  629. t.Fatalf("Count: %v", err)
  630. }
  631. if got < 0 || 3 < got {
  632. t.Errorf("Count: got %d, want [0,3]", got)
  633. }
  634. })
  635. }
  636. func TestProjection(t *testing.T) {
  637. ctx := context.Background()
  638. client := newTestClient(ctx, t)
  639. defer client.Close()
  640. parent := NameKey("SQParent", "TestProjection"+suffix, nil)
  641. now := timeNow.Truncate(time.Millisecond).Unix()
  642. children := []*SQChild{
  643. {I: 1 << 0, J: 100, T: now, U: now},
  644. {I: 1 << 1, J: 100, T: now, U: now},
  645. {I: 1 << 2, J: 200, T: now, U: now},
  646. {I: 1 << 3, J: 300, T: now, U: now},
  647. {I: 1 << 4, J: 300, T: now, U: now},
  648. }
  649. baseQuery := NewQuery("SQChild").Ancestor(parent).Filter("T=", now).Filter("J>", 150)
  650. testSmallQueries(ctx, t, client, parent, children, []SQTestCase{
  651. {
  652. "project",
  653. baseQuery.Project("J"),
  654. 3,
  655. 200 + 300 + 300,
  656. },
  657. {
  658. "distinct",
  659. baseQuery.Project("J").Distinct(),
  660. 2,
  661. 200 + 300,
  662. },
  663. {
  664. "distinct on",
  665. baseQuery.Project("J").DistinctOn("J"),
  666. 2,
  667. 200 + 300,
  668. },
  669. {
  670. "project on meaningful (GD_WHEN) field",
  671. baseQuery.Project("U"),
  672. 3,
  673. 0,
  674. },
  675. })
  676. }
  677. func TestAllocateIDs(t *testing.T) {
  678. ctx := context.Background()
  679. client := newTestClient(ctx, t)
  680. defer client.Close()
  681. keys := make([]*Key, 5)
  682. for i := range keys {
  683. keys[i] = IncompleteKey("AllocID", nil)
  684. }
  685. keys, err := client.AllocateIDs(ctx, keys)
  686. if err != nil {
  687. t.Errorf("AllocID #0 failed: %v", err)
  688. }
  689. if want := len(keys); want != 5 {
  690. t.Errorf("Expected to allocate 5 keys, %d keys are found", want)
  691. }
  692. for _, k := range keys {
  693. if k.Incomplete() {
  694. t.Errorf("Unexpeceted incomplete key found: %v", k)
  695. }
  696. }
  697. }
  698. func TestGetAllWithFieldMismatch(t *testing.T) {
  699. ctx := context.Background()
  700. client := newTestClient(ctx, t)
  701. defer client.Close()
  702. type Fat struct {
  703. X, Y int
  704. }
  705. type Thin struct {
  706. X int
  707. }
  708. // Ancestor queries (those within an entity group) are strongly consistent
  709. // by default, which prevents a test from being flaky.
  710. // See https://cloud.google.com/appengine/docs/go/datastore/queries#Go_Data_consistency
  711. // for more information.
  712. parent := NameKey("SQParent", "TestGetAllWithFieldMismatch"+suffix, nil)
  713. putKeys := make([]*Key, 3)
  714. for i := range putKeys {
  715. putKeys[i] = IDKey("GetAllThing", int64(10+i), parent)
  716. _, err := client.Put(ctx, putKeys[i], &Fat{X: 20 + i, Y: 30 + i})
  717. if err != nil {
  718. t.Fatalf("client.Put: %v", err)
  719. }
  720. }
  721. var got []Thin
  722. want := []Thin{
  723. {X: 20},
  724. {X: 21},
  725. {X: 22},
  726. }
  727. getKeys, err := client.GetAll(ctx, NewQuery("GetAllThing").Ancestor(parent), &got)
  728. if len(getKeys) != 3 && !testutil.Equal(getKeys, putKeys) {
  729. t.Errorf("client.GetAll: keys differ\ngetKeys=%v\nputKeys=%v", getKeys, putKeys)
  730. }
  731. if !testutil.Equal(got, want) {
  732. t.Errorf("client.GetAll: entities differ\ngot =%v\nwant=%v", got, want)
  733. }
  734. if _, ok := err.(*ErrFieldMismatch); !ok {
  735. t.Errorf("client.GetAll: got err=%v, want ErrFieldMismatch", err)
  736. }
  737. }
  738. func TestKindlessQueries(t *testing.T) {
  739. ctx := context.Background()
  740. client := newTestClient(ctx, t)
  741. defer client.Close()
  742. type Dee struct {
  743. I int
  744. Why string
  745. }
  746. type Dum struct {
  747. I int
  748. Pling string
  749. }
  750. parent := NameKey("Tweedle", "tweedle"+suffix, nil)
  751. keys := []*Key{
  752. NameKey("Dee", "dee0", parent),
  753. NameKey("Dum", "dum1", parent),
  754. NameKey("Dum", "dum2", parent),
  755. NameKey("Dum", "dum3", parent),
  756. }
  757. src := []interface{}{
  758. &Dee{1, "binary0001"},
  759. &Dum{2, "binary0010"},
  760. &Dum{4, "binary0100"},
  761. &Dum{8, "binary1000"},
  762. }
  763. keys, err := client.PutMulti(ctx, keys, src)
  764. if err != nil {
  765. t.Fatalf("put: %v", err)
  766. }
  767. testCases := []struct {
  768. desc string
  769. query *Query
  770. want []int
  771. wantErr string
  772. }{
  773. {
  774. desc: "Dee",
  775. query: NewQuery("Dee"),
  776. want: []int{1},
  777. },
  778. {
  779. desc: "Doh",
  780. query: NewQuery("Doh"),
  781. want: nil},
  782. {
  783. desc: "Dum",
  784. query: NewQuery("Dum"),
  785. want: []int{2, 4, 8},
  786. },
  787. {
  788. desc: "",
  789. query: NewQuery(""),
  790. want: []int{1, 2, 4, 8},
  791. },
  792. {
  793. desc: "Kindless filter",
  794. query: NewQuery("").Filter("__key__ =", keys[2]),
  795. want: []int{4},
  796. },
  797. {
  798. desc: "Kindless order",
  799. query: NewQuery("").Order("__key__"),
  800. want: []int{1, 2, 4, 8},
  801. },
  802. {
  803. desc: "Kindless bad filter",
  804. query: NewQuery("").Filter("I =", 4),
  805. wantErr: "kind is required",
  806. },
  807. {
  808. desc: "Kindless bad order",
  809. query: NewQuery("").Order("-__key__"),
  810. wantErr: "kind is required for all orders except __key__ ascending",
  811. },
  812. }
  813. loop:
  814. for _, tc := range testCases {
  815. q := tc.query.Ancestor(parent)
  816. gotCount, err := client.Count(ctx, q)
  817. if err != nil {
  818. if tc.wantErr == "" || !strings.Contains(err.Error(), tc.wantErr) {
  819. t.Errorf("count %q: err %v, want err %q", tc.desc, err, tc.wantErr)
  820. }
  821. continue
  822. }
  823. if tc.wantErr != "" {
  824. t.Errorf("count %q: want err %q", tc.desc, tc.wantErr)
  825. continue
  826. }
  827. if gotCount != len(tc.want) {
  828. t.Errorf("count %q: got %d want %d", tc.desc, gotCount, len(tc.want))
  829. continue
  830. }
  831. var got []int
  832. for iter := client.Run(ctx, q); ; {
  833. var dst struct {
  834. I int
  835. Why, Pling string
  836. }
  837. _, err := iter.Next(&dst)
  838. if err == iterator.Done {
  839. break
  840. }
  841. if err != nil {
  842. t.Errorf("iter.Next %q: %v", tc.desc, err)
  843. continue loop
  844. }
  845. got = append(got, dst.I)
  846. }
  847. sort.Ints(got)
  848. if !testutil.Equal(got, tc.want) {
  849. t.Errorf("elems %q: got %+v want %+v", tc.desc, got, tc.want)
  850. continue
  851. }
  852. }
  853. }
  854. func TestTransaction(t *testing.T) {
  855. ctx := context.Background()
  856. client := newTestClient(ctx, t)
  857. defer client.Close()
  858. type Counter struct {
  859. N int
  860. T time.Time
  861. }
  862. bangErr := errors.New("bang")
  863. tests := []struct {
  864. desc string
  865. causeConflict []bool
  866. retErr []error
  867. want int
  868. wantErr error
  869. }{
  870. {
  871. desc: "3 attempts, no conflicts",
  872. causeConflict: []bool{false},
  873. retErr: []error{nil},
  874. want: 11,
  875. },
  876. {
  877. desc: "1 attempt, user error",
  878. causeConflict: []bool{false},
  879. retErr: []error{bangErr},
  880. wantErr: bangErr,
  881. },
  882. {
  883. desc: "2 attempts, 1 conflict",
  884. causeConflict: []bool{true, false},
  885. retErr: []error{nil, nil},
  886. want: 13, // Each conflict increments by 2.
  887. },
  888. {
  889. desc: "3 attempts, 3 conflicts",
  890. causeConflict: []bool{true, true, true},
  891. retErr: []error{nil, nil, nil},
  892. wantErr: ErrConcurrentTransaction,
  893. },
  894. }
  895. for i, tt := range tests {
  896. // Put a new counter.
  897. c := &Counter{N: 10, T: timeNow}
  898. key, err := client.Put(ctx, IncompleteKey("TransCounter", nil), c)
  899. if err != nil {
  900. t.Errorf("%s: client.Put: %v", tt.desc, err)
  901. continue
  902. }
  903. defer client.Delete(ctx, key)
  904. // Increment the counter in a transaction.
  905. // The test case can manually cause a conflict or return an
  906. // error at each attempt.
  907. var attempts int
  908. _, err = client.RunInTransaction(ctx, func(tx *Transaction) error {
  909. attempts++
  910. if attempts > len(tt.causeConflict) {
  911. return fmt.Errorf("too many attempts. Got %d, max %d", attempts, len(tt.causeConflict))
  912. }
  913. var c Counter
  914. if err := tx.Get(key, &c); err != nil {
  915. return err
  916. }
  917. c.N++
  918. if _, err := tx.Put(key, &c); err != nil {
  919. return err
  920. }
  921. if tt.causeConflict[attempts-1] {
  922. c.N++
  923. if _, err := client.Put(ctx, key, &c); err != nil {
  924. return err
  925. }
  926. }
  927. return tt.retErr[attempts-1]
  928. }, MaxAttempts(i))
  929. // Check the error returned by RunInTransaction.
  930. if err != tt.wantErr {
  931. t.Errorf("%s: got err %v, want %v", tt.desc, err, tt.wantErr)
  932. continue
  933. }
  934. if err != nil {
  935. continue
  936. }
  937. // Check the final value of the counter.
  938. if err := client.Get(ctx, key, c); err != nil {
  939. t.Errorf("%s: client.Get: %v", tt.desc, err)
  940. continue
  941. }
  942. if c.N != tt.want {
  943. t.Errorf("%s: counter N=%d, want N=%d", tt.desc, c.N, tt.want)
  944. }
  945. }
  946. }
  947. func TestReadOnlyTransaction(t *testing.T) {
  948. if testing.Short() {
  949. t.Skip("Integration tests skipped in short mode")
  950. }
  951. ctx := context.Background()
  952. client := newClient(ctx, t, nil)
  953. defer client.Close()
  954. type value struct{ N int }
  955. // Put a value.
  956. const n = 5
  957. v := &value{N: n}
  958. key, err := client.Put(ctx, IncompleteKey("roTxn", nil), v)
  959. if err != nil {
  960. t.Fatal(err)
  961. }
  962. defer client.Delete(ctx, key)
  963. // Read it from a read-only transaction.
  964. _, err = client.RunInTransaction(ctx, func(tx *Transaction) error {
  965. if err := tx.Get(key, v); err != nil {
  966. return err
  967. }
  968. return nil
  969. }, ReadOnly)
  970. if err != nil {
  971. t.Fatal(err)
  972. }
  973. if v.N != n {
  974. t.Fatalf("got %d, want %d", v.N, n)
  975. }
  976. // Attempting to write from a read-only transaction is an error.
  977. _, err = client.RunInTransaction(ctx, func(tx *Transaction) error {
  978. if _, err := tx.Put(key, v); err != nil {
  979. return err
  980. }
  981. return nil
  982. }, ReadOnly)
  983. if err == nil {
  984. t.Fatal("got nil, want error")
  985. }
  986. }
  987. func TestNilPointers(t *testing.T) {
  988. ctx := context.Background()
  989. client := newTestClient(ctx, t)
  990. defer client.Close()
  991. type X struct {
  992. S string
  993. }
  994. src := []*X{{"zero"}, {"one"}}
  995. keys := []*Key{IncompleteKey("NilX", nil), IncompleteKey("NilX", nil)}
  996. keys, err := client.PutMulti(ctx, keys, src)
  997. if err != nil {
  998. t.Fatalf("PutMulti: %v", err)
  999. }
  1000. // It's okay to store into a slice of nil *X.
  1001. xs := make([]*X, 2)
  1002. if err := client.GetMulti(ctx, keys, xs); err != nil {
  1003. t.Errorf("GetMulti: %v", err)
  1004. } else if !testutil.Equal(xs, src) {
  1005. t.Errorf("GetMulti fetched %v, want %v", xs, src)
  1006. }
  1007. // It isn't okay to store into a single nil *X.
  1008. var x0 *X
  1009. if err, want := client.Get(ctx, keys[0], x0), ErrInvalidEntityType; err != want {
  1010. t.Errorf("Get: err %v; want %v", err, want)
  1011. }
  1012. // Test that deleting with duplicate keys work.
  1013. keys = append(keys, keys...)
  1014. if err := client.DeleteMulti(ctx, keys); err != nil {
  1015. t.Errorf("Delete: %v", err)
  1016. }
  1017. }
  1018. func TestNestedRepeatedElementNoIndex(t *testing.T) {
  1019. ctx := context.Background()
  1020. client := newTestClient(ctx, t)
  1021. defer client.Close()
  1022. type Inner struct {
  1023. Name string
  1024. Value string `datastore:",noindex"`
  1025. }
  1026. type Outer struct {
  1027. Config []Inner
  1028. }
  1029. m := &Outer{
  1030. Config: []Inner{
  1031. {Name: "short", Value: "a"},
  1032. {Name: "long", Value: strings.Repeat("a", 2000)},
  1033. },
  1034. }
  1035. key := NameKey("Nested", "Nested"+suffix, nil)
  1036. if _, err := client.Put(ctx, key, m); err != nil {
  1037. t.Fatalf("client.Put: %v", err)
  1038. }
  1039. if err := client.Delete(ctx, key); err != nil {
  1040. t.Fatalf("client.Delete: %v", err)
  1041. }
  1042. }
  1043. func TestPointerFields(t *testing.T) {
  1044. ctx := context.Background()
  1045. client := newTestClient(ctx, t)
  1046. defer client.Close()
  1047. want := populatedPointers()
  1048. key, err := client.Put(ctx, IncompleteKey("pointers", nil), want)
  1049. if err != nil {
  1050. t.Fatal(err)
  1051. }
  1052. var got Pointers
  1053. if err := client.Get(ctx, key, &got); err != nil {
  1054. t.Fatal(err)
  1055. }
  1056. if got.Pi == nil || *got.Pi != *want.Pi {
  1057. t.Errorf("Pi: got %v, want %v", got.Pi, *want.Pi)
  1058. }
  1059. if got.Ps == nil || *got.Ps != *want.Ps {
  1060. t.Errorf("Ps: got %v, want %v", got.Ps, *want.Ps)
  1061. }
  1062. if got.Pb == nil || *got.Pb != *want.Pb {
  1063. t.Errorf("Pb: got %v, want %v", got.Pb, *want.Pb)
  1064. }
  1065. if got.Pf == nil || *got.Pf != *want.Pf {
  1066. t.Errorf("Pf: got %v, want %v", got.Pf, *want.Pf)
  1067. }
  1068. if got.Pg == nil || *got.Pg != *want.Pg {
  1069. t.Errorf("Pg: got %v, want %v", got.Pg, *want.Pg)
  1070. }
  1071. if got.Pt == nil || !got.Pt.Equal(*want.Pt) {
  1072. t.Errorf("Pt: got %v, want %v", got.Pt, *want.Pt)
  1073. }
  1074. }
  1075. func TestMutate(t *testing.T) {
  1076. // test Client.Mutate
  1077. testMutate(t, func(ctx context.Context, client *Client, muts ...*Mutation) ([]*Key, error) {
  1078. return client.Mutate(ctx, muts...)
  1079. })
  1080. // test Transaction.Mutate
  1081. testMutate(t, func(ctx context.Context, client *Client, muts ...*Mutation) ([]*Key, error) {
  1082. var pkeys []*PendingKey
  1083. commit, err := client.RunInTransaction(ctx, func(tx *Transaction) error {
  1084. var err error
  1085. pkeys, err = tx.Mutate(muts...)
  1086. return err
  1087. })
  1088. if err != nil {
  1089. return nil, err
  1090. }
  1091. var keys []*Key
  1092. for _, pk := range pkeys {
  1093. keys = append(keys, commit.Key(pk))
  1094. }
  1095. return keys, nil
  1096. })
  1097. }
  1098. func testMutate(t *testing.T, mutate func(ctx context.Context, client *Client, muts ...*Mutation) ([]*Key, error)) {
  1099. ctx := context.Background()
  1100. client := newTestClient(ctx, t)
  1101. defer client.Close()
  1102. type T struct{ I int }
  1103. check := func(k *Key, want interface{}) {
  1104. var x T
  1105. err := client.Get(ctx, k, &x)
  1106. switch want := want.(type) {
  1107. case error:
  1108. if err != want {
  1109. t.Errorf("key %s: got error %v, want %v", k, err, want)
  1110. }
  1111. case int:
  1112. if err != nil {
  1113. t.Fatalf("key %s: %v", k, err)
  1114. }
  1115. if x.I != want {
  1116. t.Errorf("key %s: got %d, want %d", k, x.I, want)
  1117. }
  1118. default:
  1119. panic("check: bad arg")
  1120. }
  1121. }
  1122. keys, err := mutate(ctx, client,
  1123. NewInsert(IncompleteKey("t", nil), &T{1}),
  1124. NewUpsert(IncompleteKey("t", nil), &T{2}),
  1125. )
  1126. if err != nil {
  1127. t.Fatal(err)
  1128. }
  1129. check(keys[0], 1)
  1130. check(keys[1], 2)
  1131. _, err = mutate(ctx, client,
  1132. NewUpdate(keys[0], &T{3}),
  1133. NewDelete(keys[1]),
  1134. )
  1135. if err != nil {
  1136. t.Fatal(err)
  1137. }
  1138. check(keys[0], 3)
  1139. check(keys[1], ErrNoSuchEntity)
  1140. _, err = mutate(ctx, client, NewInsert(keys[0], &T{4}))
  1141. if got, want := status.Code(err), codes.AlreadyExists; got != want {
  1142. t.Errorf("Insert existing key: got %s, want %s", got, want)
  1143. }
  1144. _, err = mutate(ctx, client, NewUpdate(keys[1], &T{4}))
  1145. if got, want := status.Code(err), codes.NotFound; got != want {
  1146. t.Errorf("Update non-existing key: got %s, want %s", got, want)
  1147. }
  1148. }