Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring test methods #8624

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
ACTION_DEFAULT_FALLBACK_NAME,
ACTION_DEACTIVATE_LOOP_NAME,
ACTION_REVERT_FALLBACK_EVENTS_NAME,
ACTION_DEFAULT_ASK_AFFIRMATION_NAME,
ACTION_DEFAULT_ASK_REPHRASE_NAME,
ACTION_BACK_NAME,
ACTION_TWO_STAGE_FALLBACK_NAME,
Expand Down Expand Up @@ -105,21 +104,26 @@ def test_domain_action_instantiation():
for action_name in domain.action_names_or_texts
]

assert len(instantiated_actions) == 14
assert instantiated_actions[0].name() == ACTION_LISTEN_NAME
assert instantiated_actions[1].name() == ACTION_RESTART_NAME
assert instantiated_actions[2].name() == ACTION_SESSION_START_NAME
assert instantiated_actions[3].name() == ACTION_DEFAULT_FALLBACK_NAME
assert instantiated_actions[4].name() == ACTION_DEACTIVATE_LOOP_NAME
assert instantiated_actions[5].name() == ACTION_REVERT_FALLBACK_EVENTS_NAME
assert instantiated_actions[6].name() == ACTION_DEFAULT_ASK_AFFIRMATION_NAME
assert instantiated_actions[7].name() == ACTION_DEFAULT_ASK_REPHRASE_NAME
assert instantiated_actions[8].name() == ACTION_TWO_STAGE_FALLBACK_NAME
assert instantiated_actions[9].name() == ACTION_BACK_NAME
assert instantiated_actions[10].name() == RULE_SNIPPET_ACTION_NAME
assert instantiated_actions[11].name() == "my_module.ActionTest"
assert instantiated_actions[12].name() == "utter_test"
assert instantiated_actions[13].name() == "utter_chitchat"
name_test_array = [
ACTION_LISTEN_NAME,
ACTION_RESTART_NAME,
ACTION_SESSION_START_NAME,
ACTION_DEFAULT_FALLBACK_NAME,
ACTION_DEACTIVATE_LOOP_NAME,
ACTION_REVERT_FALLBACK_EVENTS_NAME,
ACTION_DEFAULT_ASK_REPHRASE_NAME,
ACTION_TWO_STAGE_FALLBACK_NAME,
ACTION_BACK_NAME,
RULE_SNIPPET_ACTION_NAME,
"my_module.ActionTest",
"utter_test",
"utter_chitchat",
]

instantiated_actions_len = len(instantiated_actions)
assert instantiated_actions_len == 14
for i in range(instantiated_actions_len):
assert instantiated_actions[i].name == name_test_array
guilherme-mendes marked this conversation as resolved.
Show resolved Hide resolved


async def test_remote_action_runs(
Expand Down
29 changes: 13 additions & 16 deletions tests/nlu/featurizers/test_count_vectors_featurizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from rasa.nlu.tokenizers.tokenizer import Token
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.shared.nlu.training_data.message import Message
from tests.nlu.utilities import verify_sequence_not_none
from rasa.nlu.featurizers.sparse_featurizer.count_vectors_featurizer import (
CountVectorsFeaturizer,
)
Expand Down Expand Up @@ -432,25 +433,21 @@ def test_count_vector_featurizer_persist_load(tmp_path: Path):
test_ftr.process(test_message2)

test_seq_vec_1, test_sen_vec_1 = test_message1.get_sparse_features(TEXT, [])
if test_seq_vec_1:
test_seq_vec_1 = test_seq_vec_1.features
if test_sen_vec_1:
test_sen_vec_1 = test_sen_vec_1.features
test_seq_vec_1, test_sen_vec_1 = verify_sequence_not_none(
test_seq_vec_1, test_sen_vec_1
)
train_seq_vec_1, train_sen_vec_1 = train_message1.get_sparse_features(TEXT, [])
if train_seq_vec_1:
train_seq_vec_1 = train_seq_vec_1.features
if train_sen_vec_1:
train_sen_vec_1 = train_sen_vec_1.features
train_seq_vec_1, train_sen_vec_1 = verify_sequence_not_none(
train_seq_vec_1, train_sen_vec_1
)
test_seq_vec_2, test_sen_vec_2 = test_message2.get_sparse_features(TEXT, [])
if test_seq_vec_2:
test_seq_vec_2 = test_seq_vec_2.features
if test_sen_vec_2:
test_sen_vec_2 = test_sen_vec_2.features
test_seq_vec_2, test_sen_vec_2 = verify_sequence_not_none(
test_seq_vec_2, test_sen_vec_2
)
train_seq_vec_2, train_sen_vec_2 = train_message2.get_sparse_features(TEXT, [])
if train_seq_vec_2:
train_seq_vec_2 = train_seq_vec_2.features
if train_sen_vec_2:
train_sen_vec_2 = train_sen_vec_2.features
train_seq_vec_2, train_sen_vec_2 = verify_sequence_not_none(
train_seq_vec_2, train_sen_vec_2
)

# check that train features and test features after loading are the same
assert np.all(test_seq_vec_1.toarray() == train_seq_vec_1.toarray())
Expand Down
9 changes: 9 additions & 0 deletions tests/nlu/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def write_file_config(file_config):
return f


def verify_sequence_not_none(sequence, sentence):
guilherme-mendes marked this conversation as resolved.
Show resolved Hide resolved
if sequence and sentence:
guilherme-mendes marked this conversation as resolved.
Show resolved Hide resolved
return sequence.features, sentence.features
if sequence and not sentence:
return sequence.features, None
if not sequence and sentence:
return None, sentence.features


class ResponseTest:
def __init__(self, endpoint, expected_response, payload=None):
self.endpoint = endpoint
Expand Down