-
Notifications
You must be signed in to change notification settings - Fork 1
/
definition_test.go
551 lines (523 loc) · 12.1 KB
/
definition_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package conval
import (
"bytes"
"testing"
"time"
"github.com/ftl/hamradio/callsign"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefinition_ExchangeFields(t *testing.T) {
tt := []struct {
desc string
exchange []ExchangeDefinition
expected []ExchangeField
}{
{
desc: "one definition",
exchange: []ExchangeDefinition{
{
Fields: []ExchangeField{
{RSTProperty},
{SerialNumberProperty},
},
},
},
expected: []ExchangeField{
{RSTProperty},
{SerialNumberProperty},
},
},
{
desc: "two definitions, common RST field",
exchange: []ExchangeDefinition{
{
Fields: []ExchangeField{
{RSTProperty},
{SerialNumberProperty},
},
},
{
Fields: []ExchangeField{
{RSTProperty},
{NoMemberProperty, "wag_dok"},
},
},
},
expected: []ExchangeField{
{RSTProperty},
{SerialNumberProperty, NoMemberProperty, "wag_dok"},
},
},
{
desc: "two definitions, different field count",
exchange: []ExchangeDefinition{
{
Fields: []ExchangeField{
{RSTProperty},
{SerialNumberProperty},
},
},
{
Fields: []ExchangeField{
{RSTProperty},
{NameProperty},
{StateProvinceProperty},
},
},
},
expected: []ExchangeField{
{RSTProperty},
{SerialNumberProperty, NameProperty},
{EmptyProperty, StateProvinceProperty},
},
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
definition := Definition{
Exchange: tc.exchange,
}
actual := definition.ExchangeFields()
assert.Equal(t, tc.expected, actual)
})
}
}
func TestLoadYAML(t *testing.T) {
tt := []struct {
desc string
yaml string
expected Definition
}{
{
desc: "valid name, identifier, official rules",
yaml: `name: Test Contest
identifier: TEST-CONTEST-VALID
official_rules: https://github.com/ftl/conval/testdata`,
expected: Definition{
Name: "Test Contest",
Identifier: "TEST-CONTEST-VALID",
OfficialRules: "https://github.com/ftl/conval/testdata",
},
},
{
desc: "constrained and unconstrained duration",
yaml: `name: Test Contest
duration: 48h
duration-constraints:
- duration: 24h
operator_mode: single`,
expected: Definition{
Name: "Test Contest",
Duration: 48 * time.Hour,
DurationConstraints: []ConstrainedDuration{
{Constraint: Constraint{OperatorMode: SingleOperator}, Duration: 24 * time.Hour},
},
},
},
{
desc: "constrained break",
yaml: `name: Test Contest
breaks:
- operator_mode: single
overlay: classic
duration: 1h`,
expected: Definition{
Name: "Test Contest",
Breaks: []ConstrainedDuration{
{Constraint: Constraint{OperatorMode: SingleOperator, Overlay: ClassicOverlay}, Duration: 1 * time.Hour},
},
},
},
{
desc: "single mode, dual mode, all mode categories",
yaml: `name: Test Contest
categories:
- name: Single Mode
modes: [cw]
- name: Dual Mode
modes: [cw, rtty]
- name: All Mode
modes: [all]`,
expected: Definition{
Name: "Test Contest",
Categories: []Category{
{Name: "Single Mode", Modes: []Mode{"cw"}},
{Name: "Dual Mode", Modes: []Mode{"cw", "rtty"}},
{Name: "All Mode", Modes: []Mode{"all"}},
},
},
},
{
desc: "known and unknown overlays",
yaml: `name: Test Contest
overlays:
- tb_wires
- something_special`,
expected: Definition{
Name: "Test Contest",
Overlays: []Overlay{"tb_wires", "something_special"},
},
},
{
desc: "modes and bands",
yaml: `name: Test Contest
modes:
- cw
- ssb
bands:
- 80m
- 40m
- 20m`,
expected: Definition{
Name: "Test Contest",
Modes: []Mode{"cw", "ssb"},
Bands: []ContestBand{"80m", "40m", "20m"},
},
},
{
desc: "multiple band change rules",
yaml: `name: Test Contest
band_change_rules:
- operator_mode: single
overlay: classic
grace_period: 10m
multiplier_exception: false
- grace_period: 10m
multiplier_exception: true`,
expected: Definition{
Name: "Test Contest",
BandChangeRules: []BandChangeRule{
{Constraint: Constraint{OperatorMode: SingleOperator, Overlay: ClassicOverlay}, GracePeriod: 10 * time.Minute},
{GracePeriod: 10 * time.Minute, MultiplierException: true},
},
},
},
{
desc: "three exchange field, one with two alternatives",
yaml: `name: Test Contest
exchange:
- fields:
- [rst]
- [serial]
- [member_number, nm]`,
expected: Definition{
Name: "Test Contest",
Exchange: []ExchangeDefinition{
{
Fields: []ExchangeField{
{RSTProperty},
{SerialNumberProperty},
{MemberNumberProperty, NoMemberProperty},
},
},
},
},
},
{
desc: "scoring rules",
yaml: `name: Test Contest
scoring:
qsos:
- their_continent: [other]
bands:
- 10m
- 15m
- 20m
value: 3
- their_continent: [other]
bands:
- 40m
- 80m
- 160m
value: 6
- value: 1
qso_band_rule: once_per_band
multis:
- property: dxcc_entity
my_continent: [eu]
band_rule: once_per_band
value: 1
- property: dxcc_entity
my_continent: [af, an, as, na, oc, sa]
band_rule: once_per_band
value: 2`,
expected: Definition{
Name: "Test Contest",
Scoring: Scoring{
QSORules: []ScoringRule{
{TheirContinent: []Continent{OtherContinent}, Bands: []ContestBand{Band10m, Band15m, Band20m}, Value: 3},
{TheirContinent: []Continent{OtherContinent}, Bands: []ContestBand{Band40m, Band80m, Band160m}, Value: 6},
{Value: 1},
},
QSOBandRule: OncePerBand,
MultiRules: []ScoringRule{
{Property: DXCCEntityProperty, MyContinent: []Continent{Europa}, BandRule: OncePerBand, Value: 1},
{Property: DXCCEntityProperty, MyContinent: []Continent{Africa, Antarctica, Asia, NorthAmerica, Oceania, SouthAmerica}, BandRule: OncePerBand, Value: 2},
},
},
},
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
buffer := bytes.NewBufferString(tc.yaml)
actual, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err)
assert.Equal(t, tc.expected, *actual)
})
}
}
func TestSaveLoadYAMLRoundtrip(t *testing.T) {
names, err := IncludedDefinitionNames()
require.NoError(t, err)
for _, name := range names {
t.Run(name, func(t *testing.T) {
definition, err := IncludedDefinition(name)
require.NoError(t, err)
buffer := bytes.NewBuffer([]byte{})
err = SaveDefinitionYAML(buffer, definition, true)
assert.NoError(t, err, "save")
loadedDefinition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err, "load")
assert.Equal(t, *definition, *loadedDefinition)
})
}
}
func TestSaveYAMLWithoutExamples(t *testing.T) {
names, err := IncludedDefinitionNames()
require.NoError(t, err)
for _, name := range names {
t.Run(name, func(t *testing.T) {
definition, err := IncludedDefinition(name)
require.NoError(t, err)
definitionWithoutExamples := *definition
definitionWithoutExamples.Examples = nil
buffer := bytes.NewBuffer([]byte{})
err = SaveDefinitionYAML(buffer, definition, false)
assert.NoError(t, err, "save")
loadedDefinition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err, "load")
assert.Equal(t, definitionWithoutExamples, *loadedDefinition)
})
}
}
func TestPropertyConstraint_Matches(t *testing.T) {
tt := []struct {
desc string
myValue string
theirValue string
constraint PropertyConstraint
expected bool
}{
{
desc: "not empty, true",
theirValue: "123",
constraint: PropertyConstraint{
TheirValueNotEmpty: true,
},
expected: true,
},
{
desc: "not empty, false",
theirValue: "",
constraint: PropertyConstraint{
TheirValueNotEmpty: true,
},
expected: false,
},
{
desc: "same value, same true",
myValue: "123",
theirValue: "123",
constraint: PropertyConstraint{
SameValue: true,
},
expected: true,
},
{
desc: "other value, same true",
myValue: "123",
theirValue: "321",
constraint: PropertyConstraint{
SameValue: true,
},
expected: false,
},
{
desc: "same value, other true",
myValue: "123",
theirValue: "123",
constraint: PropertyConstraint{
OtherValue: true,
},
expected: false,
},
{
desc: "other value, other true",
myValue: "123",
theirValue: "321",
constraint: PropertyConstraint{
OtherValue: true,
},
expected: true,
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
actual := tc.constraint.Matches(tc.myValue, tc.theirValue)
assert.Equal(t, tc.expected, actual)
})
}
}
func TestPropertyDefinition_Validate(t *testing.T) {
tt := []struct {
desc string
yaml string
values []string
valid []bool
}{
{
desc: "simple value list",
yaml: `name: Test Contest
properties:
- name: pty
values: [A, B, C]`,
values: []string{"A", "a", "D"},
valid: []bool{true, true, false},
},
{
desc: "regular expression",
yaml: `name: Test Contest
properties:
- name: pty
expression: "\\d*[A-Z][A-Z0-9]+"`,
values: []string{"B36", "b36", "70DARC", "1A"},
valid: []bool{true, true, true, false},
},
{
desc: "regular expression over value list",
yaml: `name: Test Contest
properties:
- name: pty
values: [1A, B, C]
expression: "\\d*[A-Z][A-Z0-9]+"`,
values: []string{"B36", "b36", "70DARC", "1A", "B", "C"},
valid: []bool{true, true, true, false, false, false},
},
}
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
buffer := bytes.NewBufferString(tc.yaml)
definition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err)
validator, ok := definition.PropertyValidator("pty")
assert.True(t, ok)
assert.NotNil(t, validator)
for i, value := range tc.values {
err := validator.ValidateProperty(value, nil)
if tc.valid[i] {
assert.NoError(t, err, i)
} else {
assert.Error(t, err, i)
}
}
})
}
}
func TestPropertyDefinition_Get(t *testing.T) {
tt := []struct {
desc string
yaml string
theirCall string
exchange []string
expected []string
}{
{
desc: "simple value list",
yaml: `name: Test Contest
properties:
- name: pty
values: [A, B, C]
exchange:
- fields:
- [pty]`,
exchange: []string{"A", "a", "D"},
expected: []string{"A", "A", ""},
},
{
desc: "regular expression",
yaml: `name: Test Contest
properties:
- name: pty
expression: "\\d*[A-Z][A-Z0-9]+"
exchange:
- fields:
- [pty]`,
exchange: []string{"B36", "b36", "70DARC", "1A"},
expected: []string{"B36", "B36", "70DARC", ""},
},
{
desc: "regular expression over value list",
yaml: `name: Test Contest
properties:
- name: pty
values: [1A, B, C]
expression: "\\d*[A-Z][A-Z0-9]+"
exchange:
- fields:
- [pty]`,
exchange: []string{"B36", "b36", "70DARC", "1A", "B", "C"},
expected: []string{"B36", "B36", "70DARC", "", "", ""},
},
{
desc: "derived property",
yaml: `name: Test Contest
properties:
- name: src
expression: "\\d*[A-Z][A-Z0-9]+"
- name: pty
source: src
expression: "\\d*([A-Z]).*"
exchange:
- fields:
- [src]`,
exchange: []string{"B36", "b36", "70DARC", "1A", "B", "C"},
expected: []string{"B", "B", "D", "", "", ""},
},
{
desc: "property derived from their call",
yaml: `name: Test Contest
properties:
- name: pty
source: their_call
expression: ".*([A-Z0-9])"`,
theirCall: "DL1ABC",
exchange: []string{""},
expected: []string{"C"},
},
}
prefixes, err := NewPrefixDatabase()
require.NoError(t, err)
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
buffer := bytes.NewBufferString(tc.yaml)
definition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err)
getter, ok := definition.PropertyGetter("pty")
assert.True(t, ok)
assert.NotNil(t, getter)
for i, exchange := range tc.exchange {
parsedExchange := ParseExchange(definition.ExchangeFields(), []string{exchange}, nil, definition)
qso := QSO{
TheirExchange: parsedExchange,
}
if tc.theirCall != "" {
qso.TheirCall = callsign.MustParse(tc.theirCall)
}
actual := getter.GetProperty(qso, Setup{}, prefixes)
assert.Equal(t, tc.expected[i], actual, i)
}
})
}
}