go.mercari.io/datastore@v1.8.2/testsuite/key.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"go.mercari.io/datastore"
     8  )
     9  
    10  func keyEqual(ctx context.Context, t *testing.T, client datastore.Client) {
    11  	defer func() {
    12  		err := client.Close()
    13  		if err != nil {
    14  			t.Fatal(err)
    15  		}
    16  	}()
    17  
    18  	specs := []struct {
    19  		A      datastore.Key
    20  		B      datastore.Key
    21  		Result bool
    22  	}{
    23  		{
    24  			client.IncompleteKey("A", nil),
    25  			client.IncompleteKey("A", nil),
    26  			true,
    27  		},
    28  		{
    29  			client.IncompleteKey("A", nil),
    30  			client.IncompleteKey("B", nil),
    31  			false,
    32  		},
    33  		{
    34  			client.IDKey("A", 1, nil),
    35  			client.IDKey("A", 1, nil),
    36  			true,
    37  		},
    38  		{
    39  			client.IDKey("A", 1, nil),
    40  			client.IDKey("A", 2, nil),
    41  			false,
    42  		},
    43  		{
    44  			client.NameKey("A", "a", nil),
    45  			client.NameKey("A", "a", nil),
    46  			true,
    47  		},
    48  		{
    49  			client.NameKey("A", "a", nil),
    50  			client.NameKey("A", "b", nil),
    51  			false,
    52  		},
    53  		{
    54  			client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
    55  			client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
    56  			true,
    57  		},
    58  		{
    59  			client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
    60  			client.NameKey("A", "a", client.IDKey("Parent", 2, nil)),
    61  			false,
    62  		},
    63  		{
    64  			client.NameKey("A", "a", nil),
    65  			client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
    66  			false,
    67  		},
    68  		{
    69  			client.NameKey("A", "a", client.IDKey("Parent", 1, nil)),
    70  			client.NameKey("A", "a", nil),
    71  			false,
    72  		},
    73  	}
    74  
    75  	for idx, spec := range specs {
    76  		if v := spec.A.Equal(spec.B); v != spec.Result {
    77  			t.Errorf("unexpected: #%d %v", idx, v)
    78  		}
    79  	}
    80  }
    81  
    82  func keyIncomplete(ctx context.Context, t *testing.T, client datastore.Client) {
    83  	if v := client.IncompleteKey("A", nil).Incomplete(); !v {
    84  		t.Errorf("unexpected: %v", v)
    85  	}
    86  	if v := client.NameKey("A", "a", nil).Incomplete(); v {
    87  		t.Errorf("unexpected: %v", v)
    88  	}
    89  	if v := client.IDKey("A", 1, nil).Incomplete(); v {
    90  		t.Errorf("unexpected: %v", v)
    91  	}
    92  }
    93  
    94  func keyPutAndGet(ctx context.Context, t *testing.T, client datastore.Client) {
    95  	defer func() {
    96  		err := client.Close()
    97  		if err != nil {
    98  			t.Fatal(err)
    99  		}
   100  	}()
   101  
   102  	type Data struct {
   103  		Key     datastore.Key
   104  		Keys    []datastore.Key
   105  		NilKey  datastore.Key
   106  		NilKeys []datastore.Key
   107  	}
   108  
   109  	obj := &Data{
   110  		Key:     client.IDKey("A", 1, nil),
   111  		Keys:    []datastore.Key{client.IDKey("A", 2, nil), client.IDKey("A", 3, nil)},
   112  		NilKey:  nil,
   113  		NilKeys: []datastore.Key{nil, nil},
   114  	}
   115  
   116  	key, err := client.Put(ctx, client.IDKey("Data", 100, nil), obj)
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  
   121  	obj = &Data{}
   122  	err = client.Get(ctx, key, obj)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	if v := obj.Key.Kind(); v != "A" {
   128  		t.Errorf("unexpected: %v", v)
   129  	}
   130  	if v := obj.Key.ID(); v != 1 {
   131  		t.Errorf("unexpected: %v", v)
   132  	}
   133  
   134  	if v := len(obj.Keys); v != 2 {
   135  		t.Errorf("unexpected: %v", v)
   136  	}
   137  	if v := obj.Keys[0].Kind(); v != "A" {
   138  		t.Errorf("unexpected: %v", v)
   139  	}
   140  	if v := obj.Keys[0].ID(); v != 2 {
   141  		t.Errorf("unexpected: %v", v)
   142  	}
   143  	if v := obj.Keys[1].Kind(); v != "A" {
   144  		t.Errorf("unexpected: %v", v)
   145  	}
   146  	if v := obj.Keys[1].ID(); v != 3 {
   147  		t.Errorf("unexpected: %v", v)
   148  	}
   149  
   150  	if v := obj.NilKey; v != nil {
   151  		t.Errorf("unexpected: %v", v)
   152  	}
   153  
   154  	if v := len(obj.NilKeys); v != 2 {
   155  		t.Errorf("unexpected: %v", v)
   156  	}
   157  	if v := obj.NilKeys[0]; v != nil {
   158  		t.Errorf("unexpected: %v", v)
   159  	}
   160  	if v := obj.NilKeys[1]; v != nil {
   161  		t.Errorf("unexpected: %v", v)
   162  	}
   163  }