github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/idx/query_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 idx_test
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/idx"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestQueryMatcherTermQuery(t *testing.T) {
    32  	for _, tc := range []struct {
    33  		left     idx.Query
    34  		right    idx.Query
    35  		expected bool
    36  	}{
    37  		{
    38  			left:     idx.NewTermQuery([]byte("abc"), []byte("def")),
    39  			right:    idx.NewTermQuery([]byte("abc"), []byte("def")),
    40  			expected: true,
    41  		},
    42  		{
    43  			left:     idx.NewTermQuery([]byte("abc"), []byte("def")),
    44  			right:    idx.NewTermQuery([]byte("abc1"), []byte("def")),
    45  			expected: false,
    46  		},
    47  		{
    48  			left:     idx.NewTermQuery([]byte("abc"), []byte("def")),
    49  			right:    idx.NewTermQuery([]byte("abc"), []byte("def1")),
    50  			expected: false,
    51  		},
    52  	} {
    53  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.left).Matches(tc.right))
    54  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.right).Matches(tc.left))
    55  	}
    56  }
    57  
    58  func TestQueryMatcherRegexpQuery(t *testing.T) {
    59  	for _, tc := range []struct {
    60  		left     idx.Query
    61  		right    idx.Query
    62  		expected bool
    63  	}{
    64  		{
    65  			left:     mustCreateRegexpQuery(t, []byte("abc"), []byte("def")),
    66  			right:    mustCreateRegexpQuery(t, []byte("abc"), []byte("def")),
    67  			expected: true,
    68  		},
    69  		{
    70  			left:     mustCreateRegexpQuery(t, []byte("abc"), []byte("def")),
    71  			right:    mustCreateRegexpQuery(t, []byte("abc1"), []byte("def")),
    72  			expected: false,
    73  		},
    74  		{
    75  			left:     mustCreateRegexpQuery(t, []byte("abc"), []byte("def")),
    76  			right:    mustCreateRegexpQuery(t, []byte("abc"), []byte("def1")),
    77  			expected: false,
    78  		},
    79  	} {
    80  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.left).Matches(tc.right))
    81  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.right).Matches(tc.left))
    82  	}
    83  }
    84  
    85  func TestQueryMatcherNegationQuery(t *testing.T) {
    86  	for _, tc := range []struct {
    87  		left     idx.Query
    88  		right    idx.Query
    89  		expected bool
    90  	}{
    91  		{
    92  			left:     idx.NewNegationQuery(idx.NewTermQuery([]byte("abc"), []byte("def"))),
    93  			right:    idx.NewNegationQuery(idx.NewTermQuery([]byte("abc"), []byte("def"))),
    94  			expected: true,
    95  		},
    96  		{
    97  			left:     idx.NewNegationQuery(idx.NewTermQuery([]byte("abc"), []byte("def"))),
    98  			right:    idx.NewNegationQuery(idx.NewTermQuery([]byte("abc"), []byte("efg"))),
    99  			expected: false,
   100  		},
   101  	} {
   102  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.left).Matches(tc.right))
   103  		require.Equal(t, tc.expected, idx.NewQueryMatcher(tc.right).Matches(tc.left))
   104  	}
   105  }
   106  
   107  func TestQueryMatcherTermRegexpMismatch(t *testing.T) {
   108  	q0 := idx.NewTermQuery([]byte("abc"), []byte("def"))
   109  	q1, err := idx.NewRegexpQuery([]byte("abc"), []byte("def"))
   110  	require.NoError(t, err)
   111  	require.False(t, idx.NewQueryMatcher(q0).Matches(q1))
   112  }
   113  
   114  func TestQueryMatcherConjunctionQuery(t *testing.T) {
   115  	tq0 := idx.NewTermQuery([]byte("abc0"), []byte("def"))
   116  	tq1 := idx.NewTermQuery([]byte("abc1"), []byte("def"))
   117  	rq, err := idx.NewRegexpQuery([]byte("abc2"), []byte("def"))
   118  	require.NoError(t, err)
   119  	q := idx.NewConjunctionQuery(tq0, tq1, rq)
   120  	require.True(t, idx.NewQueryMatcher(q).Matches(q))
   121  }
   122  
   123  func TestQueryMatcherTermConjMismatch(t *testing.T) {
   124  	q0 := idx.NewTermQuery([]byte("abc"), []byte("def"))
   125  	tq1 := idx.NewTermQuery([]byte("abc1"), []byte("def"))
   126  	rq, err := idx.NewRegexpQuery([]byte("abc2"), []byte("def"))
   127  	require.NoError(t, err)
   128  	q1 := idx.NewConjunctionQuery(q0, tq1, rq)
   129  	require.False(t, idx.NewQueryMatcher(q0).Matches(q1))
   130  }
   131  
   132  func TestQueryMatcherDisjunctionQuery(t *testing.T) {
   133  	tq0 := idx.NewTermQuery([]byte("abc0"), []byte("def"))
   134  	tq1 := idx.NewTermQuery([]byte("abc1"), []byte("def"))
   135  	rq, err := idx.NewRegexpQuery([]byte("abc2"), []byte("def"))
   136  	require.NoError(t, err)
   137  	q := idx.NewDisjunctionQuery(tq0, tq1, rq)
   138  	require.True(t, idx.NewQueryMatcher(q).Matches(q))
   139  }
   140  
   141  func mustCreateRegexpQuery(t *testing.T, field, regexp []byte) idx.Query {
   142  	q, err := idx.NewRegexpQuery(field, regexp)
   143  	require.NoError(t, err)
   144  	return q
   145  }