File: keycompat_test.go

package info (click to toggle)
golang-google-appengine 1.6.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,076 kB
  • sloc: sh: 43; makefile: 7
file content (89 lines) | stat: -rw-r--r-- 2,198 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2019 Google Inc. All Rights Reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package datastore

import (
	"reflect"
	"testing"
)

func TestKeyConversion(t *testing.T) {
	var tests = []struct {
		desc       string
		key        *Key
		encodedKey string
	}{
		{
			desc: "A control test for legacy to legacy key conversion int as the key",
			key: &Key{
				kind:  "Person",
				intID: 1,
				appID: "glibrary",
			},
			encodedKey: "aghnbGlicmFyeXIMCxIGUGVyc29uGAEM",
		},
		{
			desc: "A control test for legacy to legacy key conversion string as the key",
			key: &Key{
				kind:     "Graph",
				stringID: "graph:7-day-active",
				appID:    "glibrary",
			},
			encodedKey: "aghnbGlicmFyeXIdCxIFR3JhcGgiEmdyYXBoOjctZGF5LWFjdGl2ZQw",
		},

		// These are keys encoded with cloud.google.com/go/datastore
		// Standard int as the key
		{
			desc: "Convert new key format to old key with int id",
			key: &Key{
				kind:  "WordIndex",
				intID: 1033,
				appID: "glibrary",
			},
			encodedKey: "Eg4KCVdvcmRJbmRleBCJCA",
		},
		// These are keys encoded with cloud.google.com/go/datastore
		// Standard string
		{
			desc: "Convert new key format to old key with string id",
			key: &Key{
				kind:     "WordIndex",
				stringID: "IAmAnID",
				appID:    "glibrary",
			},
			encodedKey: "EhQKCVdvcmRJbmRleBoHSUFtQW5JRA",
		},

		// These are keys encoded with cloud.google.com/go/datastore
		// ID String with parent as string
		{
			desc: "Convert new key format to old key with string id with a parent",
			key: &Key{
				kind:     "WordIndex",
				stringID: "IAmAnID",
				appID:    "glibrary",
				parent: &Key{
					kind:     "LetterIndex",
					stringID: "IAmAnotherID",
					appID:    "glibrary",
				},
			},
			encodedKey: "EhsKC0xldHRlckluZGV4GgxJQW1Bbm90aGVySUQSFAoJV29yZEluZGV4GgdJQW1BbklE",
		},
	}

	// Simulate the key converter enablement
	keyConversion.appID = "glibrary"
	for _, tc := range tests {
		dk, err := DecodeKey(tc.encodedKey)
		if err != nil {
			t.Fatalf("DecodeKey: %v", err)
		}
		if !reflect.DeepEqual(dk, tc.key) {
			t.Errorf("%s: got %+v, want %+v", tc.desc, dk, tc.key)
		}
	}
}