-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sdk_parity.py
More file actions
1015 lines (906 loc) · 37.1 KB
/
Copy pathtest_sdk_parity.py
File metadata and controls
1015 lines (906 loc) · 37.1 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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Test SDK parity across Python, JavaScript, and R.
This module verifies that all three generated SDKs (Python, JavaScript, R)
maintain consistent method signatures and configuration parameters.
"""
import re
from pathlib import Path
import pytest
PROJECT_ROOT = Path(__file__).parent.parent.parent
# ── Canonical method definitions ──────────────────────────────────────────────
CANONICAL_METHODS = {
"set_taxa": {
"params": ["taxa", "filter_type"],
"python_name": "set_taxa",
"js_name": "setTaxa",
"r_name": "set_taxa",
},
"set_rank": {
"params": ["rank"],
"python_name": "set_rank",
"js_name": "setRank",
"r_name": "set_rank",
},
"set_assemblies": {
"params": ["assemblies"],
"python_name": "set_assemblies",
"js_name": "setAssemblies",
"r_name": "set_assemblies",
},
"set_samples": {
"params": ["samples"],
"python_name": "set_samples",
"js_name": "setSamples",
"r_name": "set_samples",
},
"add_attribute": {
"params": ["name", "operator", "value", "modifiers"],
"python_name": "add_attribute",
"js_name": "addAttribute",
"r_name": "add_attribute",
},
"set_attributes": {
"params": ["attributes"],
"python_name": "set_attributes",
"js_name": "setAttributes",
"r_name": "set_attributes",
},
"add_field": {
"params": ["name", "modifiers"],
"python_name": "add_field",
"js_name": "addField",
"r_name": "add_field",
},
"set_fields": {
"params": ["fields"],
"python_name": "set_fields",
"js_name": "setFields",
"r_name": "set_fields",
},
"set_names": {
"params": ["name_classes"],
"python_name": "set_names",
"js_name": "setNames",
"r_name": "set_names",
},
"set_ranks": {
"params": ["ranks"],
"python_name": "set_ranks",
"js_name": "setRanks",
"r_name": "set_ranks",
},
"set_exclude_ancestral": {
"params": ["fields"],
"python_name": "set_exclude_ancestral",
"js_name": "setExcludeAncestral",
"r_name": "set_exclude_ancestral",
},
"add_exclude_ancestral": {
"params": ["field"],
"python_name": "add_exclude_ancestral",
"js_name": "addExcludeAncestral",
"r_name": "add_exclude_ancestral",
},
"set_exclude_descendant": {
"params": ["fields"],
"python_name": "set_exclude_descendant",
"js_name": "setExcludeDescendant",
"r_name": "set_exclude_descendant",
},
"add_exclude_descendant": {
"params": ["field"],
"python_name": "add_exclude_descendant",
"js_name": "addExcludeDescendant",
"r_name": "add_exclude_descendant",
},
"set_exclude_direct": {
"params": ["fields"],
"python_name": "set_exclude_direct",
"js_name": "setExcludeDirect",
"r_name": "set_exclude_direct",
},
"add_exclude_direct": {
"params": ["field"],
"python_name": "add_exclude_direct",
"js_name": "addExcludeDirect",
"r_name": "add_exclude_direct",
},
"set_exclude_missing": {
"params": ["fields"],
"python_name": "set_exclude_missing",
"js_name": "setExcludeMissing",
"r_name": "set_exclude_missing",
},
"add_exclude_missing": {
"params": ["field"],
"python_name": "add_exclude_missing",
"js_name": "addExcludeMissing",
"r_name": "add_exclude_missing",
},
"set_exclude_derived": {
"params": ["fields"],
"python_name": "set_exclude_derived",
"js_name": "setExcludeDerived",
"r_name": "set_exclude_derived",
},
"set_exclude_estimated": {
"params": ["fields"],
"python_name": "set_exclude_estimated",
"js_name": "setExcludeEstimated",
"r_name": "set_exclude_estimated",
},
"set_size": {
"params": ["size"],
"python_name": "set_size",
"js_name": "setSize",
"r_name": "set_size",
},
"set_page": {
"params": ["page"],
"python_name": "set_page",
"js_name": "setPage",
"r_name": "set_page",
},
"set_sort": {
"params": ["sort_by", "direction"],
"python_name": "set_sort",
"js_name": "setSort",
"r_name": "set_sort",
},
"set_include_estimates": {
"params": ["value"],
"python_name": "set_include_estimates",
"js_name": "setIncludeEstimates",
"r_name": "set_include_estimates",
},
"set_taxonomy": {
"params": ["taxonomy"],
"python_name": "set_taxonomy",
"js_name": "setTaxonomy",
"r_name": "set_taxonomy",
},
"to_query_yaml": {
"params": [],
"python_name": "to_query_yaml",
"js_name": "toQueryYaml",
"r_name": "to_query_yaml",
},
"to_params_yaml": {
"params": [],
"python_name": "to_params_yaml",
"js_name": "toParamsYaml",
"r_name": "to_params_yaml",
},
"to_url": {
"params": [],
"python_name": "to_url",
"js_name": "toUrl",
"r_name": "to_url",
},
"to_v2_url": {
"params": [],
"python_name": "to_v2_url",
"js_name": "toV2Url",
"r_name": "to_v2_url",
},
"from_v2_url": {
"params": ["url"],
"python_name": "from_v2_url",
"js_name": "fromV2Url",
"r_name": "from_v2_url",
},
"to_ui_url": {
"params": [],
"python_name": "to_ui_url",
"js_name": "toUiUrl",
"r_name": "to_ui_url",
},
"count": {
"params": [],
"python_name": "count",
"js_name": "count",
"r_name": "count",
},
"search": {
"params": ["format"],
"python_name": "search",
"js_name": "search",
"r_name": "search",
},
"search_all": {
"params": ["max_records"],
"python_name": "search_all",
"js_name": "searchAll",
"r_name": "search_all",
},
"validate": {
"params": [],
"python_name": "validate",
"js_name": "validate",
"r_name": "validate",
},
"describe": {
"params": ["field_metadata", "mode"],
"python_name": "describe",
"js_name": "describe",
"r_name": "describe",
},
"snippet": {
"params": ["languages", "site_name", "sdk_name", "api_base"],
"python_name": "snippet",
"js_name": "snippet",
"r_name": "snippet",
},
"reset": {
"params": [],
"python_name": "reset",
"js_name": "reset",
"r_name": "reset",
},
"merge": {
"params": ["other"],
"python_name": "merge",
"js_name": "merge",
"r_name": "merge",
},
"search_batch": {
"params": ["queries", "api_base", "api_version"],
"python_name": "search_batch",
"js_name": "searchBatch",
"r_name": "search_batch",
},
"count_batch": {
"params": ["queries", "api_base", "api_version"],
"python_name": "count_batch",
"js_name": "countBatch",
"r_name": "count_batch",
},
"record": {
"params": ["api_base", "api_version"],
"python_name": "record",
"js_name": "record",
"r_name": "record",
},
"record_batch": {
"params": ["record_ids"],
"python_name": "record_batch",
"js_name": "recordBatch",
"r_name": "record_batch",
},
"lookup": {
"params": ["api_base", "api_version"],
"python_name": "lookup",
"js_name": "lookup",
"r_name": "lookup",
},
"lookup_batch": {
"params": ["lookups"],
"python_name": "lookup_batch",
"js_name": "lookupBatch",
"r_name": "lookup_batch",
},
"summary": {
"params": ["api_base", "api_version"],
"python_name": "summary",
"js_name": "summary",
"r_name": "summary",
},
"set_lineage_rank_summary": {
"params": ["specs"],
"python_name": "set_lineage_rank_summary",
"js_name": "setLineageRankSummary",
"r_name": "set_lineage_rank_summary",
},
"set_lineage_summary_mode": {
"params": ["mode"],
"python_name": "set_lineage_summary_mode",
"js_name": "setLineageSummaryMode",
"r_name": "set_lineage_summary_mode",
},
"to_flat_records": {
"params": ["lineage_summary"],
"python_name": "to_flat_records",
"js_name": "toFlatRecords",
"r_name": "to_flat_records",
},
"to_tidy_records": {
"params": ["records", "lineage_summary"],
"python_name": "to_tidy_records",
"js_name": "toTidyRecords",
"r_name": "to_tidy_records",
},
"phylopic": {
"params": ["taxon_id", "taxonomy"],
"python_name": "phylopic",
"js_name": "phylopic",
"r_name": "phylopic",
},
"phylopic_batch": {
"params": ["taxon_ids", "taxonomy"],
"python_name": "phylopic_batch",
"js_name": "phylopicBatch",
"r_name": "phylopic_batch",
},
"metadata": {
"params": [],
"python_name": "metadata",
"js_name": "metadata",
"r_name": "metadata",
},
"indices": {
"params": [],
"python_name": "indices",
"js_name": "indices",
"r_name": "indices",
},
"fields": {
"params": ["index"],
"python_name": "fields",
"js_name": "fields",
"r_name": "fields",
},
"taxonomies": {
"params": [],
"python_name": "taxonomies",
"js_name": "taxonomies",
"r_name": "taxonomies",
},
"ranks": {
"params": [],
"python_name": "ranks",
"js_name": "ranks",
"r_name": "ranks",
},
"report": {
"params": ["report"],
"python_name": "report",
"js_name": "report",
"r_name": "report",
},
"chain_query": {
"params": ["query_key", "query_string"],
"python_name": "chain_query",
"js_name": "chainQuery",
"r_name": "chain_query",
},
"set_id_set": {
"params": ["taxon_ids"],
"python_name": "set_id_set",
"js_name": "setIdSet",
"r_name": "set_id_set",
},
"set_id_type": {
"params": ["id_type"],
"python_name": "set_id_type",
"js_name": "setIdType",
"r_name": "set_id_type",
},
"positional": {
"params": ["report", "group_by", "assemblies"],
"python_name": "positional",
"js_name": "positional",
"r_name": "positional",
},
"oxford": {
"params": ["group_by", "assemblies"],
"python_name": "oxford",
"js_name": "oxford",
"r_name": "oxford",
},
"ribbon": {
"params": ["group_by", "assemblies"],
"python_name": "ribbon",
"js_name": "ribbon",
"r_name": "ribbon",
},
"painting": {
"params": ["group_by", "assembly"],
"python_name": "painting",
"js_name": "painting",
"r_name": "painting",
},
"hybrid_positional": {
"params": ["report", "group_by", "local_files"],
"python_name": "hybrid_positional",
"js_name": "hybridPositional",
"r_name": "hybrid_positional",
},
}
CONSTRUCTOR_PARAMS: dict[str, dict[str, str]] = {}
# ── ReportBuilder canonical method definitions ────────────────────────────────
CANONICAL_REPORT_BUILDER_METHODS = {
"set_x": {"python_name": "set_x", "js_name": "setX", "r_name": "set_x"},
"set_y": {"python_name": "set_y", "js_name": "setY", "r_name": "set_y"},
"set_cat": {"python_name": "set_cat", "js_name": "setCat", "r_name": "set_cat"},
"set_query": {"python_name": "set_query", "js_name": "setQuery", "r_name": "set_query"},
"set_rank": {"python_name": "set_rank", "js_name": "setRank", "r_name": "set_rank"},
"set_ranks": {"python_name": "set_ranks", "js_name": "setRanks", "r_name": "set_ranks"},
"set_fields": {"python_name": "set_fields", "js_name": "setFields", "r_name": "set_fields"},
"set_status_filter": {
"python_name": "set_status_filter",
"js_name": "setStatusFilter",
"r_name": "set_status_filter",
},
"set_cat_rank": {"python_name": "set_cat_rank", "js_name": "setCatRank", "r_name": "set_cat_rank"},
"set_collapse_monotypic": {
"python_name": "set_collapse_monotypic",
"js_name": "setCollapseMonotypic",
"r_name": "set_collapse_monotypic",
},
"set_preserve_rank": {
"python_name": "set_preserve_rank",
"js_name": "setPreserveRank",
"r_name": "set_preserve_rank",
},
"set_count_rank": {"python_name": "set_count_rank", "js_name": "setCountRank", "r_name": "set_count_rank"},
"set_location_field": {
"python_name": "set_location_field",
"js_name": "setLocationField",
"r_name": "set_location_field",
},
"set_hex_resolution": {
"python_name": "set_hex_resolution",
"js_name": "setHexResolution",
"r_name": "set_hex_resolution",
},
"set_map_threshold": {
"python_name": "set_map_threshold",
"js_name": "setMapThreshold",
"r_name": "set_map_threshold",
},
"set_scatter_threshold": {
"python_name": "set_scatter_threshold",
"js_name": "setScatterThreshold",
"r_name": "set_scatter_threshold",
},
"set_display": {"python_name": "set_display", "js_name": "setDisplay", "r_name": "set_display"},
"set_include_plot_spec": {
"python_name": "set_include_plot_spec",
"js_name": "setIncludePlotSpec",
"r_name": "set_include_plot_spec",
},
"to_report_yaml": {"python_name": "to_report_yaml", "js_name": "toReportYaml", "r_name": "to_report_yaml"},
"validate": {"python_name": "validate", "js_name": "validate", "r_name": "validate"},
"run": {"python_name": "run", "js_name": "run", "r_name": "run"},
"set_feature": {"python_name": "set_feature", "js_name": "setFeature", "r_name": "set_feature"},
"set_reference": {"python_name": "set_reference", "js_name": "setReference", "r_name": "set_reference"},
"set_context": {"python_name": "set_context", "js_name": "setContext", "r_name": "set_context"},
"add_ring": {"python_name": "add_ring", "js_name": "addRing", "r_name": "add_ring"},
"set_arc_ranks": {"python_name": "set_arc_ranks", "js_name": "setArcRanks", "r_name": "set_arc_ranks"},
"set_axis_boundaries": {
"python_name": "set_axis_boundaries",
"js_name": "setAxisBoundaries",
"r_name": "set_axis_boundaries",
},
"set_axis_date_intervals": {
"python_name": "set_axis_date_intervals",
"js_name": "setAxisDateIntervals",
"r_name": "set_axis_date_intervals",
},
}
# ── Introspection functions ──────────────────────────────────────────────────
def get_python_constructor_params():
"""Extract constructor parameters from Python template."""
query_py_tera = PROJECT_ROOT / "templates" / "python" / "query.py.tera"
content = Path(query_py_tera).read_text()
# Find __init__ signature and extract parameter names
pattern = r"def __init__\s*\(\s*self,([^)]+)\)"
match = re.search(pattern, content, re.DOTALL)
if not match:
return []
params_str = match[1]
params = []
for p in params_str.split(","):
if p := p.strip():
param_name = p.split(":")[0].strip()
params.append(param_name)
return params
def get_js_constructor_params():
"""Extract constructor parameters from JavaScript template."""
query_js = PROJECT_ROOT / "templates" / "js" / "query.js"
content = Path(query_js).read_text()
# constructor(index, options = {}) pattern
pattern = r"constructor\s*\(([^)]+)\)"
match = re.search(pattern, content, re.DOTALL)
if not match:
return []
params_str = match[1]
params = []
for p in params_str.split(","):
if p := p.strip():
param_name = p.split("=")[0].strip()
params.append(param_name)
return params
def get_r_constructor_params():
"""Extract constructor parameters from R template."""
query_r = PROJECT_ROOT / "templates" / "r" / "query.R"
content = Path(query_r).read_text()
# Find initialize = function(...) pattern
pattern = r"initialize\s*=\s*function\s*\(([^)]+)\)"
match = re.search(pattern, content, re.DOTALL)
if not match:
return []
params_str = match[1]
params = []
for p in params_str.split(","):
p = p.strip()
if p and p != "self":
param_name = p.split("=")[0].strip()
params.append(param_name)
return params
def get_python_methods():
"""Extract all public methods from templates/python/query.py.tera (generated SDK)."""
# Use the template, not the main SDK
query_py_tera = PROJECT_ROOT / "templates" / "python" / "query.py.tera"
assert query_py_tera.exists(), f"Python query template not found at {query_py_tera}"
content = Path(query_py_tera).read_text()
# Rough parsing: look for "def method_name("
methods = {}
pattern = r"^\s{4}def\s+(\w+)\s*\("
for match in re.finditer(pattern, content, re.MULTILINE):
name = match.group(1)
if not name.startswith("_"):
# Find the full method signature (may span multiple lines)
# Look from the opening paren to the closing paren
paren_start = match.end() - 1 # Position of the '('
paren_depth = 0
paren_end = paren_start
for i in range(paren_start, len(content)):
if content[i] == "(":
paren_depth += 1
elif content[i] == ")":
paren_depth -= 1
if paren_depth == 0:
paren_end = i
break
# Extract parameters from the signature
params_str = content[paren_start + 1 : paren_end]
params = [p.strip() for p in params_str.split(",") if p.strip() and p.strip() != "self"]
# Remove type annotations and defaults
params = [p.split(":")[0].split("=")[0].strip() for p in params]
methods[name] = params
return methods
def get_js_methods():
"""Extract all public methods from templates/js/query.js."""
query_js = PROJECT_ROOT / "templates" / "js" / "query.js"
assert query_js.exists(), f"JavaScript query template not found at {query_js}"
content = Path(query_js).read_text()
methods = {}
# Look for method definitions: methodName(params) {
pattern = r"(\w+)\s*\(\s*([^)]*)\s*\)\s*{"
for match in re.finditer(pattern, content):
name = match.group(1)
if not name.startswith("_") and name not in ("constructor", "if", "for", "while"):
params_str = match.group(2)
params = [p.strip() for p in params_str.split(",") if p.strip() and p.strip() != "this"]
# Remove default values and destructuring
params = [p.split("=")[0].split("{")[0].split("}")[0].strip() for p in params]
methods[name] = [p for p in params if p]
return methods
def get_r_methods():
"""Extract all public methods from templates/r/query.R."""
query_r = PROJECT_ROOT / "templates" / "r" / "query.R"
assert query_r.exists(), f"R query template not found at {query_r}"
content = Path(query_r).read_text()
methods = {}
# Look for method definitions: method_name = function(...) {
pattern = r"(\w+)\s*=\s*function\s*\(([^)]*)\)"
for match in re.finditer(pattern, content):
name = match.group(1)
if not name.startswith("_") and name != "private":
params_str = match.group(2)
params = [p.strip() for p in params_str.split(",") if p.strip()]
params = [p.split("=")[0].strip() for p in params]
methods[name] = [p for p in params if p]
return methods
def get_python_report_builder_methods() -> dict[str, list[str]]:
"""Extract all public methods from the ReportBuilder class in the Python template."""
query_py_tera = PROJECT_ROOT / "templates" / "python" / "query.py.tera"
content = Path(query_py_tera).read_text()
class_match = re.search(r"^class ReportBuilder:", content, re.MULTILINE)
if not class_match:
return {}
rb_content = content[class_match.start() :]
methods: dict[str, list[str]] = {}
pattern = r"^\s{4}def\s+(\w+)\s*\("
for match in re.finditer(pattern, rb_content, re.MULTILINE):
name = match.group(1)
if name.startswith("_"):
continue
paren_start = match.end() - 1
paren_depth = 0
paren_end = paren_start
for i in range(paren_start, len(rb_content)):
if rb_content[i] == "(":
paren_depth += 1
elif rb_content[i] == ")":
paren_depth -= 1
if paren_depth == 0:
paren_end = i
break
params_str = rb_content[paren_start + 1 : paren_end]
params = [p.strip() for p in params_str.split(",") if p.strip() and p.strip() != "self"]
params = [p.split(":")[0].split("=")[0].strip() for p in params]
methods[name] = params
return methods
def get_js_report_builder_methods() -> dict[str, list[str]]:
"""Extract all public methods from the ReportBuilder class in the JS template."""
query_js = PROJECT_ROOT / "templates" / "js" / "query.js"
content = Path(query_js).read_text()
class_match = re.search(r"^class ReportBuilder\s*{", content, re.MULTILINE)
if not class_match:
return {}
rb_content = content[class_match.start() :]
methods: dict[str, list[str]] = {}
skip = {"constructor", "if", "for", "while", "async"}
pattern = r"(\w+)\s*\(\s*([^)]*)\s*\)\s*{"
for match in re.finditer(pattern, rb_content):
name = match.group(1)
if name.startswith("_") or name in skip:
continue
params_str = match.group(2)
params = [p.strip() for p in params_str.split(",") if p.strip()]
params = [p.split("=")[0].strip() for p in params]
methods[name] = [p for p in params if p]
return methods
def get_r_report_builder_methods() -> dict[str, list[str]]:
"""Extract all public methods from the ReportBuilder R6 class in the R template."""
query_r = PROJECT_ROOT / "templates" / "r" / "query.R"
content = Path(query_r).read_text()
class_match = re.search(r"ReportBuilder\s*<-\s*R6::R6Class", content)
if not class_match:
return {}
rb_content = content[class_match.start() :]
methods: dict[str, list[str]] = {}
skip = {"private", "initialize"}
pattern = r"(\w+)\s*=\s*function\s*\(([^)]*)\)"
for match in re.finditer(pattern, rb_content):
name = match.group(1)
if name.startswith("_") or name in skip:
continue
params_str = match.group(2)
params = [p.strip() for p in params_str.split(",") if p.strip()]
params = [p.split("=")[0].strip() for p in params]
methods[name] = [p for p in params if p]
return methods
def get_python_docstring(method_name: str) -> str:
"""Get the docstring for a Python template method.
For __init__, returns the class docstring (standard Python convention).
"""
query_py_tera = PROJECT_ROOT / "templates" / "python" / "query.py.tera"
content = Path(query_py_tera).read_text()
if method_name == "__init__":
# For __init__, return the class docstring (Python convention)
class_pattern = r"class\s+QueryBuilder.*?:\s*\n\s+\"\"\"(.*?)\"\"\""
match = re.search(class_pattern, content, re.DOTALL)
else:
# For other methods, search for "def method_name" and its docstring
method_pattern = rf'def\s+{method_name}\s*\([^)]*\).*?:\s*\n\s+"""(.*?)"""'
match = re.search(method_pattern, content, re.DOTALL)
return match[1].strip() if match else ""
# ── Tests ────────────────────────────────────────────────────────────────────
class TestSDKParity:
"""Test that all three SDKs have consistent method signatures."""
def test_python_canonical_methods_present(self):
"""All canonical methods must exist in Python SDK."""
python_methods = get_python_methods()
for concept, spec in CANONICAL_METHODS.items():
method_name = spec["python_name"]
assert method_name in python_methods, f"Python missing method: {method_name}"
def test_javascript_canonical_methods_present(self):
"""All canonical methods must exist in JavaScript SDK."""
js_methods = get_js_methods()
for concept, spec in CANONICAL_METHODS.items():
method_name = spec["js_name"]
assert method_name in js_methods, f"JavaScript missing method: {method_name}"
def test_r_canonical_methods_present(self):
"""All canonical methods must exist in R SDK."""
r_methods = get_r_methods()
for concept, spec in CANONICAL_METHODS.items():
method_name = spec["r_name"]
assert method_name in r_methods, f"R missing method: {method_name}"
def test_no_extra_methods_in_python(self):
"""Python should not have extra methods beyond canonical set."""
python_methods = get_python_methods()
canonical_python_names = {spec["python_name"] for spec in CANONICAL_METHODS.values()}
# Allow documented utility methods and Python-only internals
canonical_python_names.update(
[
"__init__",
"field_modifiers",
"field_names",
"field_info",
"combine",
"search_df",
"search_polars",
"_post_json", # Python-only: internal transport helper
]
)
# ReportBuilder canonical methods are also extracted by get_python_methods()
# since the file contains both classes; allow them here.
canonical_python_names.update({spec["python_name"] for spec in CANONICAL_REPORT_BUILDER_METHODS.values()})
extra = set(python_methods.keys()) - canonical_python_names
assert len(extra) == 0, f"Python has extra methods not in canonical list: {extra}"
def test_to_url_returns_v3_get_url(self):
"""to_url() must return a v3 GET URL with a ?url= parameter."""
import warnings
from cli_generator.query import QueryBuilder
qb = QueryBuilder("taxon").set_taxa(["Mammalia"])
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
url = qb.to_url()
assert "/v3/search?url=" in url, f"Expected v3 GET URL, got: {url}"
assert not any(
issubclass(w.category, DeprecationWarning) for w in caught
), "to_url() should not emit DeprecationWarning"
def test_to_url_warns_when_names_set(self):
"""to_url() emits RuntimeWarning when name classes are set."""
import warnings
from cli_generator.query import QueryBuilder
qb = QueryBuilder("taxon").set_taxa(["Mammalia"]).set_names(["scientific_name"])
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
qb.to_url()
assert any(
issubclass(w.category, RuntimeWarning) for w in caught
), "to_url() should emit RuntimeWarning when name classes are set"
def test_to_url_no_warning_for_simple_query(self):
"""to_url() emits no warning for a query with no non-roundtrippable features."""
import warnings
from cli_generator.query import QueryBuilder
qb = QueryBuilder("taxon").set_taxa(["Mammalia"])
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
qb.to_url()
assert not any(
issubclass(w.category, RuntimeWarning) for w in caught
), "to_url() should not emit RuntimeWarning for simple queries"
class TestValidationConfiguration:
"""Test that validation is properly implemented in templates."""
def test_python_validate_method_exists(self):
"""Python template should have validate() method."""
python_methods = get_python_methods()
assert "validate" in python_methods, "Python template missing validate() method"
def test_r_validate_method_exists(self):
"""R template should have validate() method."""
r_methods = get_r_methods()
assert "validate" in r_methods, "R template missing validate() method"
class TestDocumentationParity:
"""Test that Quarto reference documentation includes all SDK methods."""
def get_documented_methods(self) -> set[str]:
"""Return the set of canonical method names that appear in the Quarto reference.
Uses a simple membership check: a method is considered documented if its
name appears in a backtick context anywhere in the file. This is robust
against heading style, table vs list format, and any punctuation convention
— the only requirement is that the method name is mentioned at least once
as a backtick-quoted identifier.
"""
quarto_path = PROJECT_ROOT / "workdir/my-goat/goat-cli/docs/reference/query-builder.qmd"
if not quarto_path.exists():
pytest.skip(
f"Quarto reference guide not found at {quarto_path}. "
"This test requires the generated goat CLI project."
)
content = quarto_path.read_text()
canonical_names = set(CANONICAL_METHODS.keys())
return {name for name in canonical_names if f"`{name}" in content}
def test_documented_methods_include_all_canonical(self):
"""All canonical methods should be documented in Quarto reference."""
documented = self.get_documented_methods()
canonical_names = set(CANONICAL_METHODS.keys())
missing = canonical_names - documented
assert len(missing) == 0, f"Documentation missing these canonical methods: {sorted(missing)}"
def test_documented_methods_include_utilities(self):
"""Documentation should include documented utility methods."""
quarto_path = PROJECT_ROOT / "workdir/my-goat/goat-cli/docs/reference/query-builder.qmd"
if not quarto_path.exists():
pytest.skip(
f"Quarto reference guide not found at {quarto_path}. "
"This test requires the generated goat CLI project."
)
content = quarto_path.read_text()
# These utility methods are not in CANONICAL_METHODS (they are Python-only wrappers)
# but should still appear in the reference documentation.
utilities = {
"search_df", # pandas wrapper
"search_polars", # polars wrapper
"search_all", # pagination wrapper
}
for util in utilities:
assert f"`{util}" in content, f"Documentation missing utility method: {util}"
def test_documented_methods_reference_parameters(self):
"""Documented methods should include parameter tables where applicable."""
quarto_path = PROJECT_ROOT / "workdir/my-goat/goat-cli/docs/reference/query-builder.qmd"
if not quarto_path.exists():
pytest.skip(
f"Quarto reference guide not found at {quarto_path}. "
"This test requires the generated goat CLI project."
)
content = quarto_path.read_text()
# Check that key methods with parameters have tables
methods_with_params = {
"set_taxa": ["taxa", "filter_type"],
"add_attribute": ["name", "operator", "value", "modifiers"],
}
for method, expected_params in methods_with_params.items():
# Find the method section
method_pattern = rf"^###\s+`{method}\("
assert re.search(method_pattern, content, re.MULTILINE), f"Method {method} not found in documentation"
# Check for parameter table after the method heading
for param in expected_params:
param_pattern = rf"(?:{method}.*?){param}"
assert re.search(
param_pattern, content, re.DOTALL
), f"Parameter {param} for method {method} not documented"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
class TestGeneratedProjectWiring:
"""Verify that lib.rs.tera registrations and patch_python_init exports stay in sync.
These tests catch the class of bug where a function is added to one place
(e.g. new.rs __init__.py exports) but forgotten in the other (lib.rs.tera
pymodule registrations), which produces an ImportError at runtime.
"""
@staticmethod
def _registered_functions() -> set[str]:
"""Names registered via add_function(wrap_pyfunction!(NAME, m)?) in lib.rs.tera."""
lib_tera = PROJECT_ROOT / "templates" / "rust" / "lib.rs.tera"
content = lib_tera.read_text()
return set(re.findall(r"wrap_pyfunction!\((\w+),", content))
@staticmethod
def _exported_from_init() -> set[str]:
"""Names imported from the extension in the generated __init__.py (new.rs)."""
new_rs = PROJECT_ROOT / "src" / "commands" / "new.rs"
content = new_rs.read_text()
# The patch_python_init format string contains: from .{} import (\n name,\n ...
# Grab the block between the first `from .{} import (` and the matching `)`.
block_match = re.search(r"from \.\{\} import \((.+?)\)", content, re.DOTALL)
if not block_match:
return set()
return {n.strip().rstrip(",") for n in block_match.group(1).splitlines() if n.strip()}
def test_all_init_exports_are_registered(self) -> None:
"""Every name imported in the generated __init__.py must be registered in lib.rs.tera."""
registered = self._registered_functions()
# sdk-level functions (build_url, search, count, …) live in generated::sdk, not as
# top-level pyfunction wrappers — they are registered as sdk::name. Collect those too.
lib_tera = PROJECT_ROOT / "templates" / "rust" / "lib.rs.tera"
content = lib_tera.read_text()
sdk_functions = set(re.findall(r"wrap_pyfunction!\(sdk::(\w+),", content))
all_registered = registered | sdk_functions
exported = self._exported_from_init()
missing = exported - all_registered
assert not missing, (
f"Functions exported in __init__.py but NOT registered in lib.rs.tera: {missing}\n"
"Add a #[pyfunction] wrapper and m.add_function() call in templates/rust/lib.rs.tera"
)
def test_all_registered_functions_are_exported(self) -> None:
"""Every non-sdk pyfunction registered in lib.rs.tera should be exported in __init__.py."""
registered = self._registered_functions()
# sdk:: registrations are not individual function names in __init__.py
lib_tera = PROJECT_ROOT / "templates" / "rust" / "lib.rs.tera"
content = lib_tera.read_text()
sdk_functions = set(re.findall(r"wrap_pyfunction!\(sdk::(\w+),", content))
# Class registrations (add_class) are also not in the function export list
# Remove anything that is clearly a class (FieldInfo, Validator)
non_exported_ok = sdk_functions | {"build_url", "build_ui_url", "search", "count"}
exported = self._exported_from_init()
missing = (registered - non_exported_ok) - exported
assert not missing, (
f"Functions registered in lib.rs.tera but NOT exported in __init__.py: {missing}\n"
"Add the name to the import block in src/commands/new.rs patch_python_init()"
)
class TestReportBuilderParity:
"""ReportBuilder methods must be present in all three SDK languages."""
def test_python_report_builder_methods_present(self):
"""All canonical ReportBuilder methods must exist in the Python template."""