code.vegaprotocol.io/vega@v0.79.0/datanode/entities/date_range_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 entities_test 17 18 import ( 19 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/libs/ptr" 24 ) 25 26 func TestDateRange_Validate(t *testing.T) { 27 type args struct { 28 Start *time.Time 29 End *time.Time 30 Required bool 31 } 32 tests := []struct { 33 name string 34 args args 35 Err error 36 }{ 37 { 38 name: "Should error if required and no dates provided", 39 args: args{ 40 Start: nil, 41 End: nil, 42 Required: true, 43 }, 44 Err: entities.ErrDateRangeIsRequired, 45 }, 46 { 47 name: "Should not error if not required and no dates provided", 48 args: args{ 49 Start: nil, 50 End: nil, 51 Required: false, 52 }, 53 Err: nil, 54 }, 55 { 56 name: "Should error if start date is before minimum date, required false", 57 args: args{ 58 Start: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 59 End: nil, 60 Required: false, 61 }, 62 Err: entities.ErrMinimumDate, 63 }, 64 { 65 name: "Should error if start date is before minimum date, required true", 66 args: args{ 67 Start: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 68 End: nil, 69 Required: true, 70 }, 71 Err: entities.ErrMinimumDate, 72 }, 73 { 74 name: "Should error if end date is before minimum date, required false", 75 args: args{ 76 Start: nil, 77 End: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 78 Required: false, 79 }, 80 Err: entities.ErrMinimumDate, 81 }, 82 { 83 name: "Should error if end date is before minimum date, required true", 84 args: args{ 85 Start: nil, 86 End: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 87 Required: true, 88 }, 89 Err: entities.ErrMinimumDate, 90 }, 91 { 92 name: "Should error if start and end date is before minimum date, required false", 93 args: args{ 94 Start: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 95 End: ptr.From(time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)), 96 Required: false, 97 }, 98 Err: entities.ErrMinimumDate, 99 }, 100 { 101 name: "Should error if start and end date is before minimum date, required true", 102 args: args{ 103 Start: ptr.From(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)), 104 End: ptr.From(time.Date(2019, 2, 1, 0, 0, 0, 0, time.UTC)), 105 Required: true, 106 }, 107 Err: entities.ErrMinimumDate, 108 }, 109 { 110 name: "Should error if end date is before start date, required false", 111 args: args{ 112 Start: ptr.From(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)), 113 End: ptr.From(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)), 114 Required: false, 115 }, 116 Err: entities.ErrEndDateBeforeStart, 117 }, 118 { 119 name: "Should error if end date is before start date, required true", 120 args: args{ 121 Start: ptr.From(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)), 122 End: ptr.From(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)), 123 Required: true, 124 }, 125 Err: entities.ErrEndDateBeforeStart, 126 }, 127 { 128 name: "Should error if duration is more than max, required false", 129 args: args{ 130 Start: ptr.From(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), 131 End: ptr.From(time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)), 132 Required: false, 133 }, 134 Err: entities.ErrDateRangeTooLong, 135 }, 136 { 137 name: "Should error if duration is more than max, required true", 138 args: args{ 139 Start: ptr.From(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)), 140 End: ptr.From(time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)), 141 Required: true, 142 }, 143 Err: entities.ErrDateRangeTooLong, 144 }, 145 { 146 name: "Should error if duration is more than max, no start date", 147 args: args{ 148 Start: nil, 149 End: ptr.From(time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)), 150 Required: true, 151 }, 152 Err: entities.ErrDateRangeTooLong, 153 }, 154 { 155 name: "Should error if duration is more than max, no end date", 156 args: args{ 157 Start: ptr.From(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)), 158 End: nil, 159 Required: true, 160 }, 161 Err: entities.ErrDateRangeTooLong, 162 }, 163 } 164 for _, tt := range tests { 165 t.Run(tt.name, func(t *testing.T) { 166 dr := entities.DateRange{ 167 Start: tt.args.Start, 168 End: tt.args.End, 169 } 170 if err := dr.Validate(tt.args.Required); err != tt.Err { 171 t.Errorf("DateRange.Validate() error = %v, wantErr %v", err, tt.Err) 172 } 173 }) 174 } 175 }