github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/index/batch_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 index_test
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/doc"
    27  	"github.com/m3db/m3/src/m3ninx/index"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestBatchMatcherAllowPartialReflexive(t *testing.T) {
    33  	a := index.Batch{
    34  		AllowPartialUpdates: true,
    35  	}
    36  	b := index.Batch{
    37  		AllowPartialUpdates: false,
    38  	}
    39  	require.False(t, index.NewBatchMatcher(a).Matches(b))
    40  	require.False(t, index.NewBatchMatcher(b).Matches(a))
    41  }
    42  
    43  func TestBatchMatcherLengthReflexive(t *testing.T) {
    44  	a := index.Batch{
    45  		Docs: []doc.Metadata{
    46  			{},
    47  		},
    48  	}
    49  	b := index.Batch{}
    50  	require.False(t, index.NewBatchMatcher(a).Matches(b))
    51  	require.False(t, index.NewBatchMatcher(b).Matches(a))
    52  }
    53  
    54  func TestBatchMatcherSameDoc(t *testing.T) {
    55  	testDoc := doc.Metadata{
    56  		ID: []byte("abc"),
    57  		Fields: []doc.Field{
    58  			{
    59  				Name:  []byte("foo"),
    60  				Value: []byte("bar"),
    61  			},
    62  		},
    63  	}
    64  	a := index.Batch{
    65  		Docs: []doc.Metadata{testDoc, testDoc},
    66  	}
    67  	b := index.Batch{
    68  		Docs: []doc.Metadata{testDoc, testDoc},
    69  	}
    70  	require.True(t, index.NewBatchMatcher(a).Matches(b))
    71  	require.True(t, index.NewBatchMatcher(b).Matches(a))
    72  }
    73  
    74  func TestBatchMatcherOrderMatters(t *testing.T) {
    75  	testDocA := doc.Metadata{
    76  		ID: []byte("abc"),
    77  		Fields: []doc.Field{
    78  			{
    79  				Name:  []byte("foo"),
    80  				Value: []byte("bar"),
    81  			},
    82  		},
    83  	}
    84  	testDocB := doc.Metadata{
    85  		ID: []byte("abc"),
    86  		Fields: []doc.Field{
    87  			{
    88  				Name:  []byte("bar"),
    89  				Value: []byte("foo"),
    90  			},
    91  		},
    92  	}
    93  	a := index.Batch{
    94  		Docs: []doc.Metadata{testDocA, testDocB},
    95  	}
    96  	b := index.Batch{
    97  		Docs: []doc.Metadata{testDocB, testDocA},
    98  	}
    99  	require.False(t, index.NewBatchMatcher(a).Matches(b))
   100  	require.False(t, index.NewBatchMatcher(b).Matches(a))
   101  }
   102  
   103  func TestBatchMatcherSameDocs(t *testing.T) {
   104  	testDocA := doc.Metadata{
   105  		ID: []byte("abc"),
   106  		Fields: []doc.Field{
   107  			{
   108  				Name:  []byte("foo"),
   109  				Value: []byte("bar"),
   110  			},
   111  		},
   112  	}
   113  	testDocB := doc.Metadata{
   114  		ID: []byte("abc"),
   115  		Fields: []doc.Field{
   116  			{
   117  				Name:  []byte("bar"),
   118  				Value: []byte("foo"),
   119  			},
   120  		},
   121  	}
   122  	a := index.Batch{
   123  		Docs: []doc.Metadata{testDocA, testDocB},
   124  	}
   125  	b := index.Batch{
   126  		Docs: []doc.Metadata{testDocA, testDocB},
   127  	}
   128  	require.True(t, index.NewBatchMatcher(a).Matches(b))
   129  	require.True(t, index.NewBatchMatcher(b).Matches(a))
   130  }
   131  
   132  func TestBatchMatcherDocFieldsDiffer(t *testing.T) {
   133  	testDocA := doc.Metadata{
   134  		ID: []byte("abc"),
   135  		Fields: []doc.Field{
   136  			{
   137  				Name:  []byte("foo"),
   138  				Value: []byte("bar"),
   139  			},
   140  		},
   141  	}
   142  	testDocB := doc.Metadata{
   143  		ID: []byte("abc"),
   144  		Fields: []doc.Field{
   145  			{
   146  				Name:  []byte("foo"),
   147  				Value: []byte("bar1"),
   148  			},
   149  		},
   150  	}
   151  	a := index.Batch{
   152  		Docs: []doc.Metadata{testDocA},
   153  	}
   154  	b := index.Batch{
   155  		Docs: []doc.Metadata{testDocB},
   156  	}
   157  	require.False(t, index.NewBatchMatcher(a).Matches(b))
   158  	require.False(t, index.NewBatchMatcher(b).Matches(a))
   159  }
   160  
   161  func TestBatchMatcherDocIDsDiffer(t *testing.T) {
   162  	testDocA := doc.Metadata{
   163  		ID: []byte("abc1"),
   164  		Fields: []doc.Field{
   165  			{
   166  				Name:  []byte("foo"),
   167  				Value: []byte("bar"),
   168  			},
   169  		},
   170  	}
   171  	testDocB := doc.Metadata{
   172  		ID: []byte("abc2"),
   173  		Fields: []doc.Field{
   174  			{
   175  				Name:  []byte("foo"),
   176  				Value: []byte("bar"),
   177  			},
   178  		},
   179  	}
   180  	a := index.Batch{
   181  		Docs: []doc.Metadata{testDocA},
   182  	}
   183  	b := index.Batch{
   184  		Docs: []doc.Metadata{testDocB},
   185  	}
   186  	require.False(t, index.NewBatchMatcher(a).Matches(b))
   187  	require.False(t, index.NewBatchMatcher(b).Matches(a))
   188  }