github.com/dolthub/go-mysql-server@v0.18.0/sql/in_mem_table/multimap_test.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package in_mem_table 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 type user struct { 24 username string 25 email string 26 sidecar int 27 } 28 29 func ueq(l, r *user) bool { 30 return l.username == r.username && l.email == r.email 31 } 32 33 type usernameKeyer struct{} 34 35 func (usernameKeyer) GetKey(u *user) any { 36 return u.username 37 } 38 39 type emailKeyer struct{} 40 41 func (emailKeyer) GetKey(u *user) any { 42 return u.email 43 } 44 45 var keyers = []Keyer[*user]{usernameKeyer{}, emailKeyer{}} 46 47 func TestIndexedSetCount(t *testing.T) { 48 set := NewIndexedSet(ueq, keyers) 49 require.Equal(t, 0, set.Count()) 50 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 51 set.Put(&user{"brian", "brian@dolthub.com", 0}) 52 require.Equal(t, 2, set.Count()) 53 set.Put(&user{"dustin", "dustin@dolthub.com", 0}) 54 require.Equal(t, 3, set.Count()) 55 set.Clear() 56 require.Equal(t, 0, set.Count()) 57 58 // IndexedSet allows the same entry multiple times. 59 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 60 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 61 require.Equal(t, 2, set.Count()) 62 } 63 64 func TestIndexedSetGet(t *testing.T) { 65 set := NewIndexedSet(ueq, keyers) 66 u, ok := set.Get(&user{"aaron", "aaron@dolthub.com", 0}) 67 require.False(t, ok) 68 require.Nil(t, u) 69 set.Put(&user{"brian", "brian@dolthub.com", 0}) 70 u, ok = set.Get(&user{"aaron", "aaron@dolthub.com", 0}) 71 require.False(t, ok) 72 require.Nil(t, u) 73 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 74 u, ok = set.Get(&user{"aaron", "aaron@dolthub.com", 0}) 75 require.True(t, ok) 76 require.NotNil(t, u) 77 require.Equal(t, "aaron", u.username) 78 require.Equal(t, "aaron@dolthub.com", u.email) 79 } 80 81 func TestIndexedSetRemove(t *testing.T) { 82 set := NewIndexedSet(ueq, keyers) 83 u, ok := set.Remove(&user{"aaron", "aaron@dolthub.com", 0}) 84 require.False(t, ok) 85 require.Nil(t, u) 86 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 87 u, ok = set.Remove(&user{"aaron", "aaron@dolthub.com", 0}) 88 require.True(t, ok) 89 require.NotNil(t, u) 90 require.Equal(t, "aaron", u.username) 91 require.Equal(t, "aaron@dolthub.com", u.email) 92 require.Equal(t, 0, set.Count()) 93 94 set.Put(&user{"brian", "brian@dolthub.com", 0}) 95 set.Put(&user{"brian", "brian@dolthub.com", 0}) 96 set.Put(&user{"brian", "brian+another@dolthub.com", 0}) 97 u, ok = set.Remove(&user{"brian", "brian@dolthub.com", 0}) 98 require.True(t, ok) 99 require.NotNil(t, u) 100 require.Equal(t, "brian", u.username) 101 require.Equal(t, "brian@dolthub.com", u.email) 102 require.Equal(t, 1, set.Count()) 103 104 res := set.GetMany(emailKeyer{}, "brian@dolthub.com") 105 require.Len(t, res, 0) 106 res = set.GetMany(emailKeyer{}, "brian+another@dolthub.com") 107 require.Len(t, res, 1) 108 } 109 110 func TestIndexedSetRemoveMany(t *testing.T) { 111 set := NewIndexedSet(ueq, keyers) 112 set.Put(&user{"brian", "brian@dolthub.com", 0}) 113 set.Put(&user{"brian", "brian@dolthub.com", 0}) 114 set.Put(&user{"brian", "brian+another@dolthub.com", 0}) 115 116 set.RemoveMany(usernameKeyer{}, "brian") 117 require.Equal(t, 0, set.Count()) 118 119 set.Put(&user{"brian", "brian@dolthub.com", 0}) 120 set.Put(&user{"brian", "brian@dolthub.com", 0}) 121 set.Put(&user{"brian", "brian+another@dolthub.com", 0}) 122 set.Put(&user{"aaron", "aaron@dolthub.com", 0}) 123 set.RemoveMany(emailKeyer{}, "brian@dolthub.com") 124 require.Equal(t, 2, set.Count()) 125 res := set.GetMany(emailKeyer{}, "brian+another@dolthub.com") 126 require.Len(t, res, 1) 127 res = set.GetMany(usernameKeyer{}, "aaron") 128 require.Len(t, res, 1) 129 }