github.com/m3db/m3@v1.5.0/src/m3ninx/search/proptest/util.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 proptest 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/stretchr/testify/require" 28 29 "github.com/m3db/m3/src/m3ninx/doc" 30 idxdocs "github.com/m3db/m3/src/m3ninx/index/segment/fst/encoding/docs" 31 ) 32 33 type documentIteratorMatcher struct { 34 expectedDocs map[string]doc.Document 35 t *testing.T 36 } 37 38 func newDocumentIteratorMatcher(t *testing.T, docs ...doc.Document) (*documentIteratorMatcher, error) { 39 docMap := make(map[string]doc.Document, len(docs)) 40 for _, d := range docs { 41 rawID, err := idxdocs.ReadIDFromDocument(d) 42 id := string(rawID) 43 require.NoError(t, err) 44 if _, ok := docMap[id]; ok { 45 return nil, fmt.Errorf("received document with duplicate id: %v", d) 46 } 47 docMap[id] = d 48 } 49 return &documentIteratorMatcher{ 50 expectedDocs: docMap, 51 t: t, 52 }, nil 53 } 54 55 // Matches returns whether the provided iterator matches the collection of provided docs. 56 func (m *documentIteratorMatcher) Matches(i doc.Iterator) error { 57 pendingDocIDs := make(map[string]doc.Document, len(m.expectedDocs)) 58 for id := range m.expectedDocs { 59 pendingDocIDs[id] = m.expectedDocs[id] 60 } 61 for i.Next() { 62 d := i.Current() 63 rawID, err := idxdocs.ReadIDFromDocument(d) 64 require.NoError(m.t, err) 65 id := string(rawID) 66 expectedDoc, ok := m.expectedDocs[id] 67 if !ok { 68 return fmt.Errorf("received un-expected document: %+v", d) 69 } 70 if !m.compareDocs(expectedDoc, d) { 71 return fmt.Errorf("received document: %+v did not match expected doc %+v", d, expectedDoc) 72 } 73 delete(pendingDocIDs, id) 74 } 75 if err := i.Err(); err != nil { 76 return fmt.Errorf("unexpected iterator error: %v", err) 77 } 78 if err := i.Close(); err != nil { 79 return fmt.Errorf("unexpected iterator close error: %v", err) 80 } 81 if len(pendingDocIDs) > 0 { 82 return fmt.Errorf("did not receive docs: %+v", pendingDocIDs) 83 } 84 return nil 85 } 86 87 func (m *documentIteratorMatcher) compareDocs(d1 doc.Document, d2 doc.Document) bool { 88 docReader := idxdocs.NewEncodedDocumentReader() 89 d1Metadata, err := idxdocs.MetadataFromDocument(d1, docReader) 90 require.NoError(m.t, err) 91 92 d2Metadata, err := idxdocs.MetadataFromDocument(d2, docReader) 93 require.NoError(m.t, err) 94 95 return d1Metadata.Equal(d2Metadata) 96 }