-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_test.go
More file actions
953 lines (770 loc) · 26.9 KB
/
integration_test.go
File metadata and controls
953 lines (770 loc) · 26.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//go:build integration
package epo_ops
import (
"context"
"flag"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
var updateFixtures = flag.Bool("update-fixtures", false, "update fixture files from live API responses")
// TestAuthenticationIntegration tests real authentication against EPO servers.
func TestAuthenticationIntegration(t *testing.T) {
// Read credentials from environment
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
// Create authenticator
auth := NewAuthenticator(consumerKey, consumerSecret, nil)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Test: Acquire token
t.Run("AcquireToken", func(t *testing.T) {
token, err := auth.GetToken(ctx)
if err != nil {
t.Fatalf("Failed to acquire token: %v", err)
}
if token == "" {
t.Fatal("Received empty token")
}
// Token should be a reasonably long string
if len(token) < 20 {
t.Errorf("Token seems too short: %d characters", len(token))
}
t.Logf("Successfully acquired token (length: %d)", len(token))
})
// Test: Token reuse within TTL
t.Run("TokenReuse", func(t *testing.T) {
// Get token first time
token1, err := auth.GetToken(ctx)
if err != nil {
t.Fatalf("Failed to get first token: %v", err)
}
// Get token second time (should be cached)
token2, err := auth.GetToken(ctx)
if err != nil {
t.Fatalf("Failed to get second token: %v", err)
}
// Should be the same token
if token1 != token2 {
t.Error("Expected same token from cache, got different token")
}
t.Log("Successfully reused cached token")
})
// Test: Token format
t.Run("TokenFormat", func(t *testing.T) {
token, err := auth.GetToken(ctx)
if err != nil {
t.Fatalf("Failed to get token: %v", err)
}
// Token should not contain spaces or newlines
if strings.Contains(token, " ") || strings.Contains(token, "\n") {
t.Error("Token contains whitespace")
}
t.Logf("Token format valid")
})
}
// TestClientCreationIntegration tests client creation with real credentials.
func TestClientCreationIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
if client == nil {
t.Fatal("Client is nil")
}
if client.authenticator == nil {
t.Error("Client authenticator is nil")
}
if client.generated == nil {
t.Error("Client generated client is nil")
}
t.Log("Successfully created client with valid credentials")
}
// TestInvalidCredentialsIntegration tests authentication with invalid credentials.
func TestInvalidCredentialsIntegration(t *testing.T) {
// Create authenticator with invalid credentials
auth := NewAuthenticator("invalid_key", "invalid_secret", nil)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
token, err := auth.GetToken(ctx)
if err == nil {
t.Error("Expected error with invalid credentials, got nil")
}
if token != "" {
t.Error("Expected empty token with invalid credentials")
}
// Should be an AuthError
if _, ok := err.(*AuthError); !ok {
t.Errorf("Expected AuthError, got: %T", err)
}
t.Logf("Correctly rejected invalid credentials: %v", err)
}
// TestTextRetrievalIntegration tests retrieving patent text data.
func TestTextRetrievalIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Test patent: EP.1000000.B1 (docdb format, well-known test patent)
testPatent := "EP.1000000.B1"
// Test: GetBiblio
t.Run("GetBiblio", func(t *testing.T) {
biblio, err := client.GetBiblio(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get biblio: %v", err)
}
if biblio == nil {
t.Fatal("Received nil biblio data")
}
if len(biblio.Titles) == 0 {
t.Error("Expected non-empty Titles map")
}
// Log English title if available
title := biblio.Titles["en"]
if title == "" {
// Fall back to any available title
for _, v := range biblio.Titles {
title = v
break
}
}
t.Logf("Successfully retrieved biblio: Title=%q, Applicants=%d, Inventors=%d",
title, len(biblio.Applicants), len(biblio.Inventors))
})
// Test: GetClaims
t.Run("GetClaims", func(t *testing.T) {
claims, err := client.GetClaims(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get claims: %v", err)
}
if claims == nil {
t.Fatal("Received nil claims data")
}
if len(claims.Claims) == 0 {
t.Error("Expected non-empty Claims slice")
}
t.Logf("Successfully retrieved %d claims (language: %s)", len(claims.Claims), claims.Language)
})
// Test: GetDescription
t.Run("GetDescription", func(t *testing.T) {
description, err := client.GetDescription(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get description: %v", err)
}
if description == nil {
t.Fatal("Received nil description data")
}
if len(description.Paragraphs) == 0 {
t.Error("Expected non-empty Paragraphs")
}
t.Logf("Successfully retrieved description: %d paragraphs (language: %s)",
len(description.Paragraphs), description.Language)
})
// Test: GetAbstract
t.Run("GetAbstract", func(t *testing.T) {
abstract, err := client.GetAbstract(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get abstract: %v", err)
}
if abstract == nil {
t.Fatal("Received nil abstract data")
}
t.Logf("Successfully retrieved abstract (language: %s, length: %d chars)",
abstract.Language, len(abstract.Text))
})
// Test: GetFulltext
t.Run("GetFulltext", func(t *testing.T) {
fulltext, err := client.GetFulltext(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get fulltext: %v", err)
}
if fulltext == nil {
t.Fatal("Received nil fulltext data")
}
if fulltext.Status == "" {
t.Logf("Warning: Fulltext status is empty")
}
t.Logf("Successfully retrieved fulltext: status=%s, hasBiblio=%v, hasClaims=%v",
fulltext.Status, fulltext.Biblio != nil, fulltext.Claims != nil)
})
}
// TestNotFoundIntegration tests handling of non-existent patents.
func TestNotFoundIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Non-existent patent
_, err = client.GetBiblio(ctx, "publication", "docdb", "EP.99999999999.A1")
if err == nil {
t.Error("Expected error for non-existent patent, got nil")
}
// Should be a NotFoundError
if _, ok := err.(*NotFoundError); !ok {
t.Logf("Error type: %T, value: %v", err, err)
// Note: EPO might return different error for truly invalid patents
}
t.Logf("Correctly handled non-existent patent: %v", err)
}
// TestQuotaTrackingIntegration tests quota tracking from API responses.
func TestQuotaTrackingIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Initially, quota should be nil (no requests made yet)
initialQuota := client.GetLastQuota()
if initialQuota != nil {
t.Error("Expected nil quota before making any requests")
}
// Make a real API call
testPatent := "EP.1000000.B1"
_, err = client.GetBiblio(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get biblio: %v", err)
}
// After request, quota should be available
quota := client.GetLastQuota()
if quota == nil {
t.Fatal("Expected quota information after API request")
}
// Log quota information
t.Logf("Quota Status: %s", quota.Status)
t.Logf("Individual Quota: Used=%d, Limit=%d (%.2f%%)",
quota.Individual.Used, quota.Individual.Limit, quota.Individual.UsagePercent())
t.Logf("Registered Quota: Used=%d, Limit=%d (%.2f%%)",
quota.Registered.Used, quota.Registered.Limit, quota.Registered.UsagePercent())
// Make another request to verify quota is updated
_, err = client.GetAbstract(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get abstract: %v", err)
}
newQuota := client.GetLastQuota()
if newQuota == nil {
t.Fatal("Expected quota information after second API request")
}
// Quota should be updated (used amount should increase or stay same)
if newQuota.Individual.Limit > 0 {
if newQuota.Individual.Used < quota.Individual.Used {
t.Error("Quota used amount decreased unexpectedly")
}
t.Logf("Quota updated after second request: Used=%d", newQuota.Individual.Used)
}
if newQuota.Registered.Limit > 0 {
if newQuota.Registered.Used < quota.Registered.Used {
t.Error("Quota used amount decreased unexpectedly")
}
t.Logf("Registered quota updated: Used=%d", newQuota.Registered.Used)
}
}
// TestSearchIntegration tests patent search functionality.
func TestSearchIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Test: Basic search for patents with "plastic" in title
t.Run("BasicSearch", func(t *testing.T) {
results, err := client.Search(ctx, "ti=plastic", "1-5")
if err != nil {
t.Fatalf("Failed to search: %v", err)
}
if results == nil {
t.Fatal("Received nil search results")
}
if results.TotalCount == 0 {
t.Error("Expected TotalCount > 0")
}
t.Logf("Successfully retrieved search results: %d total, %d parsed results",
results.TotalCount, len(results.Results))
})
// Test: Search with applicant
t.Run("SearchByApplicant", func(t *testing.T) {
results, err := client.Search(ctx, "pa=Siemens", "1-3")
if err != nil {
t.Fatalf("Failed to search by applicant: %v", err)
}
if results == nil {
t.Fatal("Received nil search results")
}
t.Logf("Successfully retrieved applicant search: %d total results", results.TotalCount)
})
// Test: Search with constituent
t.Run("SearchWithConstituent", func(t *testing.T) {
results, err := client.SearchWithConstituent(ctx, "biblio", "ti=plastic", "1-3")
if err != nil {
t.Fatalf("Failed to search with constituent: %v", err)
}
if results == nil {
t.Fatal("Received nil search results")
}
t.Logf("Successfully retrieved search with biblio constituent: %d results",
len(results.Results))
})
}
// TestFamilyRetrievalIntegration tests INPADOC family retrieval.
func TestFamilyRetrievalIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Test patent: EP.1000000.B1 (docdb format, has a known family)
testPatent := "EP.1000000.B1"
// Test: Basic family retrieval
t.Run("GetFamily", func(t *testing.T) {
family, err := client.GetFamily(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get family: %v", err)
}
if family == nil {
t.Fatal("Received nil family data")
}
if len(family.Members) == 0 {
t.Error("Expected non-empty family members")
}
// Verify parsed struct fields
for i, m := range family.Members {
if m.Country == "" || m.DocNumber == "" {
t.Errorf("Member %d: missing Country or DocNumber", i)
}
}
if len(family.Countries) == 0 {
t.Error("Expected non-empty Countries list")
}
t.Logf("Successfully retrieved family: %d members, countries: %v",
len(family.Members), family.Countries)
// Save fixture
if raw, err := client.GetFamilyRaw(ctx, "publication", "docdb", testPatent); err == nil {
saveFixture(t, "family_EP1000000", []byte(raw))
}
})
// Test: Family with biblio
t.Run("GetFamilyWithBiblio", func(t *testing.T) {
family, err := client.GetFamilyWithBiblio(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get family with biblio: %v", err)
}
if family == nil {
t.Fatal("Received nil family data")
}
if len(family.Members) == 0 {
t.Error("Expected non-empty family members")
}
// Verify biblio data extracted
withTitle := 0
for _, m := range family.Members {
if m.Title != "" {
withTitle++
}
}
t.Logf("Family with biblio: %d members, %d with Title", len(family.Members), withTitle)
if withTitle == 0 {
t.Error("Expected at least some members with Title from biblio")
}
})
// Test: Family with legal
t.Run("GetFamilyWithLegal", func(t *testing.T) {
family, err := client.GetFamilyWithLegal(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Logf("Warning: Failed to get family with legal: %v", err)
return
}
if family == nil {
t.Fatal("Received nil family data")
}
t.Logf("Successfully retrieved family with legal: %d members", len(family.Members))
})
}
// TestImageRetrievalIntegration tests patent image retrieval and TIFF conversion.
func TestImageRetrievalIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// Test: Retrieve first page of patent drawings
// EP.1000000.B1 should have drawings available
t.Run("GetImage", func(t *testing.T) {
imageData, err := client.GetImage(ctx, "EP", "1000000", "B1", "Drawing", 1)
if err != nil {
// Some patents may not have images, log instead of failing
t.Logf("Warning: Failed to get image: %v", err)
t.Skip("Skipping image test - image may not be available")
return
}
if len(imageData) == 0 {
t.Error("Received empty image data")
}
// Image should be reasonably large (at least 1KB)
if len(imageData) < 1024 {
t.Errorf("Image data seems too small: %d bytes", len(imageData))
}
// Check if it's a TIFF file (starts with II or MM)
isTIFF := len(imageData) >= 4 && ((imageData[0] == 'I' && imageData[1] == 'I') || // Little-endian
(imageData[0] == 'M' && imageData[1] == 'M')) // Big-endian
if !isTIFF {
t.Logf("Warning: Image data does not appear to be TIFF format (first bytes: %x)", imageData[:min(4, len(imageData))])
}
t.Logf("Successfully retrieved image data (length: %d bytes, format: %s)",
len(imageData),
map[bool]string{true: "TIFF", false: "unknown"}[isTIFF])
})
}
// TestAdditionalServicesIntegration tests legal, register, and number conversion services.
func TestAdditionalServicesIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
testPatent := "EP.1000000.B1"
// Test: Legal status retrieval
t.Run("GetLegal", func(t *testing.T) {
legal, err := client.GetLegal(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Logf("Warning: Failed to get legal status: %v", err)
t.Skip("Skipping legal test - legal data may not be available")
return
}
if legal == nil {
t.Fatal("Received nil legal data")
}
if len(legal.LegalEvents) == 0 {
t.Error("Expected non-empty legal events")
}
// Verify parsed fields
for i, e := range legal.LegalEvents {
if i < 3 {
t.Logf("Event %d: Code=%s Country=%s Date=%s", i+1, e.EventCode, e.Country, e.Date)
}
}
t.Logf("Legal events for %s: %d events", legal.PatentNumber, len(legal.LegalEvents))
// Save fixture
if raw, err := client.GetLegalRaw(ctx, "publication", "docdb", testPatent); err == nil {
saveFixture(t, "legal_EP1000000", []byte(raw))
}
})
// Test: Register biblio retrieval (raw XML) — register requires epodoc format
t.Run("GetRegisterBiblioRaw", func(t *testing.T) {
register, err := client.GetRegisterBiblioRaw(ctx, "publication", "epodoc", "EP1000000")
if err != nil {
// Register data might not be available for all patents
t.Logf("Warning: Failed to get register biblio: %v", err)
t.Skip("Skipping register biblio test - data may not be available")
return
}
if register == "" {
t.Error("Received empty register data")
}
t.Logf("Successfully retrieved register biblio (length: %d bytes)", len(register))
})
// Test: Register events retrieval (parsed) — register requires epodoc format
t.Run("GetRegisterEvents", func(t *testing.T) {
data, err := client.GetRegisterEvents(ctx, "publication", "epodoc", "EP1000000")
if err != nil {
t.Logf("Warning: Failed to get register events: %v", err)
t.Skip("Skipping register events test - data may not be available")
return
}
if len(data.Events) == 0 {
t.Error("Expected non-empty Events")
}
// Verify events have required fields
for i, evt := range data.Events {
if evt.Date == "" || evt.EventCode == "" {
t.Errorf("Event %d missing Date or EventCode", i)
break
}
if evt.Category == "" {
t.Errorf("Event %d missing Category", i)
break
}
}
t.Logf("Retrieved %d register events, %d statuses for %s",
len(data.Events), len(data.Statuses), data.PatentNumber)
// Save fixture
if raw, err := client.GetRegisterEventsRaw(ctx, "publication", "epodoc", "EP1000000"); err == nil {
saveFixture(t, "register_events_EP1000000", []byte(raw))
}
})
// Test: Number conversion (parsed)
t.Run("ConvertPatentNumber", func(t *testing.T) {
data, err := client.ConvertPatentNumber(ctx, "publication", "docdb", testPatent, "epodoc")
if err != nil {
t.Logf("Warning: Failed to convert patent number: %v", err)
t.Skip("Skipping number conversion test - service may not be available")
return
}
if data.DocNumber == "" {
t.Error("Expected non-empty DocNumber")
}
if data.Kind == "" {
t.Error("Expected non-empty Kind")
}
if data.InputFormat != "docdb" {
t.Errorf("Expected InputFormat 'docdb', got %q", data.InputFormat)
}
if data.OutputFormat != "epodoc" {
t.Errorf("Expected OutputFormat 'epodoc', got %q", data.OutputFormat)
}
t.Logf("Converted %s → DocNumber=%s Kind=%s Date=%s",
testPatent, data.DocNumber, data.Kind, data.Date)
// Save fixture
if raw, err := client.ConvertPatentNumberRaw(ctx, "publication", "docdb", testPatent, "epodoc"); err == nil {
saveFixture(t, "convert_patent_number", []byte(raw))
}
})
}
// TestClassificationIntegration tests CPC classification schema retrieval.
func TestClassificationIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
t.Run("GetClassificationSchema", func(t *testing.T) {
data, err := client.GetClassificationSchema(ctx, "H04W", false, false)
if err != nil {
t.Fatalf("Failed to get classification schema: %v", err)
}
if data.Symbol == "" {
t.Error("Expected non-empty Symbol")
}
if data.Title == "" {
t.Error("Expected non-empty Title")
}
if data.Level < 0 {
t.Errorf("Expected Level >= 0, got %d", data.Level)
}
if data.SchemeType == "" {
t.Error("Expected non-empty SchemeType")
}
t.Logf("Classification: Symbol=%s Title=%q Level=%d SchemeType=%s Children=%d",
data.Symbol, data.Title, data.Level, data.SchemeType, len(data.Children))
// Verify child fields if present
for i, ch := range data.Children {
if ch.Symbol == "" {
t.Errorf("Child[%d] has empty Symbol", i)
}
if i < 3 {
t.Logf(" Child[%d]: Symbol=%s Title=%q Level=%d", i, ch.Symbol, ch.Title, ch.Level)
}
}
// Save fixture
if raw, err := client.GetClassificationSchemaRaw(ctx, "H04W", false, false); err == nil {
saveFixture(t, "classification_schema_H04W", []byte(raw))
}
})
t.Run("GetClassificationSchema_WithAncestors", func(t *testing.T) {
data, err := client.GetClassificationSchema(ctx, "H04W84/18", true, false)
if err != nil {
t.Logf("Warning: Failed to get classification with ancestors: %v", err)
t.Skip("Skipping ancestors test")
return
}
if data.Symbol == "" {
t.Error("Expected non-empty Symbol")
}
t.Logf("Classification with ancestors: Symbol=%s Title=%q Level=%d",
data.Symbol, data.Title, data.Level)
})
}
// TestPublishedEquivalentsIntegration tests published equivalents retrieval.
func TestPublishedEquivalentsIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
testPatent := "EP.1000000.B1"
t.Run("GetPublishedEquivalents", func(t *testing.T) {
data, err := client.GetPublishedEquivalents(ctx, "publication", "docdb", testPatent)
if err != nil {
t.Fatalf("Failed to get published equivalents: %v", err)
}
if len(data.Equivalents) == 0 {
t.Error("Expected non-empty Equivalents for EP.1000000.B1")
}
for i, eq := range data.Equivalents {
if eq.Country == "" || eq.DocNumber == "" {
t.Errorf("Equivalent[%d]: missing Country or DocNumber", i)
}
if i < 5 {
t.Logf("Equivalent[%d]: %s%s%s", i, eq.Country, eq.DocNumber, eq.Kind)
}
}
t.Logf("Total equivalents for %s: %d", testPatent, len(data.Equivalents))
// Save fixture
if raw, err := client.GetPublishedEquivalentsRaw(ctx, "publication", "docdb", testPatent); err == nil {
saveFixture(t, "published_equivalents_EP1000000", []byte(raw))
}
})
}
// TestEdgeCasesIntegration tests edge cases with patents that may have limited data.
func TestEdgeCasesIntegration(t *testing.T) {
consumerKey := os.Getenv("EPO_OPS_CONSUMER_KEY")
consumerSecret := os.Getenv("EPO_OPS_CONSUMER_SECRET")
if consumerKey == "" || consumerSecret == "" {
t.Skip("Skipping integration test: EPO_OPS_CONSUMER_KEY and EPO_OPS_CONSUMER_SECRET must be set")
}
config := &Config{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
}
client, err := NewClient(config)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
// EP.0000001.B1 — very old patent, likely has minimal or no legal events
t.Run("GetLegal_OldPatent", func(t *testing.T) {
data, err := client.GetLegal(ctx, "publication", "docdb", "EP.0000001.B1")
if err != nil {
// Not found or no data is acceptable for edge case
t.Logf("GetLegal for EP.0000001.B1 returned error (expected for edge case): %v", err)
return
}
t.Logf("EP0000001B1 legal events: %d", len(data.LegalEvents))
if len(data.LegalEvents) == 0 {
t.Log("No legal events for EP0000001B1 (edge case confirmed)")
}
})
// Test GetFamily on a patent with a small family
t.Run("GetFamily_SmallFamily", func(t *testing.T) {
data, err := client.GetFamily(ctx, "publication", "docdb", "EP.0000001.B1")
if err != nil {
t.Logf("GetFamily for EP.0000001.B1 returned error: %v", err)
return
}
t.Logf("EP0000001B1 family: %d members, countries: %v",
len(data.Members), data.Countries)
})
}
// saveFixture saves raw API response data as a fixture file when -update-fixtures is set.
func saveFixture(t *testing.T, name string, data []byte) {
t.Helper()
if !*updateFixtures {
return
}
dir := filepath.Join("testdata", "fixtures")
if err := os.MkdirAll(dir, 0755); err != nil {
t.Logf("Warning: failed to create fixture dir: %v", err)
return
}
path := filepath.Join(dir, name+".xml")
if err := os.WriteFile(path, data, 0644); err != nil {
t.Logf("Warning: failed to write fixture %s: %v", path, err)
return
}
t.Logf("Updated fixture: %s", path)
}