Skip to content

Commit

Permalink
Merge pull request RasaHQ#872 from RasaHQ/test-cleanups
Browse files Browse the repository at this point in the history
cleaned up tests and used moto mocking for s3
  • Loading branch information
tmbo authored Feb 28, 2018
2 parents 98e4ab8 + ba51a43 commit b3e69d8
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ test_models/model_*
test_projects/test_project_*

rasa_nlu/tmbo_test.py
.mypy_cache/
.mypy_cache/
*.tar.gz
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ python:
- '2.7'
- '3.5'
- '3.6'
env:
# needed to fix issues with boto during testing:
# https://github.com/travis-ci/travis-ci/issues/7940
global: BOTO_CONFIG=/dev/null
install:
- pip install git+https://github.com/tmbo/MITIE.git
- pip install -r alt_requirements/requirements_dev.txt
Expand Down
1 change: 1 addition & 0 deletions alt_requirements/requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pytest-cov==2.5.1
pytest-twisted==1.6
pytest==3.3.2
treq==17.8.0
moto==1.2.0
mock==2.0.0
# other
google-cloud-storage==1.7.0
Expand Down
8 changes: 4 additions & 4 deletions rasa_nlu/persistor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import boto3
import botocore
from builtins import object
from typing import Optional, Tuple, List, Text

