-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidation_test.go
More file actions
700 lines (646 loc) · 17.9 KB
/
validation_test.go
File metadata and controls
700 lines (646 loc) · 17.9 KB
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package epo_ops
import (
"context"
"errors"
"fmt"
"strings"
"testing"
)
func TestValidateDocdbFormat(t *testing.T) {
tests := []struct {
name string
number string
wantError bool
}{
{"Valid EP patent", "EP.1000000.B1", false},
{"Valid US patent", "US.5551212.A", false},
{"Valid WO patent", "WO.2023123456.A1", false},
{"Valid with single digit kind", "DE.102023001.A", false},
{"Empty number", "", true},
{"Missing dots", "EP1000000B1", true},
{"No kind code EP", "EP.1000000.", false},
{"No kind code US", "US.7654321.", false},
{"Lowercase country", "ep.1000000.B1", true},
{"Invalid kind code", "EP.1000000.B12", true},
{"No country code", ".1000000.B1", true},
{"Letters in number", "EP.100A000.B1", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateDocdbFormat(tt.number)
if (err != nil) != tt.wantError {
t.Errorf("ValidateDocdbFormat(%q) error = %v, wantError %v", tt.number, err, tt.wantError)
}
// Check that error is ValidationError type
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
if valErr.Format != "docdb" {
t.Errorf("Expected format 'docdb', got %q", valErr.Format)
}
}
})
}
}
func TestValidateEpodocFormat(t *testing.T) {
tests := []struct {
name string
number string
wantError bool
}{
{"Valid with kind code", "EP1000000B1", false},
{"Valid without kind code", "EP1000000", false},
{"Valid US patent", "US5551212A", false},
{"Valid WO patent", "WO2023123456A1", false},
{"Valid single char kind", "DE102023001A", false},
{"Empty number", "", true},
{"With dots", "EP.1000000.B1", true},
{"Lowercase country", "ep1000000B1", true},
{"No country code", "1000000B1", true},
{"Letters in number", "EP100A000B1", true},
{"Invalid kind code", "EP1000000B12", true},
{"Single letter country", "E1000000B1", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateEpodocFormat(tt.number)
if (err != nil) != tt.wantError {
t.Errorf("ValidateEpodocFormat(%q) error = %v, wantError %v", tt.number, err, tt.wantError)
}
// Check that error is ValidationError type
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
if valErr.Format != "epodoc" {
t.Errorf("Expected format 'epodoc', got %q", valErr.Format)
}
}
})
}
}
func TestValidateOriginalFormat(t *testing.T) {
tests := []struct {
name string
number string
wantError bool
}{
{"Standard format", "EP 1000000 B1", false},
{"With commas", "US5,551,212", false},
{"With slashes", "WO/2023/123456", false},
{"Mixed format", "EP-1000000-B1", false},
{"Very flexible", "Patent#123-456/789", false},
{"Empty number", "", true},
{"Too long", string(make([]byte, 101)), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateOriginalFormat(tt.number)
if (err != nil) != tt.wantError {
t.Errorf("ValidateOriginalFormat(%q) error = %v, wantError %v", tt.number, err, tt.wantError)
}
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
}
})
}
}
func TestValidateFormat(t *testing.T) {
tests := []struct {
name string
format string
number string
wantError bool
}{
{"Docdb format valid", "docdb", "EP.1000000.B1", false},
{"Epodoc format valid", "epodoc", "EP1000000B1", false},
{"Original format valid", "original", "EP 1000000 B1", false},
{"Using constant FormatDocDB", FormatDocDB, "EP.1000000.B1", false},
{"Using constant FormatEPODOC", FormatEPODOC, "EP1000000B1", false},
{"Using constant FormatOriginal", FormatOriginal, "EP 1000000 B1", false},
{"Invalid format", "invalid", "EP1000000B1", true},
{"Wrong format for number", "docdb", "EP1000000B1", true},
{"Empty number", "docdb", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateFormat(tt.format, tt.number)
if (err != nil) != tt.wantError {
t.Errorf("ValidateFormat(%q, %q) error = %v, wantError %v", tt.format, tt.number, err, tt.wantError)
}
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
}
})
}
}
func TestValidateDate(t *testing.T) {
tests := []struct {
name string
date string
wantError bool
}{
{"Valid date", "20231015", false},
{"Valid date 1999", "19990101", false},
{"Valid date 2025", "20251231", false},
{"Empty date (optional)", "", false},
{"Too short", "2023101", true},
{"Too long", "202310155", true},
{"Letters in date", "2023AB15", true},
{"With dashes", "2023-10-15", true},
{"With slashes", "2023/10/15", true},
{"Invalid month (format check only)", "20231399", false}, // Format is valid, semantic check not done
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateDate(tt.date)
if (err != nil) != tt.wantError {
t.Errorf("ValidateDate(%q) error = %v, wantError %v", tt.date, err, tt.wantError)
}
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
if valErr.Field != "date" {
t.Errorf("Expected field 'date', got %q", valErr.Field)
}
}
})
}
}
func TestValidateRefType(t *testing.T) {
tests := []struct {
name string
refType string
wantError bool
}{
{"Valid publication", "publication", false},
{"Valid application", "application", false},
{"Valid priority", "priority", false},
{"Using constant RefTypePublication", RefTypePublication, false},
{"Using constant RefTypeApplication", RefTypeApplication, false},
{"Using constant RefTypePriority", RefTypePriority, false},
{"Invalid type", "invalid", true},
{"Empty string", "", true},
{"Uppercase", "PUBLICATION", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateRefType(tt.refType)
if (err != nil) != tt.wantError {
t.Errorf("ValidateRefType(%q) error = %v, wantError %v", tt.refType, err, tt.wantError)
}
if err != nil {
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
if valErr.Field != "refType" {
t.Errorf("Expected field 'refType', got %q", valErr.Field)
}
}
})
}
}
func TestValidationError_Error(t *testing.T) {
tests := []struct {
name string
valErr *ValidationError
expected string
}{
{
name: "With format",
valErr: &ValidationError{
Field: "number",
Format: "docdb",
Value: "EP1000000B1",
Message: "must match pattern",
},
expected: `validation error: number (docdb format): must match pattern - got: "EP1000000B1"`,
},
{
name: "Without format",
valErr: &ValidationError{
Field: "refType",
Value: "invalid",
Message: "must be publication, application, or priority",
},
expected: `validation error: refType: must be publication, application, or priority - got: "invalid"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.valErr.Error()
if got != tt.expected {
t.Errorf("Error() = %q, want %q", got, tt.expected)
}
})
}
}
// Integration test: Verify validation happens before API call
func TestValidation_PreventsBadAPICall(t *testing.T) {
// This test verifies that validation errors are returned
// BEFORE making any HTTP request to the API
config := DefaultConfig()
config.ConsumerKey = "test"
config.ConsumerSecret = "test"
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
// Test with invalid docdb format (should fail validation before API call)
_, err = client.GetBiblioRaw(context.TODO(), RefTypePublication, FormatDocDB, "EP1000000B1") // Missing dots
if err == nil {
t.Fatal("Expected validation error, got nil")
}
// Should be a ValidationError, not an API error
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError before API call, got %T: %v", err, err)
}
// Verify it's the format validation that failed
if valErr != nil && valErr.Format != "docdb" {
t.Errorf("Expected docdb format validation error, got format: %q", valErr.Format)
}
}
func TestNormalizeToDocdb(t *testing.T) {
tests := []struct {
name string
input string
want string
wantError bool
errorMsg string // Partial match for error message
}{
// Valid EPODOC format conversions
{
name: "EPODOC EP format",
input: "EP2884620A2",
want: "EP.2884620.A2",
},
{
name: "EPODOC US format",
input: "US5551212A",
want: "US.5551212.A",
},
{
name: "EPODOC WO format",
input: "WO2023123456A1",
want: "WO.2023123456.A1",
},
{
name: "EPODOC DE format single char kind",
input: "DE123C",
want: "DE.123.C",
},
{
name: "EPODOC EP with B1 kind",
input: "EP1000000B1",
want: "EP.1000000.B1",
},
{
name: "EPODOC with complex number",
input: "JP2023567890A",
want: "JP.2023567890.A",
},
// Already valid DOCDB format (should return unchanged)
{
name: "Already DOCDB EP",
input: "EP.2884620.A2",
want: "EP.2884620.A2",
},
{
name: "Already DOCDB US",
input: "US.5551212.A",
want: "US.5551212.A",
},
{
name: "Already DOCDB WO",
input: "WO.2023123456.A1",
want: "WO.2023123456.A1",
},
// With whitespace (should be cleaned)
{
name: "With spaces",
input: "EP 2884620 A2",
want: "EP.2884620.A2",
},
{
name: "With leading/trailing spaces",
input: " EP2884620A2 ",
want: "EP.2884620.A2",
},
{
name: "DOCDB with spaces",
input: "EP .2884620 .A2",
want: "EP.2884620.A2",
},
// With hyphens (should be removed)
{
name: "With hyphens",
input: "EP-2884620-A2",
want: "EP.2884620.A2",
},
// With slashes (should be removed)
{
name: "With slashes",
input: "EP/2884620/A2",
want: "EP.2884620.A2",
},
// Mixed separators
{
name: "Mixed separators",
input: "EP 2884620-A2",
want: "EP.2884620.A2",
},
// Error cases
{
name: "Empty string",
input: "",
wantError: true,
errorMsg: "cannot be empty",
},
{
name: "Only whitespace",
input: " ",
wantError: true,
errorMsg: "only whitespace",
},
{
name: "Only separators",
input: "---",
wantError: true,
errorMsg: "only whitespace",
},
{
name: "Missing country code",
input: "2884620A2",
wantError: true,
errorMsg: "unable to parse",
},
{
name: "Single char country",
input: "E2884620A2",
wantError: true,
errorMsg: "unable to parse",
},
{
name: "Missing kind code",
input: "EP2884620",
wantError: true,
errorMsg: "unable to parse",
},
{
name: "Invalid DOCDB format",
input: "EP.2884620.A23", // Kind code too long
wantError: true,
errorMsg: "invalid DOCDB format",
},
{
name: "Lowercase country code",
input: "ep2884620A2",
wantError: true,
errorMsg: "converted format is invalid",
},
{
name: "Letters in number",
input: "EP28A4620A2",
wantError: true,
errorMsg: "converted format is invalid",
},
{
name: "Invalid kind code format",
input: "EP2884620A23", // Kind code too long
wantError: true,
errorMsg: "unable to parse",
},
{
name: "Only country code",
input: "EP",
wantError: true,
errorMsg: "unable to parse",
},
{
name: "Missing number portion",
input: "EPA2",
wantError: true,
errorMsg: "unable to parse",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NormalizeToDocdb(tt.input)
// Check error expectation
if (err != nil) != tt.wantError {
t.Errorf("NormalizeToDocdb(%q) error = %v, wantError %v", tt.input, err, tt.wantError)
return
}
// If error expected, check error message
if tt.wantError {
if err == nil {
t.Errorf("NormalizeToDocdb(%q) expected error containing %q, got nil", tt.input, tt.errorMsg)
return
}
// Check that error is ValidationError type
var valErr *ValidationError
if !errors.As(err, &valErr) {
t.Errorf("Expected ValidationError, got %T", err)
}
// Check error message contains expected substring
if tt.errorMsg != "" && !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("NormalizeToDocdb(%q) error = %q, want error containing %q", tt.input, err.Error(), tt.errorMsg)
}
return
}
// Check result
if got != tt.want {
t.Errorf("NormalizeToDocdb(%q) = %q, want %q", tt.input, got, tt.want)
}
// Verify result is valid DOCDB format
if err := ValidateDocdbFormat(got); err != nil {
t.Errorf("NormalizeToDocdb(%q) produced invalid DOCDB format %q: %v", tt.input, got, err)
}
})
}
}
// TestNormalizeToDocdb_Idempotent verifies that normalizing an already-normalized number returns the same value
func TestNormalizeToDocdb_Idempotent(t *testing.T) {
inputs := []string{
"EP2884620A2",
"US5551212A",
"WO2023123456A1",
"EP 1000000 B1",
"DE-123-C",
}
for _, input := range inputs {
t.Run(input, func(t *testing.T) {
// First normalization
first, err := NormalizeToDocdb(input)
if err != nil {
t.Fatalf("First normalization failed: %v", err)
}
// Second normalization (should be idempotent)
second, err := NormalizeToDocdb(first)
if err != nil {
t.Fatalf("Second normalization failed: %v", err)
}
if first != second {
t.Errorf("Not idempotent: first=%q, second=%q", first, second)
}
})
}
}
// TestNormalizeToDocdb_EdgeCases tests various edge cases
func TestNormalizeToDocdb_EdgeCases(t *testing.T) {
tests := []struct {
name string
input string
wantError bool
}{
{"Very long number", "EP123456789012345678901234567890A1", false},
{"Single digit number", "EP1A", false},
{"Two digit number", "EP12A", false},
{"Three letter country (should fail)", "EUR1000000A1", true},
{"Numeric country code (should fail)", "12345678A1", true},
{"Special characters in country", "E@1000000A1", true},
{"Unicode characters", "EP\u20001000000A1", true},
{"Tab character", "EP\t1000000\tA1", false}, // Should be cleaned like space
{"Newline character", "EP\n1000000\nA1", true}, // Not cleaned, should fail
{"Kind code without number", "EPA", true},
{"Multiple dots already", "EP..1000000..A1", true},
{"Dot at wrong position", "E.P1000000A1", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NormalizeToDocdb(tt.input)
if (err != nil) != tt.wantError {
t.Errorf("NormalizeToDocdb(%q) error = %v, wantError %v", tt.input, err, tt.wantError)
}
})
}
}
func TestValidateBulkNumbers(t *testing.T) {
tests := []struct {
name string
numbers []string
format string
wantError bool
errField string // Expected error field name
}{
{
name: "Valid single docdb number",
numbers: []string{"EP.1000000.B1"},
format: FormatDocDB,
wantError: false,
},
{
name: "Valid multiple docdb numbers",
numbers: []string{"EP.1000000.B1", "US.5551212.A", "WO.2023123456.A1"},
format: FormatDocDB,
wantError: false,
},
{
name: "Valid 100 numbers (max allowed)",
numbers: make100ValidNumbers(),
format: FormatDocDB,
wantError: false,
},
{
name: "Empty numbers slice",
numbers: []string{},
format: FormatDocDB,
wantError: true,
errField: "numbers",
},
{
name: "Nil numbers slice",
numbers: nil,
format: FormatDocDB,
wantError: true,
errField: "numbers",
},
{
name: "Exceeds maximum of 100",
numbers: make101Numbers(),
format: FormatDocDB,
wantError: true,
errField: "numbers",
},
{
name: "Invalid format in first number",
numbers: []string{"INVALID", "EP.1000001.B1"},
format: FormatDocDB,
wantError: true,
},
{
name: "Invalid format in middle number",
numbers: []string{"EP.1000000.B1", "INVALID", "EP.1000002.B1"},
format: FormatDocDB,
wantError: true,
},
{
name: "Invalid format in last number",
numbers: []string{"EP.1000000.B1", "EP.1000001.B1", "INVALID"},
format: FormatDocDB,
wantError: true,
},
{
name: "Valid epodoc numbers",
numbers: []string{"EP1000000", "US5551212"},
format: FormatEPODOC,
wantError: false,
},
{
name: "Mixed valid and invalid formats",
numbers: []string{"EP.1000000.B1", "EP1000001"}, // docdb then epodoc
format: FormatDocDB,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateBulkNumbers(tt.numbers, tt.format)
if (err != nil) != tt.wantError {
t.Errorf("ValidateBulkNumbers() error = %v, wantError %v", err, tt.wantError)
return
}
if err != nil {
// Check error message contains expected field if specified
if tt.errField != "" && !strings.Contains(err.Error(), tt.errField) {
t.Errorf("Expected error to contain field %q, got: %v", tt.errField, err)
}
// Check that error is ValidationError type for our errors
var valErr *ValidationError
if errors.As(err, &valErr) {
if tt.errField != "" && valErr.Field != tt.errField {
t.Errorf("Expected error field %q, got %q", tt.errField, valErr.Field)
}
}
}
})
}
}
// Helper functions for test data generation
func make100ValidNumbers() []string {
numbers := make([]string, 100)
for i := 0; i < 100; i++ {
numbers[i] = fmt.Sprintf("EP.%07d.B1", i+1000000)
}
return numbers
}
func make101Numbers() []string {
numbers := make([]string, 101)
for i := 0; i < 101; i++ {
numbers[i] = fmt.Sprintf("EP.%07d.B1", i+1000000)
}
return numbers
}