code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/party_activity_streak_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package sqlstore_test 17 18 import ( 19 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/sqlstore" 24 "code.vegaprotocol.io/vega/libs/ptr" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 type partyActivityStreakStores struct { 30 streaksStore *sqlstore.PartyActivityStreaks 31 } 32 33 func newPartyActivityStreakStores(t *testing.T) *partyActivityStreakStores { 34 t.Helper() 35 streaks := sqlstore.NewPartyActivityStreaks(connectionSource) 36 37 return &partyActivityStreakStores{ 38 streaksStore: streaks, 39 } 40 } 41 42 func TestPartyActivityStreak(t *testing.T) { 43 ctx := tempTransaction(t) 44 45 stores := newPartyActivityStreakStores(t) 46 47 now := time.Now() 48 activityStreaks := []entities.PartyActivityStreak{ 49 { 50 PartyID: entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"), 51 ActiveFor: 1, 52 InactiveFor: 0, 53 IsActive: true, 54 RewardDistributionActivityMultiplier: "1", 55 RewardVestingActivityMultiplier: "1", 56 Epoch: 1, 57 TradedVolume: "1000", 58 OpenVolume: "500", 59 VegaTime: now, 60 TxHash: "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0", 61 }, 62 { 63 PartyID: entities.PartyID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"), 64 ActiveFor: 1, 65 InactiveFor: 0, 66 IsActive: true, 67 RewardDistributionActivityMultiplier: "1.5", 68 RewardVestingActivityMultiplier: "1.5", 69 Epoch: 1, 70 TradedVolume: "10000", 71 OpenVolume: "5000", 72 VegaTime: now, 73 TxHash: "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e1", 74 }, 75 { 76 PartyID: entities.PartyID("46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a"), 77 ActiveFor: 2, 78 InactiveFor: 0, 79 IsActive: true, 80 RewardDistributionActivityMultiplier: "1", 81 RewardVestingActivityMultiplier: "1", 82 Epoch: 2, 83 TradedVolume: "1000", 84 OpenVolume: "500", 85 VegaTime: now.Add(1 * time.Second), 86 TxHash: "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e2", 87 }, 88 { 89 PartyID: entities.PartyID("09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e0"), 90 ActiveFor: 2, 91 InactiveFor: 0, 92 IsActive: true, 93 RewardDistributionActivityMultiplier: "1", 94 RewardVestingActivityMultiplier: "1", 95 Epoch: 2, 96 TradedVolume: "1000", 97 OpenVolume: "500", 98 VegaTime: now.Add(1 * time.Second), 99 TxHash: "09d82547b823da327af14727d02936db75c33cffe8e09341a9fc729fe53865e3", 100 }, 101 } 102 103 for _, as := range activityStreaks { 104 assert.NoError(t, stores.streaksStore.Add(ctx, &as)) 105 } 106 107 // now try to get them, without an epoch 108 as, err := stores.streaksStore.Get(ctx, "46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a", nil) 109 assert.NoError(t, err) 110 // should be the last one for this party 111 assert.Equal(t, as.Epoch, uint64(2)) 112 assert.Equal(t, as.PartyID.String(), "46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a") 113 114 // now try to get them with an epoch 115 as, err = stores.streaksStore.Get(ctx, "46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a", ptr.From(uint64(1))) 116 assert.NoError(t, err) 117 // should be the last one for this party 118 assert.Equal(t, as.Epoch, uint64(1)) 119 assert.Equal(t, as.PartyID.String(), "46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a") 120 121 // now try to get them, without the wrong epoch 122 as, err = stores.streaksStore.Get(ctx, "46d66ea0a00609615e04aaf6b41e5e9f552650535ed85059444d68bb6456852a", ptr.From(uint64(4))) 123 assert.EqualError(t, err, entities.ErrNotFound.Error()) 124 assert.Nil(t, as) 125 }