github.com/thiagoyeds/go-cloud@v0.26.0/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 in: []*Action{{Kind: Create}, {Kind: Create}, {Kind: Create}}, 96 want: [][]int{nil, nil, {0, 1, 2}, nil}, 97 }, 98 } { 99 got := make([][]*Action, 4) 100 got[0], got[1], got[2], got[3] = GroupActions(test.in) 101 want := make([][]*Action, 4) 102 for i, s := range test.want { 103 for _, x := range s { 104 want[i] = append(want[i], test.in[x]) 105 } 106 } 107 diff := cmp.Diff(got, want, 108 cmpopts.IgnoreUnexported(Document{}), 109 cmpopts.SortSlices(func(a1, a2 *Action) bool { 110 if a1.Kind != a2.Kind { 111 return a1.Kind < a2.Kind 112 } 113 a1k, _ := a1.Key.(int) 114 a2k, _ := a2.Key.(int) 115 return a1k < a2k 116 })) 117 if diff != "" { 118 t.Errorf("%v: %s", test.in, diff) 119 } 120 } 121 } 122 123 func (a *Action) String() string { // for TestGroupActions 124 return fmt.Sprintf("<%s %v>", a.Kind, a.Key) 125 } 126 127 func TestAsFunc(t *testing.T) { 128 x := 1 129 as := AsFunc(x) 130 131 var y int 132 if !as(&y) || y != 1 { 133 t.Errorf("*int: returned false or wrong value %d", y) 134 } 135 136 var z float64 137 for _, arg := range []interface{}{nil, y, &z} { 138 if as(arg) { 139 t.Errorf("%#v: got true, want false", arg) 140 } 141 } 142 } 143 144 func TestGroupByFieldPath(t *testing.T) { 145 for i, test := range []struct { 146 in []*Action 147 want [][]int // indexes into test.in 148 }{ 149 { 150 in: []*Action{{Index: 0}, {Index: 1}, {Index: 2}}, 151 want: [][]int{{0, 1, 2}}, 152 }, 153 { 154 in: []*Action{{Index: 0}, {Index: 1, FieldPaths: [][]string{{"a"}}}, {Index: 2}}, 155 want: [][]int{{0, 2}, {1}}, 156 }, 157 { 158 in: []*Action{ 159 {Index: 0, FieldPaths: [][]string{{"a", "b"}}}, 160 {Index: 1, FieldPaths: [][]string{{"a"}}}, 161 {Index: 2}, 162 {Index: 3, FieldPaths: [][]string{{"a"}, {"b"}}}, 163 }, 164 want: [][]int{{0}, {1}, {2}, {3}}, 165 }, 166 } { 167 got := GroupByFieldPath(test.in) 168 want := make([][]*Action, len(test.want)) 169 for i, s := range test.want { 170 want[i] = make([]*Action, len(s)) 171 for j, x := range s { 172 want[i][j] = test.in[x] 173 } 174 } 175 if diff := cmp.Diff(got, want, cmpopts.IgnoreUnexported(Document{})); diff != "" { 176 t.Errorf("#%d: %s", i, diff) 177 } 178 } 179 }