github.com/m3db/m3@v1.5.0/src/m3ninx/doc/document_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 doc
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestDocumentMatcher(t *testing.T) {
    30  	tests := []struct {
    31  		name     string
    32  		l, r     Metadata
    33  		expected bool
    34  	}{
    35  		{
    36  			name:     "empty documents are equal",
    37  			l:        Metadata{},
    38  			r:        Metadata{},
    39  			expected: true,
    40  		},
    41  		{
    42  			name: "documents with the same fields in the same order are equal",
    43  			l: Metadata{
    44  				ID: []byte("831992"),
    45  				Fields: []Field{
    46  					Field{
    47  						Name:  []byte("apple"),
    48  						Value: []byte("red"),
    49  					},
    50  					Field{
    51  						Name:  []byte("banana"),
    52  						Value: []byte("yellow"),
    53  					},
    54  				},
    55  			},
    56  			r: Metadata{
    57  				ID: []byte("831992"),
    58  				Fields: []Field{
    59  					Field{
    60  						Name:  []byte("apple"),
    61  						Value: []byte("red"),
    62  					},
    63  					Field{
    64  						Name:  []byte("banana"),
    65  						Value: []byte("yellow"),
    66  					},
    67  				},
    68  			},
    69  			expected: true,
    70  		},
    71  		{
    72  			name: "documents with the same fields in different order are unequal",
    73  			l: Metadata{
    74  				ID: []byte("831992"),
    75  				Fields: []Field{
    76  					Field{
    77  						Name:  []byte("banana"),
    78  						Value: []byte("yellow"),
    79  					},
    80  					Field{
    81  						Name:  []byte("apple"),
    82  						Value: []byte("red"),
    83  					},
    84  				},
    85  			},
    86  			r: Metadata{
    87  				ID: []byte("831992"),
    88  				Fields: []Field{
    89  					Field{
    90  						Name:  []byte("apple"),
    91  						Value: []byte("red"),
    92  					},
    93  					Field{
    94  						Name:  []byte("banana"),
    95  						Value: []byte("yellow"),
    96  					},
    97  				},
    98  			},
    99  			expected: false,
   100  		},
   101  		{
   102  			name: "documents with different fields are unequal",
   103  			l: Metadata{
   104  				ID: []byte("831992"),
   105  				Fields: []Field{
   106  					Field{
   107  						Name:  []byte("apple"),
   108  						Value: []byte("red"),
   109  					},
   110  					Field{
   111  						Name:  []byte("banana"),
   112  						Value: []byte("yellow"),
   113  					},
   114  				},
   115  			},
   116  			r: Metadata{
   117  				ID: []byte("831992"),
   118  				Fields: []Field{
   119  					Field{
   120  						Name:  []byte("apple"),
   121  						Value: []byte("red"),
   122  					},
   123  					Field{
   124  						Name:  []byte("carrot"),
   125  						Value: []byte("orange"),
   126  					},
   127  				},
   128  			},
   129  			expected: false,
   130  		},
   131  		{
   132  			name: "documents with different IDs are unequal",
   133  			l: Metadata{
   134  				ID: []byte("831992"),
   135  				Fields: []Field{
   136  					Field{
   137  						Name:  []byte("apple"),
   138  						Value: []byte("red"),
   139  					},
   140  				},
   141  			},
   142  			r: Metadata{
   143  				ID: []byte("080292"),
   144  				Fields: []Field{
   145  					Field{
   146  						Name:  []byte("apple"),
   147  						Value: []byte("red"),
   148  					},
   149  				},
   150  			},
   151  			expected: false,
   152  		},
   153  	}
   154  
   155  	for _, test := range tests {
   156  		require.Equal(t, NewDocumentMatcher(test.l).Matches(test.r), test.expected, test.name)
   157  	}
   158  }