1// Copyright 2019 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
15package datastore
16
17import (
18	"testing"
19)
20
21func TestKeyConversion(t *testing.T) {
22	tests := []struct {
23		desc string
24
25		gaeKey      string
26		wantEncoded string
27		wantName    string
28	}{
29		{
30			desc:        "A GAE key with a Name ID.", // Generated by: https://play.golang.org/p/00fNVEKSmBX
31			gaeKey:      "agdmb28tYXBwchwLEgh0ZXN0S2luZCIOZm9vYmFyc3RyaW5naWQM",
32			wantEncoded: "EhoKCHRlc3RLaW5kGg5mb29iYXJzdHJpbmdpZA",
33			wantName:    "foobarstringid",
34		},
35		{
36			desc:        "Another GAE key with a Name ID",
37			gaeKey:      "ag1wfnBlbmRvLWV4YW1wcjkLEg90ZXN0U3RyaW5nS2V5czIiJDdlNDA2YTNkLTUyNTItMTFlOS05MTMyLTdmNjUyMzg3YThlYgw",
38			wantEncoded: "EjcKD3Rlc3RTdHJpbmdLZXlzMhokN2U0MDZhM2QtNTI1Mi0xMWU5LTkxMzItN2Y2NTIzODdhOGVi",
39			wantName:    "7e406a3d-5252-11e9-9132-7f652387a8eb",
40		},
41	}
42
43	for _, tc := range tests {
44		k, err := DecodeKey(tc.gaeKey)
45		if err != nil {
46			t.Fatalf("DecodeKey(%s): %v", tc.desc, err)
47		}
48		if k.Name != tc.wantName {
49			t.Errorf("Name(%s): got %+v, want %+v", tc.desc, k.Name, tc.wantName)
50		}
51		if enc := k.Encode(); enc != tc.wantEncoded {
52			t.Errorf("Encode(%s): got %+v, want %+v", tc.desc, enc, tc.wantEncoded)
53		}
54	}
55}
56