github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/map_test.go (about) 1 // Copyright (c) 2017 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 namespace 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/x/ident" 27 28 "github.com/golang/mock/gomock" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestMapEmpty(t *testing.T) { 33 _, err := NewMap(nil) 34 require.Error(t, err) 35 } 36 37 func TestMapSingleElement(t *testing.T) { 38 var ( 39 opts = NewOptions() 40 id = ident.StringID("someID") 41 ) 42 md1, err := NewMetadata(id, opts) 43 require.NoError(t, err) 44 metadatas := []Metadata{md1} 45 nsMap, err := NewMap(metadatas) 46 require.NoError(t, err) 47 require.Equal(t, 1, len(nsMap.IDs())) 48 require.Equal(t, id.String(), nsMap.IDs()[0].String()) 49 require.Equal(t, 1, len(nsMap.Metadatas())) 50 md := nsMap.Metadatas()[0] 51 require.Equal(t, id.String(), md.ID().String()) 52 require.Equal(t, opts, md.Options()) 53 } 54 55 func TestMapMultipleElements(t *testing.T) { 56 var ( 57 opts1 = NewOptions() 58 opts2 = opts1.SetRepairEnabled(true) 59 id1 = ident.StringID("someID1") 60 id2 = ident.StringID("someID2") 61 ) 62 md1, err := NewMetadata(id1, opts1) 63 require.NoError(t, err) 64 md2, err := NewMetadata(id2, opts2) 65 require.NoError(t, err) 66 metadatas := []Metadata{md1, md2} 67 nsMap, err := NewMap(metadatas) 68 require.NoError(t, err) 69 70 require.Equal(t, 2, len(nsMap.IDs())) 71 ids := nsMap.IDs() 72 require.True(t, ids[0].Equal(id1) || ids[1].Equal(id1)) 73 require.True(t, ids[0].Equal(id2) || ids[1].Equal(id2)) 74 75 require.Equal(t, 2, len(nsMap.Metadatas())) 76 mds := nsMap.Metadatas() 77 require.True(t, id1.Equal(mds[0].ID()) || id1.Equal(mds[1].ID())) 78 require.True(t, id2.Equal(mds[0].ID()) || id2.Equal(mds[1].ID())) 79 } 80 81 func testMap(t *testing.T) Map { 82 var ( 83 opts1 = NewOptions() 84 opts2 = opts1.SetRepairEnabled(true) 85 id1 = ident.StringID("someID1") 86 id2 = ident.StringID("someID2") 87 ) 88 md1, err := NewMetadata(id1, opts1) 89 require.NoError(t, err) 90 md2, err := NewMetadata(id2, opts2) 91 require.NoError(t, err) 92 metadatas := []Metadata{md1, md2} 93 reg, err := NewMap(metadatas) 94 require.NoError(t, err) 95 return reg 96 } 97 98 func TestMapEqualsTrue(t *testing.T) { 99 r1 := testMap(t) 100 require.True(t, r1.Equal(r1)) 101 102 r2 := testMap(t) 103 require.True(t, r1.Equal(r2)) 104 require.True(t, r2.Equal(r1)) 105 } 106 107 func TestMapValidateDuplicateID(t *testing.T) { 108 ctrl := gomock.NewController(t) 109 defer ctrl.Finish() 110 111 var ( 112 opts = NewMockOptions(ctrl) 113 id = ident.StringID("someID") 114 ) 115 opts.EXPECT().Validate().Return(nil).AnyTimes() 116 117 md1, err := NewMetadata(id, opts) 118 require.NoError(t, err) 119 120 md2, err := NewMetadata(id, opts) 121 require.NoError(t, err) 122 123 metadatas := []Metadata{md1, md2} 124 _, err = NewMap(metadatas) 125 require.Error(t, err) 126 }