github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/discovery/priced_repository_test.go (about) 1 /* 2 * Copyright (C) 2021 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 discovery 19 20 import ( 21 "errors" 22 "math/big" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 27 "github.com/mysteriumnetwork/node/core/discovery/proposal" 28 "github.com/mysteriumnetwork/node/market" 29 ) 30 31 var mockProposal = market.ServiceProposal{ 32 ID: 1, 33 Format: "good format, i like it", 34 Compatibility: 2, 35 ProviderID: "0x0", 36 ServiceType: "much service", 37 Location: market.Location{ 38 IPType: "residential", 39 Country: "yes", 40 Continent: "no", 41 ASN: 1, 42 ISP: "some isp", 43 }, 44 } 45 46 var presetRepository = &mockFilterPresetRepository{ 47 presets: proposal.FilterPresets{Entries: []proposal.FilterPreset{ 48 { 49 ID: 0, 50 Name: "", 51 IPType: "", 52 }, 53 }}, 54 } 55 56 func TestGetProposal(t *testing.T) { 57 t.Run("fetches proposal correctly", func(t *testing.T) { 58 mockPrice := market.Price{ 59 PricePerHour: big.NewInt(1), 60 PricePerGiB: big.NewInt(2), 61 } 62 mp := &mockPriceInfoProvider{ 63 priceToReturn: mockPrice, 64 } 65 66 mr := &mockRepository{ 67 proposalToReturn: &mockProposal, 68 errToReturn: nil, 69 } 70 71 repo := NewPricedServiceProposalRepository(mr, mp, presetRepository) 72 73 result, err := repo.Proposal(market.ProposalID{}) 74 assert.NoError(t, err) 75 76 assert.EqualValues(t, mockProposal, result.ServiceProposal) 77 assert.EqualValues(t, mockPrice, result.Price) 78 }) 79 t.Run("bubbles repo errors", func(t *testing.T) { 80 mockError := errors.New("boom") 81 mr := &mockRepository{ 82 errToReturn: mockError, 83 } 84 85 repo := NewPricedServiceProposalRepository(mr, &mockPriceInfoProvider{}, presetRepository) 86 _, err := repo.Proposal(market.ProposalID{}) 87 assert.Error(t, err) 88 assert.Equal(t, mockError, err) 89 }) 90 t.Run("bubbles price errors", func(t *testing.T) { 91 mockError := errors.New("boom") 92 mp := &mockPriceInfoProvider{ 93 errorToReturn: mockError, 94 } 95 repo := NewPricedServiceProposalRepository(&mockRepository{ 96 proposalToReturn: &mockProposal, 97 }, mp, nil) 98 99 _, err := repo.Proposal(market.ProposalID{}) 100 assert.Error(t, err) 101 assert.Equal(t, mockError, err) 102 }) 103 } 104 105 func TestGetProposals(t *testing.T) { 106 t.Run("fetches proposals correctly", func(t *testing.T) { 107 mockPrice := market.Price{ 108 PricePerHour: big.NewInt(1), 109 PricePerGiB: big.NewInt(2), 110 } 111 mp := &mockPriceInfoProvider{ 112 priceToReturn: mockPrice, 113 } 114 115 mr := &mockRepository{ 116 proposalsToReturn: []market.ServiceProposal{mockProposal}, 117 errToReturn: nil, 118 } 119 120 repo := NewPricedServiceProposalRepository(mr, mp, presetRepository) 121 122 result, err := repo.Proposals(nil) 123 assert.NoError(t, err) 124 125 assert.EqualValues(t, mockProposal, result[0].ServiceProposal) 126 assert.EqualValues(t, mockPrice, result[0].Price) 127 }) 128 t.Run("bubbles repo errors", func(t *testing.T) { 129 mockError := errors.New("boom") 130 mr := &mockRepository{ 131 errToReturn: mockError, 132 } 133 134 repo := NewPricedServiceProposalRepository(mr, &mockPriceInfoProvider{}, presetRepository) 135 _, err := repo.Proposals(nil) 136 assert.Error(t, err) 137 assert.Equal(t, mockError, err) 138 }) 139 t.Run("skips if price errors", func(t *testing.T) { 140 mockError := errors.New("boom") 141 mp := &mockPriceInfoProvider{ 142 errorToReturn: mockError, 143 } 144 repo := NewPricedServiceProposalRepository(&mockRepository{ 145 proposalsToReturn: []market.ServiceProposal{mockProposal}, 146 }, mp, presetRepository) 147 148 res, err := repo.Proposals(nil) 149 assert.NoError(t, err) 150 assert.Len(t, res, 0) 151 }) 152 } 153 154 type mockRepository struct { 155 proposalsToReturn []market.ServiceProposal 156 errToReturn error 157 proposalToReturn *market.ServiceProposal 158 } 159 160 func (mr *mockRepository) Proposal(id market.ProposalID) (*market.ServiceProposal, error) { 161 return mr.proposalToReturn, mr.errToReturn 162 } 163 164 func (mr *mockRepository) Proposals(filter *proposal.Filter) ([]market.ServiceProposal, error) { 165 return mr.proposalsToReturn, mr.errToReturn 166 } 167 168 func (mr *mockRepository) Countries(filter *proposal.Filter) (map[string]int, error) { 169 return nil, nil 170 } 171 172 type mockPriceInfoProvider struct { 173 priceToReturn market.Price 174 errorToReturn error 175 } 176 177 func (mpip *mockPriceInfoProvider) GetCurrentPrice(nodeType string, country string, serviceType string) (market.Price, error) { 178 return mpip.priceToReturn, mpip.errorToReturn 179 } 180 181 type mockFilterPresetRepository struct { 182 presets proposal.FilterPresets 183 } 184 185 func (m *mockFilterPresetRepository) List() (*proposal.FilterPresets, error) { 186 return &m.presets, nil 187 } 188 189 func (m *mockFilterPresetRepository) Get(id int) (*proposal.FilterPreset, error) { 190 for _, p := range m.presets.Entries { 191 if p.ID == id { 192 return &p, nil 193 } 194 } 195 return nil, errors.New("preset not found") 196 }