github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/beneficiary/saver_test.go (about) 1 /* 2 * Copyright (C) 2017 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 beneficiary 19 20 import ( 21 "os" 22 "testing" 23 24 "github.com/mysteriumnetwork/node/core/storage/boltdb" 25 "github.com/mysteriumnetwork/node/identity" 26 27 "github.com/pkg/errors" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestBeneficiaryChangeStatus(t *testing.T) { 32 // given: 33 dir, err := os.MkdirTemp("/tmp", "mysttest") 34 assert.NoError(t, err) 35 36 defer os.RemoveAll(dir) 37 db, err := boltdb.NewStorage(dir) 38 39 id := identity.FromAddress("0x94bb756322a137a5f0b013dd972d227fe7caa698") 40 benef := "0x94bb756322a137a5f0b013dd972d227fe7caa000" 41 fixture := beneficiaryChangeKeeper{chainID: 1, st: db} 42 43 // when: 44 fixture.updateChangeStatus(id, Pending, benef, nil) 45 r, err := fixture.GetChangeStatus(id) 46 47 // then: 48 assert.Equal(t, Pending, r.State) 49 assert.NoError(t, err) 50 assert.Empty(t, r.Error) 51 52 // when: 53 fixture.updateChangeStatus(id, Completed, benef, errors.New("ran out of gas")) 54 r, err = fixture.GetChangeStatus(id) 55 56 // then: 57 assert.Equal(t, Completed, r.State) 58 assert.NoError(t, err) 59 assert.Equal(t, r.Error, "ran out of gas") 60 61 // when: 62 fixture.updateChangeStatus(id, Completed, benef, nil) 63 r, err = fixture.GetChangeStatus(id) 64 65 // then: 66 assert.Equal(t, Completed, r.State) 67 assert.NoError(t, err) 68 assert.Empty(t, r.Error) 69 70 // when 71 r, err = fixture.GetChangeStatus(identity.FromAddress("0x94aa756322a137a5f0b013dd972d227fe7caa698")) 72 73 // then: 74 assert.Nil(t, r) 75 assert.Error(t, err) 76 77 // when: 78 fixture.updateChangeStatus(id, Pending, benef, nil) 79 r, err = fixture.GetChangeStatus(id) 80 assert.NoError(t, err) 81 assert.Equal(t, Pending, r.State) 82 83 // then: 84 r, err = fixture.CleanupAndGetChangeStatus(id, benef) 85 assert.NoError(t, err) 86 assert.Equal(t, Completed, r.State) 87 88 }