github.com/m3db/m3@v1.5.0/src/m3ninx/search/query/regexp_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 TestRegexpQuery(t *testing.T) { 32 tests := []struct { 33 name string 34 field, regexp []byte 35 expectErr bool 36 }{ 37 { 38 name: "valid field and regexp should not return an error", 39 field: []byte("fruit"), 40 regexp: []byte(".*ple"), 41 expectErr: false, 42 }, 43 { 44 name: "invalid regexp should return an error", 45 field: []byte("fruit"), 46 regexp: []byte("(*]ple"), 47 expectErr: true, 48 }, 49 } 50 51 for _, test := range tests { 52 t.Run(test.name, func(t *testing.T) { 53 q, err := NewRegexpQuery(test.field, test.regexp) 54 55 if test.expectErr { 56 require.Error(t, err) 57 return 58 } 59 require.NoError(t, err) 60 61 _, err = q.Searcher() 62 require.NoError(t, err) 63 }) 64 } 65 } 66 67 func TestRegexpQueryEqual(t *testing.T) { 68 tests := []struct { 69 name string 70 left, right search.Query 71 expected bool 72 }{ 73 { 74 name: "same field and regexp", 75 left: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 76 right: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 77 expected: true, 78 }, 79 { 80 name: "singular conjunction query", 81 left: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 82 right: NewConjunctionQuery([]search.Query{ 83 MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 84 }), 85 expected: true, 86 }, 87 { 88 name: "singular disjunction query", 89 left: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 90 right: NewDisjunctionQuery([]search.Query{ 91 MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 92 }), 93 expected: true, 94 }, 95 { 96 name: "different field", 97 left: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 98 right: MustCreateRegexpQuery([]byte("food"), []byte(".*ple")), 99 expected: false, 100 }, 101 { 102 name: "different regexp", 103 left: MustCreateRegexpQuery([]byte("fruit"), []byte(".*ple")), 104 right: MustCreateRegexpQuery([]byte("fruit"), []byte(".*na")), 105 expected: false, 106 }, 107 } 108 109 for _, test := range tests { 110 t.Run(test.name, func(t *testing.T) { 111 require.Equal(t, test.expected, test.left.Equal(test.right)) 112 }) 113 } 114 }