github.com/m3db/m3@v1.5.0/src/dbnode/client/iterator_matcher_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package client_test 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/dbnode/client" 27 "github.com/m3db/m3/src/x/ident" 28 29 "github.com/golang/mock/gomock" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestTaggedIDsIteratorMatcherMatches(t *testing.T) { 34 ctrl := gomock.NewController(t) 35 defer ctrl.Finish() 36 37 mIter := client.NewMockTaggedIDsIterator(ctrl) 38 gomock.InOrder( 39 mIter.EXPECT().Next().Return(true), 40 mIter.EXPECT().Current().Return( 41 ident.StringID("ns"), 42 ident.StringID("id0"), 43 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 44 ), 45 mIter.EXPECT().Next().Return(true), 46 mIter.EXPECT().Current().Return( 47 ident.StringID("ns"), 48 ident.StringID("id1"), 49 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 50 ), 51 mIter.EXPECT().Next().Return(false), 52 mIter.EXPECT().Next().Return(false), 53 mIter.EXPECT().Err().Return(nil), 54 ) 55 56 m, err := client.NewTaggedIDsIteratorMatcher(client.TaggedIDsIteratorMatcherOption{ 57 Namespace: "ns", 58 ID: "id0", 59 Tags: []string{"fgh", "ijk"}, 60 }, client.TaggedIDsIteratorMatcherOption{ 61 Namespace: "ns", 62 ID: "id1", 63 Tags: []string{"fgh", "ijk"}, 64 }) 65 require.NoError(t, err) 66 require.True(t, m.Matches(mIter)) 67 } 68 69 func TestTaggedIDsIteratorMatcherDoesNotMatchTooFew(t *testing.T) { 70 ctrl := gomock.NewController(t) 71 defer ctrl.Finish() 72 73 mIter := client.NewMockTaggedIDsIterator(ctrl) 74 gomock.InOrder( 75 mIter.EXPECT().Next().Return(true), 76 mIter.EXPECT().Current().Return( 77 ident.StringID("ns"), 78 ident.StringID("id0"), 79 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 80 ), 81 mIter.EXPECT().Next().Return(false), 82 mIter.EXPECT().Next().Return(false), 83 mIter.EXPECT().Err().Return(nil), 84 ) 85 86 m, err := client.NewTaggedIDsIteratorMatcher(client.TaggedIDsIteratorMatcherOption{ 87 Namespace: "ns", 88 ID: "id0", 89 Tags: []string{"fgh", "ijk"}, 90 }, client.TaggedIDsIteratorMatcherOption{ 91 Namespace: "ns", 92 ID: "id1", 93 Tags: []string{"fgh", "ijk"}, 94 }) 95 require.NoError(t, err) 96 require.False(t, m.Matches(mIter)) 97 } 98 99 func TestTaggedIDsIteratorMatcherDoesNotMatchTooMany(t *testing.T) { 100 ctrl := gomock.NewController(t) 101 defer ctrl.Finish() 102 103 mIter := client.NewMockTaggedIDsIterator(ctrl) 104 gomock.InOrder( 105 mIter.EXPECT().Next().Return(true), 106 mIter.EXPECT().Current().Return( 107 ident.StringID("ns"), 108 ident.StringID("id0"), 109 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 110 ), 111 mIter.EXPECT().Next().Return(true), 112 mIter.EXPECT().Current().Return( 113 ident.StringID("ns"), 114 ident.StringID("id1"), 115 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 116 ), 117 mIter.EXPECT().Next().Return(true), 118 mIter.EXPECT().Current().Return( 119 ident.StringID("ns"), 120 ident.StringID("id2"), 121 ident.NewTagsIterator(ident.NewTags(ident.StringTag("fgh", "ijk"))), 122 ), 123 ) 124 125 m, err := client.NewTaggedIDsIteratorMatcher(client.TaggedIDsIteratorMatcherOption{ 126 Namespace: "ns", 127 ID: "id0", 128 Tags: []string{"fgh", "ijk"}, 129 }, client.TaggedIDsIteratorMatcherOption{ 130 Namespace: "ns", 131 ID: "id1", 132 Tags: []string{"fgh", "ijk"}, 133 }) 134 require.NoError(t, err) 135 require.False(t, m.Matches(mIter)) 136 } 137 138 func TestTaggedIDsIteratorMatcherDuplicateIDs(t *testing.T) { 139 _, err := client.NewTaggedIDsIteratorMatcher(client.TaggedIDsIteratorMatcherOption{ 140 ID: "abc", 141 Namespace: "cde", 142 Tags: []string{"fgh", "ijk"}, 143 }, client.TaggedIDsIteratorMatcherOption{ 144 ID: "abc", 145 Namespace: "cde", 146 Tags: []string{"fgh", "ijk"}, 147 }) 148 require.Error(t, err) 149 }