github.com/m3db/m3@v1.5.0/src/m3ninx/index/metadata_iterator_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 index
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/golang/mock/gomock"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/m3db/m3/src/m3ninx/doc"
    30  	"github.com/m3db/m3/src/m3ninx/postings"
    31  )
    32  
    33  func TestIterator(t *testing.T) {
    34  	mockCtrl := gomock.NewController(t)
    35  	defer mockCtrl.Finish()
    36  
    37  	docWithIds := []struct {
    38  		id  postings.ID
    39  		doc doc.Metadata
    40  	}{
    41  		{
    42  			id: 42,
    43  			doc: doc.Metadata{
    44  				Fields: []doc.Field{
    45  					{
    46  						Name:  []byte("apple"),
    47  						Value: []byte("red"),
    48  					},
    49  				},
    50  			},
    51  		},
    52  		{
    53  			id: 53,
    54  			doc: doc.Metadata{
    55  				Fields: []doc.Field{
    56  					{
    57  						Name:  []byte("banana"),
    58  						Value: []byte("yellow"),
    59  					},
    60  				},
    61  			},
    62  		},
    63  		{
    64  			id: 81,
    65  			doc: doc.Metadata{
    66  				Fields: []doc.Field{
    67  					{
    68  						Name:  []byte("carrot"),
    69  						Value: []byte("orange"),
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  
    76  	retriever := NewMockMetadataRetriever(mockCtrl)
    77  	gomock.InOrder(
    78  		retriever.EXPECT().Metadata(docWithIds[0].id).Return(docWithIds[0].doc, nil),
    79  		retriever.EXPECT().Metadata(docWithIds[1].id).Return(docWithIds[1].doc, nil),
    80  		retriever.EXPECT().Metadata(docWithIds[2].id).Return(docWithIds[2].doc, nil),
    81  	)
    82  
    83  	postingsIter := postings.NewMockIterator(mockCtrl)
    84  	gomock.InOrder(
    85  		postingsIter.EXPECT().Next().Return(true),
    86  		postingsIter.EXPECT().Current().Return(docWithIds[0].id),
    87  		postingsIter.EXPECT().Next().Return(true),
    88  		postingsIter.EXPECT().Current().Return(docWithIds[1].id),
    89  		postingsIter.EXPECT().Next().Return(true),
    90  		postingsIter.EXPECT().Current().Return(docWithIds[2].id),
    91  		postingsIter.EXPECT().Next().Return(false),
    92  		postingsIter.EXPECT().Close().Return(nil),
    93  	)
    94  
    95  	it := NewIDDocIterator(retriever, postingsIter)
    96  
    97  	require.True(t, it.Next())
    98  	require.Equal(t, docWithIds[0].doc, it.Current())
    99  	require.Equal(t, docWithIds[0].id, it.PostingsID())
   100  	require.True(t, it.Next())
   101  	require.Equal(t, docWithIds[1].doc, it.Current())
   102  	require.Equal(t, docWithIds[1].id, it.PostingsID())
   103  	require.True(t, it.Next())
   104  	require.Equal(t, docWithIds[2].doc, it.Current())
   105  	require.Equal(t, docWithIds[2].id, it.PostingsID())
   106  
   107  	require.False(t, it.Next())
   108  	require.NoError(t, it.Err())
   109  
   110  	require.NoError(t, it.Close())
   111  }