github.com/m3db/m3@v1.5.0/src/m3ninx/search/query/codec_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 TestMarshal(t *testing.T) {
    32  	tests := []struct {
    33  		name  string
    34  		query search.Query
    35  	}{
    36  		{
    37  			name:  "all query",
    38  			query: NewAllQuery(),
    39  		},
    40  		{
    41  			name:  "field query",
    42  			query: NewFieldQuery([]byte("fruit")),
    43  		},
    44  		{
    45  			name:  "term query",
    46  			query: NewTermQuery([]byte("fruit"), []byte("apple")),
    47  		},
    48  		{
    49  			name:  "regexp query",
    50  			query: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")),
    51  		},
    52  		{
    53  			name:  "negation query",
    54  			query: NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("apple"))),
    55  		},
    56  		{
    57  			name: "disjunction query",
    58  			query: NewDisjunctionQuery([]search.Query{
    59  				NewFieldQuery([]byte("fruit")),
    60  				NewTermQuery([]byte("fruit"), []byte("apple")),
    61  				NewConjunctionQuery([]search.Query{
    62  					NewTermQuery([]byte("fruit"), []byte("banana")),
    63  				}),
    64  				NewDisjunctionQuery([]search.Query{
    65  					NewTermQuery([]byte("fruit"), []byte("orange")),
    66  				}),
    67  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("pear"))),
    68  			}),
    69  		},
    70  		{
    71  			name: "conjunction query",
    72  			query: NewConjunctionQuery([]search.Query{
    73  				NewFieldQuery([]byte("fruit")),
    74  				NewTermQuery([]byte("fruit"), []byte("apple")),
    75  				NewConjunctionQuery([]search.Query{
    76  					NewTermQuery([]byte("fruit"), []byte("banana")),
    77  				}),
    78  				NewDisjunctionQuery([]search.Query{
    79  					NewTermQuery([]byte("fruit"), []byte("orange")),
    80  				}),
    81  				NewNegationQuery(NewTermQuery([]byte("fruit"), []byte("pear"))),
    82  			}),
    83  		},
    84  	}
    85  
    86  	for _, test := range tests {
    87  		t.Run(test.name, func(t *testing.T) {
    88  			data, err := Marshal(test.query)
    89  			require.NoError(t, err)
    90  
    91  			cpy, err := Unmarshal(data)
    92  			require.NoError(t, err)
    93  
    94  			require.True(t, test.query.Equal(cpy))
    95  		})
    96  	}
    97  }
    98  
    99  func TestMarshalError(t *testing.T) {
   100  	tests := []struct {
   101  		name  string
   102  		query search.Query
   103  	}{
   104  		{
   105  			name:  "nil query",
   106  			query: nil,
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			_, err := Marshal(test.query)
   113  			require.Error(t, err)
   114  		})
   115  	}
   116  }