code.vegaprotocol.io/vega@v0.79.0/core/datasource/common/spec_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 //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types 17 18 package common_test 19 20 import ( 21 "testing" 22 23 "code.vegaprotocol.io/vega/core/datasource/common" 24 datapb "code.vegaprotocol.io/vega/protos/vega/data/v1" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestSpecConditionDeepClone(t *testing.T) { 30 sc := common.SpecCondition{ 31 Operator: common.SpecConditionOperator(int32(5)), 32 Value: "12", 33 } 34 35 res := sc.DeepClone() 36 assert.NotNil(t, res) 37 assert.IsType(t, &common.SpecCondition{}, res) 38 assert.Equal(t, common.SpecConditionOperator(int32(5)), res.Operator) 39 assert.Equal(t, "12", res.Value) 40 } 41 42 func TestSpecConditionsString(t *testing.T) { 43 sc := common.SpecConditions{ 44 { 45 Operator: common.SpecConditionOperator(int32(5)), 46 Value: "12", 47 }, 48 { 49 Operator: common.SpecConditionOperator(int32(58)), 50 Value: "17", 51 }, 52 } 53 54 assert.Equal(t, "[value(12) operator(OPERATOR_LESS_THAN_OR_EQUAL), value(17) operator(58)]", sc.String()) 55 } 56 57 func TestSpecConditionsIntoProto(t *testing.T) { 58 sc := common.SpecConditions{ 59 { 60 Operator: common.SpecConditionOperator(int32(5)), 61 Value: "12", 62 }, 63 { 64 Operator: common.SpecConditionOperator(int32(58)), 65 Value: "17", 66 }, 67 } 68 69 res := sc.IntoProto() 70 assert.NotNil(t, res) 71 assert.IsType(t, []*datapb.Condition{}, res) 72 assert.Equal(t, 2, len(res)) 73 assert.Equal(t, datapb.Condition_Operator(5), res[0].Operator) 74 assert.Equal(t, "12", res[0].Value) 75 assert.Equal(t, datapb.Condition_Operator(58), res[1].Operator) 76 assert.Equal(t, "17", res[1].Value) 77 } 78 79 func TestSpecConditionsFromProto(t *testing.T) { 80 pc := []*datapb.Condition{ 81 { 82 Operator: datapb.Condition_OPERATOR_GREATER_THAN, 83 Value: "test-value-0", 84 }, 85 { 86 Operator: datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, 87 Value: "test-value-1", 88 }, 89 } 90 91 res := common.SpecConditionsFromProto(pc) 92 assert.NotNil(t, res) 93 assert.IsType(t, []*common.SpecCondition{}, res) 94 95 assert.Equal(t, 2, len(res)) 96 assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN, res[0].Operator) 97 assert.Equal(t, "test-value-0", res[0].Value) 98 assert.Equal(t, datapb.Condition_OPERATOR_GREATER_THAN_OR_EQUAL, res[1].Operator) 99 assert.Equal(t, "test-value-1", res[1].Value) 100 } 101 102 func TestSpecFiltersString(t *testing.T) { 103 dp := uint64(5) 104 sf := common.SpecFilters{ 105 { 106 Key: &common.SpecPropertyKey{ 107 Name: "test-name-0", 108 Type: common.SpecPropertyKeyType(4), 109 NumberDecimalPlaces: &dp, 110 }, 111 }, 112 { 113 Key: &common.SpecPropertyKey{ 114 Name: "test-name-1", 115 Type: common.SpecPropertyKeyType(9), 116 NumberDecimalPlaces: &dp, 117 }, 118 }, 119 } 120 121 assert.Equal(t, "[key(name(test-name-0) type(TYPE_BOOLEAN) decimals(5)) conditions([]), key(name(test-name-1) type(9) decimals(5)) conditions([])]", sf.String()) 122 } 123 124 func TestSpecFiltersIntoProto(t *testing.T) { 125 dp := uint64(7) 126 sf := common.SpecFilters{ 127 { 128 Key: &common.SpecPropertyKey{ 129 Name: "test-name-0", 130 Type: common.SpecPropertyKeyType(4), 131 NumberDecimalPlaces: &dp, 132 }, 133 Conditions: []*common.SpecCondition{ 134 {}, 135 }, 136 }, 137 { 138 Key: &common.SpecPropertyKey{ 139 Name: "test-name-1", 140 Type: common.SpecPropertyKeyType(9), 141 NumberDecimalPlaces: &dp, 142 }, 143 Conditions: []*common.SpecCondition{ 144 { 145 Operator: common.SpecConditionOperator(5), 146 Value: "25", 147 }, 148 }, 149 }, 150 } 151 152 res := sf.IntoProto() 153 assert.NotNil(t, res) 154 assert.IsType(t, []*datapb.Filter{}, res) 155 assert.Equal(t, 2, len(res)) 156 assert.NotNil(t, res[0].Key) 157 assert.IsType(t, &datapb.PropertyKey{}, res[0].Key) 158 assert.Equal(t, "test-name-0", res[0].Key.Name) 159 assert.Equal(t, datapb.PropertyKey_Type(4), res[0].Key.Type) 160 assert.Equal(t, &dp, res[0].Key.NumberDecimalPlaces) 161 assert.Equal(t, 1, len(res[0].Conditions)) 162 assert.NotNil(t, res[1].Key) 163 assert.IsType(t, &datapb.PropertyKey{}, res[1].Key) 164 assert.Equal(t, "test-name-1", res[1].Key.Name) 165 assert.Equal(t, datapb.PropertyKey_Type(9), res[1].Key.Type) 166 assert.Equal(t, &dp, res[1].Key.NumberDecimalPlaces) 167 assert.Equal(t, 1, len(res[1].Conditions)) 168 assert.Equal(t, datapb.Condition_OPERATOR_LESS_THAN_OR_EQUAL, res[1].Conditions[0].Operator) 169 assert.Equal(t, "25", res[1].Conditions[0].Value) 170 } 171 172 func TestSpecFiltersFromProto(t *testing.T) { 173 dp := uint64(12) 174 pf := []*datapb.Filter{ 175 { 176 Key: &datapb.PropertyKey{ 177 Name: "test-proto-name-0", 178 Type: datapb.PropertyKey_TYPE_EMPTY, 179 NumberDecimalPlaces: &dp, 180 }, 181 Conditions: []*datapb.Condition{ 182 { 183 Operator: datapb.Condition_OPERATOR_EQUALS, 184 Value: "21", 185 }, 186 { 187 Operator: datapb.Condition_OPERATOR_UNSPECIFIED, 188 Value: "17", 189 }, 190 }, 191 }, 192 } 193 194 res := common.SpecFiltersFromProto(pf) 195 196 assert.NotNil(t, res) 197 assert.IsType(t, []*common.SpecFilter{}, res) 198 assert.Equal(t, 1, len(res)) 199 assert.Equal(t, "test-proto-name-0", res[0].Key.Name) 200 assert.Equal(t, common.SpecPropertyKeyType(1), res[0].Key.Type) 201 assert.Equal(t, dp, *res[0].Key.NumberDecimalPlaces) 202 assert.Equal(t, 2, len(res[0].Conditions)) 203 assert.Equal(t, datapb.Condition_Operator(1), res[0].Conditions[0].Operator) 204 assert.Equal(t, "21", res[0].Conditions[0].Value) 205 assert.Equal(t, datapb.Condition_Operator(0), res[0].Conditions[1].Operator) 206 assert.Equal(t, "17", res[0].Conditions[1].Value) 207 } 208 209 func TestSpecFiltersDeepClone(t *testing.T) { 210 dp := uint64(12) 211 sf := common.SpecFilter{ 212 Key: &common.SpecPropertyKey{ 213 Name: "test-name-1", 214 Type: common.SpecPropertyKeyType(9), 215 NumberDecimalPlaces: &dp, 216 }, 217 Conditions: []*common.SpecCondition{ 218 { 219 Operator: common.SpecConditionOperator(5), 220 Value: "25", 221 }, 222 }, 223 } 224 225 res := sf.DeepClone() 226 assert.NotNil(t, res) 227 assert.IsType(t, &common.SpecFilter{}, res) 228 assert.IsType(t, &common.SpecPropertyKey{}, res.Key) 229 assert.Equal(t, "test-name-1", res.Key.Name) 230 assert.Equal(t, common.SpecPropertyKeyType(9), res.Key.Type) 231 assert.Equal(t, &dp, res.Key.NumberDecimalPlaces) 232 assert.Equal(t, 1, len(res.Conditions)) 233 assert.Equal(t, common.SpecConditionOperator(5), res.Conditions[0].Operator) 234 assert.Equal(t, "25", res.Conditions[0].Value) 235 }