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 all 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
39 changes: 22 additions & 17 deletions tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,28 @@ def test_domain_action_instantiation():
for action_name in domain.action_names_or_texts
]

assert len(instantiated_actions) == 16
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_UNLIKELY_INTENT_NAME
assert instantiated_actions[10].name() == ACTION_BACK_NAME
assert instantiated_actions[11].name() == RULE_SNIPPET_ACTION_NAME
assert instantiated_actions[12].name() == ACTION_EXTRACT_SLOTS
assert instantiated_actions[13].name() == "my_module.ActionTest"
assert instantiated_actions[14].name() == "utter_test"
assert instantiated_actions[15].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_AFFIRMATION_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[i]



async def test_remote_action_runs(
Expand Down
25 changes: 9 additions & 16 deletions tests/nlu/featurizers/test_count_vectors_featurizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 get_feature_vectors
from rasa.nlu.featurizers.sparse_featurizer.count_vectors_featurizer import (
CountVectorsFeaturizer,
)
Expand Down Expand Up @@ -478,25 +479,17 @@ def test_count_vector_featurizer_persist_load(
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 = get_feature_vectors(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 = get_feature_vectors(
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 = get_feature_vectors(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 = get_feature_vectors(
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
8 changes: 8 additions & 0 deletions tests/nlu/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def write_file_config(file_config):
return f


# check if os sequences e sentences is loaded correctly
def get_feature_vectors(sequence, sentence):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add type annotations for the arguments and return types. Also, our convention is to put descriptions into the function's doc string, i.e.

def get_feature_vectors(sequence: ???, sentence: ???) -> Tuple[Optional[???], Optional[???]]:
  """Retrieves feature vectors from the features.
  
  Args:
    sequence: Features of individual tokens.
    sentence: Features of the entire string.
  
  Returns:
    Feature vectors (or None) for sequence and sentence features.
  """
  ...

You'll have to substitute the right thing for the question marks (just follow the return type of test_message1.get_sparse_features). Sorry I didn't see this earlier.

return (
sequence.features if sequence else None,
sentence.features if sentence else None,
)


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