github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/brokerdiscovery/storage_test.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package brokerdiscovery 19 20 import ( 21 "testing" 22 23 "github.com/mysteriumnetwork/node/core/discovery/proposal" 24 "github.com/mysteriumnetwork/node/core/discovery/reducer" 25 "github.com/mysteriumnetwork/node/eventbus" 26 "github.com/mysteriumnetwork/node/market" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 var ( 31 proposalProvider1Streaming = market.ServiceProposal{ 32 ServiceType: "streaming", 33 ProviderID: "0x1", 34 } 35 proposalProvider1Noop = market.ServiceProposal{ 36 ServiceType: "noop", 37 ProviderID: "0x1", 38 } 39 proposalProvider2Streaming = market.ServiceProposal{ 40 ServiceType: "streaming", 41 ProviderID: "0x2", 42 } 43 ) 44 45 func Test_Storage_Proposals(t *testing.T) { 46 storage := createEmptyStorage() 47 assert.Equal(t, []market.ServiceProposal{}, storage.Proposals()) 48 49 storage = createFullStorage() 50 assert.Equal( 51 t, 52 []market.ServiceProposal{proposalProvider1Streaming, proposalProvider1Noop, proposalProvider2Streaming}, 53 storage.Proposals(), 54 ) 55 } 56 57 func Test_Finder_MatchProposals(t *testing.T) { 58 storage := createFullStorage() 59 60 proposals, err := storage.MatchProposals(reducer.All()) 61 assert.NoError(t, err) 62 assert.Len(t, proposals, 3) 63 assert.Exactly(t, 64 []market.ServiceProposal{proposalProvider1Streaming, proposalProvider1Noop, proposalProvider2Streaming}, 65 proposals, 66 ) 67 68 proposals, err = storage.MatchProposals(reducer.Equal(reducer.ProviderID, "0x2")) 69 assert.NoError(t, err) 70 assert.Len(t, proposals, 1) 71 assert.Exactly(t, 72 []market.ServiceProposal{proposalProvider2Streaming}, 73 proposals, 74 ) 75 } 76 77 func Test_Finder_FindProposals(t *testing.T) { 78 storage := createFullStorage() 79 80 proposals, err := storage.FindProposals(&proposal.Filter{}) 81 assert.NoError(t, err) 82 assert.Len(t, proposals, 3) 83 assert.Exactly(t, 84 []market.ServiceProposal{proposalProvider1Streaming, proposalProvider1Noop, proposalProvider2Streaming}, 85 proposals, 86 ) 87 88 proposals, err = storage.FindProposals(&proposal.Filter{ServiceType: "streaming"}) 89 assert.NoError(t, err) 90 assert.Len(t, proposals, 2) 91 assert.Exactly(t, 92 []market.ServiceProposal{proposalProvider1Streaming, proposalProvider2Streaming}, 93 proposals, 94 ) 95 } 96 97 func Test_Storage_HasProposal(t *testing.T) { 98 storage := createEmptyStorage() 99 assert.False(t, storage.HasProposal(market.ProposalID{ServiceType: "unknown", ProviderID: "0x1"})) 100 101 storage = createFullStorage() 102 assert.False(t, storage.HasProposal(market.ProposalID{ServiceType: "unknown", ProviderID: "0x1"})) 103 assert.True(t, storage.HasProposal(market.ProposalID{ServiceType: "streaming", ProviderID: "0x1"})) 104 } 105 106 func Test_Storage_GetProposal(t *testing.T) { 107 storage := createEmptyStorage() 108 p, err := storage.GetProposal(market.ProposalID{ServiceType: "unknown", ProviderID: "0x1"}) 109 assert.EqualError(t, err, "proposal does not exist: {unknown 0x1 0}") 110 assert.Nil(t, p) 111 112 storage = createFullStorage() 113 p, err = storage.GetProposal(market.ProposalID{ServiceType: "unknown", ProviderID: "0x1"}) 114 assert.EqualError(t, err, "proposal does not exist: {unknown 0x1 0}") 115 assert.Nil(t, p) 116 117 p, err = storage.GetProposal(market.ProposalID{ServiceType: "streaming", ProviderID: "0x1"}) 118 assert.NoError(t, err) 119 assert.Exactly(t, proposalProvider1Streaming, *p) 120 } 121 122 func Test_Storage_Set(t *testing.T) { 123 storage := createEmptyStorage() 124 storage.Set([]market.ServiceProposal{proposalProvider1Streaming, proposalProvider1Noop}) 125 assert.Equal( 126 t, 127 []market.ServiceProposal{ 128 proposalProvider1Streaming, 129 proposalProvider1Noop, 130 }, 131 storage.proposals, 132 ) 133 134 storage = createFullStorage() 135 storage.Set([]market.ServiceProposal{proposalProvider1Streaming, proposalProvider1Noop}) 136 assert.Equal( 137 t, 138 []market.ServiceProposal{ 139 proposalProvider1Streaming, 140 proposalProvider1Noop, 141 }, 142 storage.proposals, 143 ) 144 } 145 146 func Test_Storage_AddProposal(t *testing.T) { 147 storage := createEmptyStorage() 148 storage.AddProposal(proposalProvider1Streaming, proposalProvider1Noop) 149 assert.Equal( 150 t, 151 []market.ServiceProposal{ 152 proposalProvider1Streaming, 153 proposalProvider1Noop, 154 }, 155 storage.proposals, 156 ) 157 158 storage.AddProposal(proposalProvider1Streaming) 159 assert.Equal( 160 t, 161 []market.ServiceProposal{ 162 proposalProvider1Streaming, 163 proposalProvider1Noop, 164 }, 165 storage.proposals, 166 ) 167 168 storage.AddProposal(proposalProvider2Streaming) 169 assert.Equal( 170 t, 171 []market.ServiceProposal{ 172 proposalProvider1Streaming, 173 proposalProvider1Noop, 174 proposalProvider2Streaming, 175 }, 176 storage.proposals, 177 ) 178 179 storage = createFullStorage() 180 storage.AddProposal(proposalProvider1Streaming, proposalProvider1Noop) 181 assert.Equal( 182 t, 183 []market.ServiceProposal{ 184 proposalProvider1Streaming, 185 proposalProvider1Noop, 186 proposalProvider2Streaming, 187 }, 188 storage.proposals, 189 ) 190 } 191 192 func Test_Storage_RemoveProposal(t *testing.T) { 193 storage := createEmptyStorage() 194 storage.RemoveProposal(market.ProposalID{ServiceType: "streaming", ProviderID: "0x1"}) 195 assert.Equal( 196 t, 197 []market.ServiceProposal{}, 198 storage.proposals, 199 ) 200 201 storage = createFullStorage() 202 storage.RemoveProposal(market.ProposalID{ServiceType: "streaming", ProviderID: "0x1"}) 203 assert.Equal( 204 t, 205 []market.ServiceProposal{ 206 proposalProvider1Noop, 207 proposalProvider2Streaming, 208 }, 209 storage.proposals, 210 ) 211 } 212 213 func createEmptyStorage() *ProposalStorage { 214 return &ProposalStorage{ 215 eventPublisher: eventbus.New(), 216 proposals: []market.ServiceProposal{}, 217 } 218 } 219 220 func createFullStorage() *ProposalStorage { 221 return &ProposalStorage{ 222 eventPublisher: eventbus.New(), 223 proposals: []market.ServiceProposal{ 224 proposalProvider1Streaming, proposalProvider1Noop, proposalProvider2Streaming, 225 }, 226 } 227 }