-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathtest_validation.py
1222 lines (1001 loc) · 35.2 KB
/
test_validation.py
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
from typing import Any, Callable, Dict, Text, Tuple, Type, Optional, List
from collections import namedtuple
import itertools
from unittest.mock import Mock
import pytest
from rasa.core.policies.policy import PolicyPrediction
from rasa.engine import validation
from rasa.engine.exceptions import GraphSchemaValidationException
from rasa.engine.graph import (
GraphComponent,
ExecutionContext,
GraphSchema,
SchemaNode,
GraphModelConfiguration,
)
from rasa.engine.constants import PLACEHOLDER_IMPORTER
from rasa.engine.storage.resource import Resource
from rasa.engine.storage.storage import ModelStorage
from rasa.shared.core.domain import Domain
from rasa.shared.data import TrainingType
from rasa.shared.importers.importer import TrainingDataImporter
from rasa.shared.nlu.training_data.message import Message
from rasa.shared.nlu.training_data.training_data import TrainingData
class TestComponentWithoutRun(GraphComponent):
@classmethod
def create(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
) -> GraphComponent:
return cls()
class TestComponentWithRun(TestComponentWithoutRun):
def run(self) -> TrainingData:
pass
class TestComponentWithRunAndParam(TestComponentWithoutRun):
def run(self, training_data: TrainingData) -> TrainingData:
pass
class TestNLUTarget(TestComponentWithoutRun):
def run(self) -> List[Message]:
pass
class TestCoreTarget(TestComponentWithoutRun):
def run(self) -> PolicyPrediction:
pass
class TestComponentWithClsTypeHints(GraphComponent):
@classmethod
def create(
cls: "TestComponentWithClsTypeHints",
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
) -> GraphComponent:
return cls()
DEFAULT_PREDICT_SCHEMA = GraphSchema(
{
"nlu_target": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="load",
fn="run",
config={},
)
}
)
def create_test_schema(
uses: Type, # The unspecified type is on purpose to enable testing of invalid cases
constructor_name: Text = "create",
run_fn: Text = "run",
needs: Optional[Dict[Text, Text]] = None,
eager: bool = True,
parent: Optional[Type[GraphComponent]] = None,
language: Optional[Text] = None,
is_train_graph: bool = True,
) -> GraphModelConfiguration:
parent_node = {}
if parent:
parent_node = {
"parent": SchemaNode(
needs={}, uses=parent, constructor_name="create", fn="run", config={}
)
}
train_schema = GraphSchema({})
predict_schema = DEFAULT_PREDICT_SCHEMA
# noinspection PyTypeChecker
schema = GraphSchema(
{
"my_node": SchemaNode(
needs=needs or {},
uses=uses,
eager=eager,
constructor_name=constructor_name,
fn=run_fn,
config={},
),
**DEFAULT_PREDICT_SCHEMA.nodes,
**parent_node,
}
)
if is_train_graph:
train_schema = schema
else:
predict_schema = schema
return GraphModelConfiguration(
train_schema=train_schema,
predict_schema=predict_schema,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
core_target=None,
nlu_target="nlu_target",
language=language,
)
def test_graph_component_is_no_graph_component():
class MyComponent:
def other(self) -> TrainingData:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="implement .+ interface"):
validation.validate(graph_config)
def test_graph_component_fn_does_not_exist():
graph_config = create_test_schema(uses=TestComponentWithRun, run_fn="some_fn")
with pytest.raises(
GraphSchemaValidationException, match="required method 'some_fn'"
):
validation.validate(graph_config)
def test_graph_output_is_not_fingerprintable_int():
class MyComponent(TestComponentWithoutRun):
def run(self) -> int:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="fingerprintable"):
validation.validate(graph_config)
def test_predict_graph_output_is_not_fingerprintable():
class MyComponent(TestComponentWithoutRun):
def run(self) -> int:
pass
graph_config = create_test_schema(uses=MyComponent, is_train_graph=False)
validation.validate(graph_config)
def test_graph_output_is_not_fingerprintable_any():
class MyComponent(TestComponentWithoutRun):
def run(self) -> Any:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="fingerprintable"):
validation.validate(graph_config)
def test_graph_output_is_not_fingerprintable_None():
class MyComponent(TestComponentWithoutRun):
def run(self) -> None:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="fingerprintable"):
validation.validate(graph_config)
def test_graph_with_forward_referenced_output_type():
class MyComponent(TestComponentWithoutRun):
# The non imported type annotation is on purpose so we can provoke a error in
# the test
def run(self) -> "UserUttered": # noqa: F821
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="forward reference"):
validation.validate(graph_config)
def test_graph_output_missing_type_annotation():
class MyComponent(TestComponentWithoutRun):
def run(self):
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(
GraphSchemaValidationException, match="does not have a type annotation"
):
validation.validate(graph_config)
def test_graph_with_fingerprintable_output():
class MyComponent(TestComponentWithoutRun):
def run(self) -> TrainingData:
pass
graph_config = create_test_schema(uses=MyComponent)
validation.validate(graph_config)
class MyTrainingData(TrainingData):
pass
def test_graph_with_fingerprintable_output_subclass():
class MyComponent(TestComponentWithoutRun):
def run(self) -> MyTrainingData:
pass
graph_config = create_test_schema(uses=MyComponent)
validation.validate(graph_config)
def test_graph_constructor_missing():
class MyComponent(TestComponentWithoutRun):
def run(self) -> TrainingData:
pass
graph_config = create_test_schema(uses=MyComponent, constructor_name="invalid")
with pytest.raises(
GraphSchemaValidationException, match="required method 'invalid'"
):
validation.validate(graph_config)
def test_graph_constructor_config_wrong_type():
class MyComponent(TestComponentWithRun):
@classmethod
def create(
cls,
config: Dict[int, int],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
) -> GraphComponent:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="incompatible type"):
validation.validate(graph_config)
def test_graph_constructor_resource_wrong_type():
class MyComponent(TestComponentWithRun):
@classmethod
def create(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Dict,
execution_context: ExecutionContext,
) -> GraphComponent:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="incompatible type"):
validation.validate(graph_config)
def test_graph_constructor_model_storage_wrong_type():
class MyComponent(TestComponentWithRun):
@classmethod
def create(
cls,
config: Dict[Text, Any],
model_storage: Any,
resource: Resource,
execution_context: ExecutionContext,
) -> GraphComponent:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="incompatible type"):
validation.validate(graph_config)
def test_graph_constructor_execution_context_wrong_type():
class MyComponent(TestComponentWithRun):
@classmethod
def create(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: Any,
) -> GraphComponent:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="incompatible type"):
validation.validate(graph_config)
@pytest.mark.parametrize(
"current_language, supported_languages",
[("de", ["en"]), ("en", ["zh", "fi"]), ("us", [])],
)
def test_graph_constructor_execution_not_supported_language(
current_language: Text, supported_languages: Optional[List[Text]]
):
class MyComponent(TestComponentWithRun):
@staticmethod
def supported_languages() -> Optional[List[Text]]:
return supported_languages
graph_config = create_test_schema(uses=MyComponent, language=current_language)
with pytest.raises(
GraphSchemaValidationException, match="does not support .* language"
):
validation.validate(graph_config)
@pytest.mark.parametrize(
"current_language, supported_languages",
[(None, None), ("en", ["zh", "en"]), ("zh", None), (None, ["en"])],
)
def test_graph_constructor_execution_supported_language(
current_language: Optional[Text], supported_languages: Optional[List[Text]]
):
class MyComponent(TestComponentWithRun):
@staticmethod
def supported_languages() -> Optional[List[Text]]:
return supported_languages
graph_config = create_test_schema(uses=MyComponent, language=current_language)
validation.validate(graph_config)
@pytest.mark.parametrize(
"current_language, not_supported_languages", [("de", ["de", "en"]), ("en", ["en"])]
)
def test_graph_constructor_execution_exclusive_list_not_supported_language(
current_language: Text, not_supported_languages: Optional[List[Text]]
):
class MyComponent(TestComponentWithRun):
@staticmethod
def not_supported_languages() -> Optional[List[Text]]:
return not_supported_languages
graph_config = create_test_schema(
uses=MyComponent, language=current_language, is_train_graph=False
)
with pytest.raises(
GraphSchemaValidationException, match="does not support .* language"
):
validation.validate(graph_config)
@pytest.mark.parametrize(
"current_language, not_supported_languages",
[(None, None), ("en", ["zh"]), ("zh", None), (None, ["de"])],
)
def test_graph_constructor_execution_exclusive_list_supported_language(
current_language: Optional[Text], not_supported_languages: Optional[List[Text]]
):
class MyComponent(TestComponentWithRun):
@staticmethod
def not_supported_languages() -> Optional[List[Text]]:
return not_supported_languages
graph_config = create_test_schema(
uses=MyComponent, language=current_language, is_train_graph=False
)
validation.validate(graph_config)
@pytest.mark.parametrize(
"required_packages", [["pytorch"], ["tensorflow", "kubernetes"]]
)
def test_graph_missing_package_requirements(required_packages: List[Text]):
class MyComponent(TestComponentWithRun):
@staticmethod
def required_packages() -> List[Text]:
"""Any extra python dependencies required for this component to run."""
return required_packages
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="not installed"):
validation.validate(graph_config)
@pytest.mark.parametrize("required_packages", [["tensorflow"], ["tensorflow", "numpy"]])
def test_graph_satisfied_package_requirements(required_packages: List[Text]):
class MyComponent(TestComponentWithRun):
@staticmethod
def required_packages() -> List[Text]:
"""Any extra python dependencies required for this component to run."""
return required_packages
graph_config = create_test_schema(uses=MyComponent)
validation.validate(graph_config)
def test_run_param_not_satisfied():
class MyComponent(TestComponentWithoutRun):
def run(self, some_param: TrainingData) -> TrainingData:
pass
graph_config = create_test_schema(uses=MyComponent)
with pytest.raises(GraphSchemaValidationException, match="needs the param"):
validation.validate(graph_config)
def test_run_param_satifisfied_due_to_default():
class MyComponent(TestComponentWithoutRun):
def run(self, some_param: TrainingData = TrainingData()) -> TrainingData:
pass
graph_config = create_test_schema(uses=MyComponent)
validation.validate(graph_config)
def test_too_many_supplied_params():
graph_config = create_test_schema(
uses=TestComponentWithRun, needs={"some_param": "parent"}
)
with pytest.raises(
GraphSchemaValidationException, match="does not accept a parameter"
):
validation.validate(graph_config)
def test_too_many_supplied_params_but_kwargs():
class MyComponent(TestComponentWithoutRun):
def run(self, **kwargs: Any) -> TrainingData:
pass
graph_config = create_test_schema(
uses=MyComponent, needs={"some_param": "parent"}, parent=TestComponentWithRun
)
validation.validate(graph_config)
def test_run_fn_with_variable_length_positional_param():
class MyComponent(TestComponentWithoutRun):
def run(self, *args: Any, some_param: TrainingData) -> TrainingData:
pass
graph_config = create_test_schema(
uses=MyComponent, needs={"some_param": "parent"}, parent=TestComponentWithRun
)
validation.validate(graph_config)
def test_matching_params_due_to_constructor():
class MyComponent(TestComponentWithRun):
@classmethod
def load(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
some_param: TrainingData,
) -> GraphComponent:
pass
graph_config = create_test_schema(
uses=MyComponent,
needs={"some_param": "parent"},
eager=False,
constructor_name="load",
parent=TestComponentWithRun,
)
validation.validate(graph_config)
def test_matching_params_due_to_constructor_but_eager():
class MyComponent(TestComponentWithRun):
@classmethod
def load(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
some_param: TrainingData,
) -> GraphComponent:
pass
graph_config = create_test_schema(
uses=MyComponent,
needs={"some_param": "parent"},
eager=True,
constructor_name="load",
)
with pytest.raises(
GraphSchemaValidationException, match="which is used during training"
):
validation.validate(graph_config)
@pytest.mark.parametrize(
"eager, error_message", [(True, "during training"), (False, "needs the param")]
)
def test_unsatisfied_constructor(eager: bool, error_message: Text):
class MyComponent(TestComponentWithRun):
@classmethod
def load(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
some_param: TrainingData,
) -> GraphComponent:
pass
graph_config = create_test_schema(
uses=MyComponent, eager=eager, constructor_name="load"
)
with pytest.raises(GraphSchemaValidationException, match=error_message):
validation.validate(graph_config)
def test_parent_is_missing():
graph_config = create_test_schema(
uses=TestComponentWithRunAndParam,
needs={"training_data": "not existing parent"},
)
with pytest.raises(
GraphSchemaValidationException, match="The component is missing from"
):
validation.validate(graph_config)
def test_parent_supplying_wrong_type():
class MyUnreliableParent(TestComponentWithoutRun):
def run(self) -> Domain:
pass
graph_config = create_test_schema(
uses=TestComponentWithRunAndParam,
parent=MyUnreliableParent,
needs={"training_data": "parent"},
)
with pytest.raises(
GraphSchemaValidationException, match="expects an input of type"
):
validation.validate(graph_config)
def test_parent_supplying_wrong_type_to_constructor():
class MyUnreliableParent(TestComponentWithoutRun):
def run(self) -> Domain:
pass
class MyComponent(TestComponentWithRun):
@classmethod
def load(
cls,
config: Dict[Text, Any],
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
some_param: TrainingData,
) -> GraphComponent:
pass
graph_config = create_test_schema(
uses=MyComponent,
eager=False,
constructor_name="load",
parent=MyUnreliableParent,
needs={"some_param": "parent"},
)
with pytest.raises(
GraphSchemaValidationException, match="expects an input of type"
):
validation.validate(graph_config)
def test_parent_supplying_subtype():
class Parent(TestComponentWithoutRun):
def run(self) -> MyTrainingData:
pass
class MyComponent(TestComponentWithoutRun):
def run(self, training_data: TrainingData) -> TrainingData:
pass
graph_config = create_test_schema(
uses=MyComponent, parent=Parent, needs={"training_data": "parent"}
)
validation.validate(graph_config)
def test_child_accepting_any_type_from_parent():
class Parent(TestComponentWithoutRun):
def run(self) -> MyTrainingData:
pass
class MyComponent(TestComponentWithoutRun):
def run(self, training_data: Any) -> TrainingData:
pass
graph_config = create_test_schema(
uses=MyComponent, parent=Parent, needs={"training_data": "parent"}
)
validation.validate(graph_config)
@pytest.mark.parametrize("is_train_graph", [True, False])
def test_cycle(is_train_graph: bool):
class MyTestComponent(TestComponentWithoutRun):
def run(self, training_data: TrainingData) -> TrainingData:
pass
train_schema = GraphSchema({})
predict_schema = DEFAULT_PREDICT_SCHEMA
schema = GraphSchema(
{
"A": SchemaNode(
needs={"training_data": "B"},
uses=MyTestComponent,
eager=True,
constructor_name="create",
fn="run",
is_target=True,
config={},
),
"B": SchemaNode(
needs={"training_data": "C"},
uses=MyTestComponent,
eager=True,
constructor_name="create",
fn="run",
config={},
),
"C": SchemaNode(
needs={"training_data": "A"},
uses=MyTestComponent,
eager=True,
constructor_name="create",
fn="run",
config={},
),
}
)
if is_train_graph:
train_schema = schema
else:
predict_schema = schema
with pytest.raises(GraphSchemaValidationException, match="Cycles"):
validation.validate(
GraphModelConfiguration(
train_schema=train_schema,
predict_schema=predict_schema,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target=None,
nlu_target="nlu_target",
)
)
def test_validation_with_placeholders():
class MyTestComponent(TestComponentWithoutRun):
def run(self, training_data: TrainingDataImporter) -> TrainingDataImporter:
pass
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={"training_data": "B"},
uses=MyTestComponent,
eager=True,
constructor_name="create",
fn="run",
is_target=True,
config={},
),
"B": SchemaNode(
needs={"training_data": PLACEHOLDER_IMPORTER},
uses=MyTestComponent,
eager=True,
constructor_name="create",
fn="run",
config={},
),
}
)
# Does not raise
validation.validate(
GraphModelConfiguration(
train_schema=graph_config,
predict_schema=DEFAULT_PREDICT_SCHEMA,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target=None,
nlu_target="nlu_target",
)
)
def test_validation_with_missing_nlu_target():
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
)
}
)
with pytest.raises(
GraphSchemaValidationException, match="no target for the 'nlu_target'"
):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target=None,
nlu_target=None,
)
)
def test_validation_with_nlu_target_used_by_other_node():
class NLUTargetConsumer(TestComponentWithoutRun):
def run(self, nlu_target_output: List[Message]) -> List[Message]:
pass
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
),
"B": SchemaNode(
needs={"nlu_target_output": "A"},
uses=NLUTargetConsumer,
eager=True,
constructor_name="create",
fn="run",
config={},
),
}
)
with pytest.raises(
GraphSchemaValidationException, match="uses the NLU target 'A' as input"
):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target=None,
nlu_target="A",
)
)
def test_validation_with_nlu_target_wrong_type():
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestCoreTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
)
}
)
with pytest.raises(GraphSchemaValidationException, match="invalid return type"):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target=None,
nlu_target="A",
)
)
def test_validation_with_missing_core_target():
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
)
}
)
with pytest.raises(GraphSchemaValidationException, match="invalid Core target"):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target="B",
nlu_target="A",
)
)
def test_validation_with_core_target_wrong_type():
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
)
}
)
with pytest.raises(
GraphSchemaValidationException, match="Core model's .* invalid return type"
):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target="A",
nlu_target="A",
)
)
def test_validation_with_core_target_used_by_other_node():
class CoreTargetConsumer(TestComponentWithoutRun):
def run(self, core_target_output: PolicyPrediction) -> PolicyPrediction:
pass
graph_config = GraphSchema(
{
"A": SchemaNode(
needs={},
uses=TestNLUTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
),
"B": SchemaNode(
needs={},
uses=TestCoreTarget,
eager=True,
constructor_name="create",
fn="run",
config={},
),
"C": SchemaNode(
needs={"core_target_output": "B"},
uses=CoreTargetConsumer,
eager=True,
constructor_name="create",
fn="run",
config={},
),
}
)
with pytest.raises(
GraphSchemaValidationException, match="uses the Core target 'B' as input"
):
validation.validate(
GraphModelConfiguration(
train_schema=GraphSchema({}),
predict_schema=graph_config,
training_type=TrainingType.BOTH,
assistant_id="test_assistant",
language=None,
core_target="B",
nlu_target="A",
)
)
def _create_run_function(num_args) -> Callable[..., TrainingData]:
# Note: setting __annotations__ is not sufficient for the validation and
# creating a function via types.FunctionType is cumbersome, so we just
# explicitly create the function we need:
if num_args == 0:
def run() -> TrainingData:
return TrainingData()
elif num_args == 1:
def run(param0: TrainingData) -> TrainingData:
return TrainingData()
elif num_args == 2:
def run(param0: TrainingData, param1: TrainingData) -> TrainingData:
return TrainingData()
elif num_args == 3:
def run(
param0: TrainingData, param1: TrainingData, param2: TrainingData
) -> TrainingData:
return TrainingData()
else:
assert False, f"This test doesn't work with num_args={num_args} ."
return run