github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/utils/metadata_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 utils
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/m3db/m3/src/query/block"
    27  	"github.com/m3db/m3/src/query/models"
    28  	"github.com/m3db/m3/src/query/test"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestFlattenMetadata(t *testing.T) {
    34  	meta := block.Metadata{Tags: test.TagSliceToTags([]models.Tag{
    35  		{Name: []byte("a"), Value: []byte("b")},
    36  		{Name: []byte("c"), Value: []byte("d")},
    37  	})}
    38  
    39  	seriesMetas := []block.SeriesMeta{
    40  		{Name: []byte("foo"),
    41  			Tags: test.TagSliceToTags([]models.Tag{
    42  				{Name: []byte("e"), Value: []byte("f")},
    43  			})},
    44  		{Name: []byte("bar"),
    45  			Tags: test.TagSliceToTags([]models.Tag{
    46  				{Name: []byte("g"), Value: []byte("h")}},
    47  			)},
    48  	}
    49  	flattened := FlattenMetadata(meta, seriesMetas)
    50  
    51  	expected := []block.SeriesMeta{
    52  		{Name: []byte("foo"), Tags: test.TagSliceToTags([]models.Tag{
    53  			{Name: []byte("a"), Value: []byte("b")},
    54  			{Name: []byte("c"), Value: []byte("d")},
    55  			{Name: []byte("e"), Value: []byte("f")},
    56  		})},
    57  		{Name: []byte("bar"), Tags: test.TagSliceToTags([]models.Tag{
    58  			{Name: []byte("a"), Value: []byte("b")},
    59  			{Name: []byte("c"), Value: []byte("d")},
    60  			{Name: []byte("g"), Value: []byte("h")},
    61  		})},
    62  	}
    63  
    64  	assert.Equal(t, expected, flattened)
    65  }
    66  
    67  var dedupeMetadataTests = []struct {
    68  	name               string
    69  	metaTags           []test.StringTags
    70  	expectedCommon     test.StringTags
    71  	expectedSeriesTags []test.StringTags
    72  }{
    73  	{
    74  		"empty metas",
    75  		[]test.StringTags{},
    76  		test.StringTags{},
    77  		[]test.StringTags{},
    78  	},
    79  	{
    80  		"single metas",
    81  		[]test.StringTags{{{N: "a", V: "b"}, {N: "c", V: "d"}}},
    82  		test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}},
    83  		[]test.StringTags{{}},
    84  	},
    85  	{
    86  		"one common tag, longer first",
    87  		[]test.StringTags{{{N: "a", V: "b"}, {N: "c", V: "d"}}, {{N: "a", V: "b"}}},
    88  		test.StringTags{{N: "a", V: "b"}},
    89  		[]test.StringTags{{{N: "c", V: "d"}}, {}},
    90  	},
    91  	{
    92  		"one common tag, longer second",
    93  		[]test.StringTags{{{N: "a", V: "b"}}, {{N: "a", V: "b"}, {N: "c", V: "d"}}},
    94  		test.StringTags{{N: "a", V: "b"}},
    95  		[]test.StringTags{{}, {{N: "c", V: "d"}}},
    96  	},
    97  	{
    98  		"two common tags",
    99  		[]test.StringTags{{{N: "a", V: "b"}, {N: "c", V: "d"}}, {{N: "a", V: "b"},
   100  			{N: "c", V: "d"}}, {{N: "a", V: "b"}, {N: "c", V: "d"}}},
   101  		test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}},
   102  		[]test.StringTags{{}, {}, {}},
   103  	},
   104  	{
   105  		"no common tags in one series",
   106  		[]test.StringTags{{{N: "a", V: "b"}, {N: "c", V: "d"}}, {{N: "a", V: "b"},
   107  			{N: "c", V: "d"}}, {{N: "a", V: "b*"}, {N: "c*", V: "d"}}},
   108  		test.StringTags{},
   109  		[]test.StringTags{{{N: "a", V: "b"}, {N: "c", V: "d"}}, {{N: "a", V: "b"},
   110  			{N: "c", V: "d"}}, {{N: "a", V: "b*"}, {N: "c*", V: "d"}}},
   111  	},
   112  }
   113  
   114  func TestDedupeMetadata(t *testing.T) {
   115  	for _, tt := range dedupeMetadataTests {
   116  		t.Run(tt.name, func(t *testing.T) {
   117  			metaTags := tt.metaTags
   118  			numSeries := len(metaTags)
   119  			seriesMetas := make([]block.SeriesMeta, numSeries)
   120  			for i, stringTags := range metaTags {
   121  				tags := test.StringTagsToTags(stringTags)
   122  				seriesMetas[i] = block.SeriesMeta{Tags: tags}
   123  			}
   124  
   125  			actual, actualSeriesMetas := DedupeMetadata(seriesMetas,
   126  				models.NewTagOptions())
   127  			exCommon := test.StringTagsToTags(tt.expectedCommon)
   128  			assert.Equal(t, exCommon, actual)
   129  
   130  			actualTags := make([]models.Tags, numSeries)
   131  			for i, metas := range actualSeriesMetas {
   132  				actualTags[i] = metas.Tags
   133  			}
   134  
   135  			exSeriesTags := test.StringTagsSliceToTagSlice(tt.expectedSeriesTags)
   136  			assert.Equal(t, exSeriesTags, actualTags)
   137  		})
   138  	}
   139  }