|
| 1 | +import argparse |
| 2 | +import pytest |
| 3 | +from typing import Callable, Text |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +from _pytest.monkeypatch import MonkeyPatch |
| 7 | +from _pytest.pytester import RunResult |
| 8 | + |
| 9 | +import rasa |
| 10 | +from rasa.cli import interactive, train |
| 11 | + |
| 12 | + |
| 13 | +def test_interactive_help(run: Callable[..., RunResult]): |
| 14 | + output = run("interactive", "--help") |
| 15 | + |
| 16 | + help_text = """usage: rasa interactive [-h] [-v] [-vv] [--quiet] [--e2e] [-m MODEL] |
| 17 | + [--data DATA [DATA ...]] [--skip-visualization] |
| 18 | + [--endpoints ENDPOINTS] [-c CONFIG] [-d DOMAIN] |
| 19 | + [--out OUT] [--augmentation AUGMENTATION] |
| 20 | + [--debug-plots] [--dump-stories] [--force] |
| 21 | + [--persist-nlu-data] |
| 22 | + {core} ... [model-as-positional-argument]""" |
| 23 | + |
| 24 | + lines = help_text.split("\n") |
| 25 | + |
| 26 | + for i, line in enumerate(lines): |
| 27 | + assert output.outlines[i] == line |
| 28 | + |
| 29 | + |
| 30 | +def test_interactive_core_help(run: Callable[..., RunResult]): |
| 31 | + output = run("interactive", "core", "--help") |
| 32 | + |
| 33 | + help_text = """usage: rasa interactive core [-h] [-v] [-vv] [--quiet] [-m MODEL] [-s STORIES] |
| 34 | + [--skip-visualization] [--endpoints ENDPOINTS] |
| 35 | + [-c CONFIG] [-d DOMAIN] [--out OUT] |
| 36 | + [--augmentation AUGMENTATION] [--debug-plots] |
| 37 | + [--dump-stories] |
| 38 | + [model-as-positional-argument]""" |
| 39 | + |
| 40 | + lines = help_text.split("\n") |
| 41 | + |
| 42 | + for i, line in enumerate(lines): |
| 43 | + assert output.outlines[i] == line |
| 44 | + |
| 45 | + |
| 46 | +def test_pass_arguments_to_rasa_train( |
| 47 | + default_stack_config: Text, monkeypatch: MonkeyPatch |
| 48 | +) -> None: |
| 49 | + # Create parser |
| 50 | + parser = argparse.ArgumentParser() |
| 51 | + sub_parser = parser.add_subparsers() |
| 52 | + interactive.add_subparser(sub_parser, []) |
| 53 | + |
| 54 | + # Parse interactive command |
| 55 | + args = parser.parse_args(["interactive", "--config", default_stack_config]) |
| 56 | + interactive._set_not_required_args(args) |
| 57 | + |
| 58 | + # Mock actual training |
| 59 | + mock = Mock() |
| 60 | + monkeypatch.setattr(rasa, "train", mock.method) |
| 61 | + |
| 62 | + # If the `Namespace` object does not have all required fields this will throw |
| 63 | + train.train(args) |
| 64 | + |
| 65 | + # Assert `train` was actually called |
| 66 | + mock.method.assert_called_once() |
| 67 | + |
| 68 | + |
| 69 | +def test_train_called_when_no_model_passed( |
| 70 | + default_stack_config: Text, monkeypatch: MonkeyPatch |
| 71 | +) -> None: |
| 72 | + parser = argparse.ArgumentParser() |
| 73 | + sub_parser = parser.add_subparsers() |
| 74 | + interactive.add_subparser(sub_parser, []) |
| 75 | + |
| 76 | + args = parser.parse_args( |
| 77 | + [ |
| 78 | + "interactive", |
| 79 | + "--config", |
| 80 | + default_stack_config, |
| 81 | + "--data", |
| 82 | + "examples/moodbot/data", |
| 83 | + ] |
| 84 | + ) |
| 85 | + interactive._set_not_required_args(args) |
| 86 | + |
| 87 | + # Mock actual training and interactive learning methods |
| 88 | + mock = Mock() |
| 89 | + monkeypatch.setattr(train, "train", mock.train_model) |
| 90 | + monkeypatch.setattr( |
| 91 | + interactive, "perform_interactive_learning", mock.perform_interactive_learning |
| 92 | + ) |
| 93 | + |
| 94 | + interactive.interactive(args) |
| 95 | + mock.train_model.assert_called_once() |
| 96 | + |
| 97 | + |
| 98 | +def test_train_core_called_when_no_model_passed_and_core( |
| 99 | + default_stack_config: Text, monkeypatch: MonkeyPatch |
| 100 | +) -> None: |
| 101 | + parser = argparse.ArgumentParser() |
| 102 | + sub_parser = parser.add_subparsers() |
| 103 | + interactive.add_subparser(sub_parser, []) |
| 104 | + |
| 105 | + args = parser.parse_args( |
| 106 | + [ |
| 107 | + "interactive", |
| 108 | + "core", |
| 109 | + "--config", |
| 110 | + default_stack_config, |
| 111 | + "--stories", |
| 112 | + "examples/moodbot/data/stories.md", |
| 113 | + "--domain", |
| 114 | + "examples/moodbot/domain.yml", |
| 115 | + ] |
| 116 | + ) |
| 117 | + interactive._set_not_required_args(args) |
| 118 | + |
| 119 | + # Mock actual training and interactive learning methods |
| 120 | + mock = Mock() |
| 121 | + monkeypatch.setattr(train, "train_core", mock.train_core) |
| 122 | + monkeypatch.setattr( |
| 123 | + interactive, "perform_interactive_learning", mock.perform_interactive_learning |
| 124 | + ) |
| 125 | + |
| 126 | + interactive.interactive(args) |
| 127 | + mock.train_core.assert_called_once() |
| 128 | + |
| 129 | + |
| 130 | +def test_no_interactive_without_core_data( |
| 131 | + default_stack_config: Text, monkeypatch: MonkeyPatch |
| 132 | +) -> None: |
| 133 | + parser = argparse.ArgumentParser() |
| 134 | + sub_parser = parser.add_subparsers() |
| 135 | + interactive.add_subparser(sub_parser, []) |
| 136 | + |
| 137 | + args = parser.parse_args( |
| 138 | + [ |
| 139 | + "interactive", |
| 140 | + "--config", |
| 141 | + default_stack_config, |
| 142 | + "--data", |
| 143 | + "examples/moodbot/data/nlu.md", |
| 144 | + ] |
| 145 | + ) |
| 146 | + interactive._set_not_required_args(args) |
| 147 | + |
| 148 | + mock = Mock() |
| 149 | + monkeypatch.setattr(train, "train", mock.train_model) |
| 150 | + monkeypatch.setattr( |
| 151 | + interactive, "perform_interactive_learning", mock.perform_interactive_learning |
| 152 | + ) |
| 153 | + |
| 154 | + with pytest.raises(SystemExit): |
| 155 | + interactive.interactive(args) |
| 156 | + |
| 157 | + mock.train_model.assert_not_called() |
| 158 | + mock.perform_interactive_learning.assert_not_called() |
0 commit comments