github.com/cornelk/go-cloud@v0.17.1/docstore/driver/util_test.go (about) 1 // Copyright 2019 The Go Cloud Development Kit Authors 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 // https://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 15 package driver 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/google/go-cmp/cmp/cmpopts" 23 ) 24 25 func TestSplitActions(t *testing.T) { 26 in := []*Action{ 27 {Kind: Get}, 28 {Kind: Get}, 29 {Kind: Put}, 30 {Kind: Update}, 31 {Kind: Get}, 32 {Kind: Create}, 33 } 34 35 for _, test := range []struct { 36 desc string 37 split func(a, b *Action) bool 38 want [][]*Action 39 }{ 40 { 41 "always false", 42 func(a, b *Action) bool { return false }, 43 [][]*Action{in}, 44 }, 45 { 46 "always true", 47 func(a, b *Action) bool { return true }, 48 [][]*Action{ 49 {{Kind: Get}}, 50 {{Kind: Get}}, 51 {{Kind: Put}}, 52 {{Kind: Update}}, 53 {{Kind: Get}}, 54 {{Kind: Create}}, 55 }, 56 }, 57 { 58 "Get vs. other", 59 func(a, b *Action) bool { return (a.Kind == Get) != (b.Kind == Get) }, 60 [][]*Action{ 61 {{Kind: Get}, {Kind: Get}}, 62 {{Kind: Put}, {Kind: Update}}, 63 {{Kind: Get}}, 64 {{Kind: Create}}, 65 }, 66 }, 67 } { 68 got := SplitActions(in, test.split) 69 if diff := cmp.Diff(got, test.want, cmpopts.IgnoreFields(Action{}, "Doc")); diff != "" { 70 t.Errorf("%s: %s", test.desc, diff) 71 } 72 } 73 } 74 75 func TestGroupActions(t *testing.T) { 76 for _, test := range []struct { 77 in []*Action 78 want [][]int // in the same order as the return args 79 }{ 80 { 81 in: []*Action{{Kind: Get, Key: 1}}, 82 want: [][]int{nil, {0}, nil, nil}, 83 }, 84 { 85 in: []*Action{ 86 {Kind: Get, Key: 1}, 87 {Kind: Get, Key: 3}, 88 {Kind: Put, Key: 1}, 89 {Kind: Replace, Key: 2}, 90 {Kind: Get, Key: 2}, 91 }, 92 want: [][]int{{0}, {1}, {2, 3}, {4}}, 93 }, 94 } { 95 got := make([][]*Action, 4) 96 got[0], got[1], got[2], got[3] = GroupActions(test.in) 97 want := make([][]*Action, 4) 98 for i, s := range test.want { 99 for _, x := range s { 100 want[i] = append(want[i], test.in[x]) 101 } 102 } 103 diff := cmp.Diff(got, want, 104 cmpopts.IgnoreUnexported(Document{}), 105 cmpopts.SortSlices(func(a1, a2 *Action) bool { 106 if a1.Kind != a2.Kind { 107 return a1.Kind < a2.Kind 108 } 109 return a1.Key.(int) < a2.Key.(int) 110 })) 111 if diff != "" { 112 113 t.Errorf("%v: %s", test.in, diff) 114 } 115 } 116 } 117 118 func (a *Action) String() string { // for TestGroupActions 119 return fmt.Sprintf("<%s %v>", a.Kind, a.Key) 120 } 121 122 func TestAsFunc(t *testing.T) { 123 x := 1 124 as := AsFunc(x) 125 126 var y int 127 if !as(&y) || y != 1 { 128 t.Errorf("*int: returned false or wrong value %d", y) 129 } 130 131 var z float64 132 for _, arg := range []interface{}{nil, y, &z} { 133 if as(arg) { 134 t.Errorf("%#v: got true, want false", arg) 135 } 136 } 137 } 138 139 func TestGroupByFieldPath(t *testing.T) { 140 for i, test := range []struct { 141 in []*Action 142 want [][]int // indexes into test.in 143 }{ 144 { 145 in: []*Action{{Index: 0}, {Index: 1}, {Index: 2}}, 146 want: [][]int{{0, 1, 2}}, 147 }, 148 { 149 in: []*Action{{Index: 0}, {Index: 1, FieldPaths: [][]string{{"a"}}}, {Index: 2}}, 150 want: [][]int{{0, 2}, {1}}, 151 }, 152 { 153 in: []*Action{ 154 {Index: 0, FieldPaths: [][]string{{"a", "b"}}}, 155 {Index: 1, FieldPaths: [][]string{{"a"}}}, 156 {Index: 2}, 157 {Index: 3, FieldPaths: [][]string{{"a"}, {"b"}}}, 158 }, 159 want: [][]int{{0}, {1}, {2}, {3}}, 160 }, 161 } { 162 got := GroupByFieldPath(test.in) 163 want := make([][]*Action, len(test.want)) 164 for i, s := range test.want { 165 want[i] = make([]*Action, len(s)) 166 for j, x := range s { 167 want[i][j] = test.in[x] 168 } 169 } 170 if diff := cmp.Diff(got, want, cmpopts.IgnoreUnexported(Document{})); diff != "" { 171 t.Errorf("#%d: %s", i, diff) 172 } 173 } 174 }