github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/query/conjunction_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 query
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/search"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestConjunctionQuery(t *testing.T) {
    32  	tests := []struct {
    33  		name    string
    34  		queries []search.Query
    35  	}{
    36  		{
    37  			name: "no queries provided",
    38  		},
    39  		{
    40  			name: "a single query provided",
    41  			queries: []search.Query{
    42  				NewTermQuery([]byte("fruit"), []byte("apple")),
    43  			},
    44  		},
    45  		{
    46  			name: "multiple queries provided",
    47  			queries: []search.Query{
    48  				NewTermQuery([]byte("fruit"), []byte("apple")),
    49  				NewTermQuery([]byte("vegetable"), []byte("carrot")),
    50  			},
    51  		},
    52  		{
    53  			name: "multiple queries including negations",
    54  			queries: []search.Query{
    55  				NewTermQuery([]byte("fruit"), []byte("apple")),
    56  				NewTermQuery([]byte("vegetable"), []byte("carrot")),
    57  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("banana"))),
    58  			},
    59  		},
    60  		{
    61  			name: "single negation query",
    62  			queries: []search.Query{
    63  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("banana"))),
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		t.Run(test.name, func(t *testing.T) {
    70  			q := NewConjunctionQuery(test.queries)
    71  			_, err := q.Searcher()
    72  			require.NoError(t, err)
    73  		})
    74  	}
    75  }
    76  
    77  func TestConjunctionQueryEqual(t *testing.T) {
    78  	tests := []struct {
    79  		name        string
    80  		left, right search.Query
    81  		expected    bool
    82  	}{
    83  		{
    84  			name:     "empty queries",
    85  			left:     NewConjunctionQuery(nil),
    86  			right:    NewConjunctionQuery(nil),
    87  			expected: true,
    88  		},
    89  		{
    90  			name: "equal queries",
    91  			left: NewConjunctionQuery([]search.Query{
    92  				NewTermQuery([]byte("fruit"), []byte("apple")),
    93  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("banana"))),
    94  			}),
    95  			right: NewConjunctionQuery([]search.Query{
    96  				NewTermQuery([]byte("fruit"), []byte("apple")),
    97  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("banana"))),
    98  			}),
    99  			expected: true,
   100  		},
   101  		{
   102  			name: "single query",
   103  			left: NewConjunctionQuery([]search.Query{
   104  				NewTermQuery([]byte("fruit"), []byte("apple")),
   105  			}),
   106  			right:    NewTermQuery([]byte("fruit"), []byte("apple")),
   107  			expected: true,
   108  		},
   109  		{
   110  			name: "different order",
   111  			left: NewConjunctionQuery([]search.Query{
   112  				NewTermQuery([]byte("fruit"), []byte("apple")),
   113  				NewTermQuery([]byte("fruit"), []byte("banana")),
   114  			}),
   115  			right: NewConjunctionQuery([]search.Query{
   116  				NewTermQuery([]byte("fruit"), []byte("banana")),
   117  				NewTermQuery([]byte("fruit"), []byte("apple")),
   118  			}),
   119  			expected: true,
   120  		},
   121  	}
   122  
   123  	for _, test := range tests {
   124  		t.Run(test.name, func(t *testing.T) {
   125  			require.Equal(t, test.expected, test.left.Equal(test.right))
   126  		})
   127  	}
   128  }