from rasa_nlu.config import RasaNLUConfig
from typing import Optional, Tuple, List
from typing import Text

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,8 +134,8 @@ class AWSPersistor(Persistor):
Fetches them when needed, instead of storing them on the local disk."""

def __init__(self, aws_region, bucket_name, endpoint_url):
# type: (Text, Text, Text) -> None
def __init__(self, aws_region, bucket_name, endpoint_url=None):
# type: (Text, Text, Optional[Text]) -> None
super(AWSPersistor, self).__init__()
self.s3 = boto3.resource('s3',
region_name=aws_region,
Expand Down
16 changes: 8 additions & 8 deletions tests/base/test_config.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import pytest
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import json
import os
import io
import tempfile

import pytest
from typing import Text

import rasa_nlu
from tests.utilities import write_file_config
from tests.conftest import CONFIG_DEFAULTS_PATH
from rasa_nlu.config import RasaNLUConfig, InvalidConfigError
from rasa_nlu.registry import registered_pipeline_templates

from tests.conftest import CONFIG_DEFAULTS_PATH
from tests.utilities import write_file_config

with io.open(CONFIG_DEFAULTS_PATH, "r") as f:
defaults = json.load(f)
Expand Down
8 changes: 6 additions & 2 deletions tests/base/test_data_router.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import pytest
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import mock

from rasa_nlu import persistor
from rasa_nlu import data_router
from rasa_nlu import persistor


def test_list_projects_in_cloud_method():
Expand Down
6 changes: 3 additions & 3 deletions tests/base/test_emulators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals


def test_luis_request():
Expand Down
2 changes: 1 addition & 1 deletion tests/base/test_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from __future__ import unicode_literals

from rasa_nlu.extractors.spacy_entity_extractor import SpacyEntityExtractor
from tests import utilities
from rasa_nlu.training_data import TrainingData, Message
from tests import utilities


def test_crf_extractor(spacy_nlp):
Expand Down
9 changes: 5 additions & 4 deletions tests/base/test_featurizers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os

import numpy as np
import pytest

from rasa_nlu import training_data
from rasa_nlu.tokenizers.mitie_tokenizer import MitieTokenizer
from rasa_nlu.tokenizers.spacy_tokenizer import SpacyTokenizer
from rasa_nlu import training_data
from rasa_nlu.training_data import Message


Expand Down
9 changes: 5 additions & 4 deletions tests/base/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import pytest

from tests import utilities
from rasa_nlu import registry, training_data
from tests import utilities


@utilities.slowtest
Expand Down
9 changes: 3 additions & 6 deletions tests/base/test_multitenancy.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import json
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import tempfile

import pytest

from treq.testing import StubTreq

from rasa_nlu.config import RasaNLUConfig
Expand Down
57 changes: 26 additions & 31 deletions tests/base/test_persistor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import pytest
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import mock
import pytest
from moto import mock_s3

from tests import utilities
from rasa_nlu import persistor


Expand All @@ -13,41 +20,29 @@ def test_if_persistor_class_has_list_projects_method():
persistor.Persistor().list_projects()


def test_list_projects_method_in_AWSPersistor():
def mocked_init(self, *args, **kwargs):
self._project_and_model_from_filename = lambda x: {'project_key': ('project', 'model')}[x]
self.bucket = Object()
self.bucket.objects = Object()
self.bucket_name = 'bucket'

def mocked_filter():
filter_result = Object()
filter_result.key = 'project_key'
return filter_result,

self.bucket.objects.filter = mocked_filter
print(self.bucket_name)
@mock_s3
def test_list_projects_method_in_AWSPersistor(component_builder):
# artificially create a persisted model
_config = utilities.base_test_conf("keyword")
_config['storage'] = 'aws'
_config['project'] = 'mytestproject'
_config['aws_region'] = 'us-east-1'
_config['bucket_name'] = 'rasa-test'
(trained, persisted_path) = utilities.run_train(_config, component_builder)

with mock.patch.object(persistor.AWSPersistor, "__init__", mocked_init):
result = persistor.AWSPersistor("", "", "").list_projects()
# We need to create the bucket since this is all in Moto's 'virtual' AWS
# account
awspersistor = persistor.AWSPersistor(_config['aws_region'],
_config['bucket_name'])
result = awspersistor.list_projects()

assert result == ['project']
assert result == ['mytestproject']


@mock_s3
def test_list_projects_method_raise_exeception_in_AWSPersistor():
def mocked_init(self, *args, **kwargs):
self.bucket = Object()
self.bucket.objects = Object()
self.bucket_name = 'bucket'
self.aws_region = 'region'

def mocked_filter():
raise ValueError

self.bucket.objects.filter = mocked_filter

with mock.patch.object(persistor.AWSPersistor, "__init__", mocked_init):
result = persistor.AWSPersistor("", "", "").list_projects()
awspersistor = persistor.AWSPersistor("us-east-1", "rasa-test")
result = awspersistor.list_projects()

assert result == []

Expand Down
5 changes: 5 additions & 0 deletions tests/base/test_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import mock

from rasa_nlu.project import Project
Expand Down
16 changes: 7 additions & 9 deletions tests/base/test_server.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import io
import json
import tempfile

import pytest
import time

import pytest
from treq.testing import StubTreq

from rasa_nlu.config import RasaNLUConfig
import json
import io

from rasa_nlu.server import RasaNLU
from tests import utilities
from tests.utilities import ResponseTest
from rasa_nlu.server import RasaNLU, InvalidProjectError


@pytest.fixture(scope="module")
Expand Down
7 changes: 4 additions & 3 deletions tests/base/test_synonyms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from rasa_nlu.extractors.entity_synonyms import EntitySynonymMapper


Expand Down
8 changes: 3 additions & 5 deletions tests/base/test_tokenizers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-


from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals


def test_whitespace():
Expand Down
6 changes: 3 additions & 3 deletions tests/base/test_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import pytest
from jsonschema import ValidationError

from rasa_nlu import training_data
from rasa_nlu import utils
from rasa_nlu.convert import convert_training_data
from rasa_nlu.training_data.formats.rasa import validate_rasa_nlu_data
from rasa_nlu.extractors.mitie_entity_extractor import MitieEntityExtractor
from rasa_nlu.tokenizers.whitespace_tokenizer import WhitespaceTokenizer
from rasa_nlu import training_data
from rasa_nlu import utils
from rasa_nlu.training_data.formats.rasa import validate_rasa_nlu_data


def test_example_training_data_is_valid():
Expand Down
16 changes: 10 additions & 6 deletions tests/base/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import pytest

from rasa_nlu.utils import relative_normpath, recursively_find_files, create_dir, ordered
from rasa_nlu.utils import (
relative_normpath,
recursively_find_files, create_dir, ordered)


def test_relative_normpath():
assert relative_normpath("/my/test/path/file.txt", "/my/test") == "path/file.txt"
test_file = "/my/test/path/file.txt"
assert relative_normpath(test_file, "/my/test") == "path/file.txt"
assert relative_normpath(None, "/my/test") is None


Expand All @@ -26,7 +29,8 @@ def test_recursively_find_files_non_existing_dir():


def test_creation_of_existing_dir(tmpdir):
assert create_dir(tmpdir.strpath) is None # makes sure there is no exception
# makes sure there is no exception
assert create_dir(tmpdir.strpath) is None


def test_ordered():
Expand Down

0 comments on commit b3e69d8

Please sign in to comment.