Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #9 from freedomofkeima/add-tests
Browse files Browse the repository at this point in the history
Add pytest and CircleCI 2 integration
  • Loading branch information
freedomofkeima committed Oct 19, 2017
2 parents 61b89be + 4e23930 commit 684d938
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2
jobs:
build:
working_directory: ~/circleci-maidchan
docker:
- image: circleci/python:2.7
steps:
- checkout
- restore_cache:
key: deps1-{{ .Branch }}
- run:
command: |
virtualenv venv
. venv/bin/activate
pip install -r requirements.txt
pip install -r requirements-dev.txt
- save_cache:
key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
paths:
- "venv"
- run:
command: |
. venv/bin/activate
mkdir -p "${CIRCLE_WORKING_DIRECTORY}/results/junit/"
pytest --junitxml="results/junit/results-${CIRCLE_NODE_INDEX}.xml" --verbose --cov=maidchan --cov-report term-missing tests
- store_artifacts:
path: "results"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*.pyc
/build
/venv
.cache/
.coverage

# Packages
*.egg
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Maid-chan feat Facebook Messenger

[![CircleCI](https://circleci.com/gh/freedomofkeima/messenger-maid-chan.svg?style=shield)](https://circleci.com/gh/freedomofkeima/messenger-maid-chan)

Maid-chan name is inspired from [Sakurasou's Artificial Intelligence](http://sakurasounopetnakanojo.wikia.com/wiki/Maid).

If you have any other ideas, I am accepting contributions :) For developers, you could access [the documentation pages for developers here](https://messenger-maid-chan.readthedocs.io/). For others who are interested in using Maid-chan features, please ask me directly and head to [Maid-chan Facebook Page](https://www.facebook.com/maidchan2/).
Expand Down
7 changes: 7 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
coverage==4.4.1
funcsigs==1.0.2
mock==2.0.0
pbr==3.1.1
py==1.4.34
pytest==3.2.3
pytest-cov==2.5.1
Empty file added tests/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions tests/data/get_trans_prediction.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
English
Name English
Family Indo-European
Writing system Latin
Code en
ISO 639-3 eng
SIL http://www-01.sil.org/iso639-3/documentation.asp?id=eng
Glottolog http://glottolog.org/resource/languoid/id/stan1293
Wikipedia http://en.wikipedia.org/wiki/English_language
1 change: 1 addition & 0 deletions tests/data/get_trans_translation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
こんにちは世界!
1 change: 1 addition & 0 deletions tests/data/get_trans_translation_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Halo Dunia!
52 changes: 52 additions & 0 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import os

from mock import Mock, patch

from maidchan.translate import get_trans_language_prediction, get_translation

SCRIPT_PATH = os.path.abspath(os.path.dirname(__file__))


def _get_response(name):
path = os.path.join(SCRIPT_PATH, 'data', name)
with open(path) as f:
return f.read()


def mocked_trans(*args, **kwargs):
"""
Mocked "trans"
"""
process_mock = Mock()
return_value = None
if '-id' in args[0] and 'hello, world!' in args[0]:
return_value = _get_response('get_trans_prediction.txt')
elif '-b' in args[0] and 'en:ja' in args[0] and 'hello, world!' in args[0]:
return_value = _get_response('get_trans_translation.txt')
elif '-b' in args[0] and 'en:id' in args[0] and 'hello, world!' in args[0]:
return_value = _get_response('get_trans_translation_2.txt')
attrs = {'communicate.return_value': (return_value, None)}
process_mock.configure_mock(**attrs)
return process_mock


class TestTranslate:
@patch('subprocess.Popen', side_effect=mocked_trans)
def test_get_translate_language_prediction(self, mock_trans):
assert get_trans_language_prediction("hello, world!") == "en"

@patch('subprocess.Popen', side_effect=mocked_trans)
def test_get_translation_en_to_ja(self, mock_trans):
query = "translate hello, world! from english to japanese"
assert get_translation(query).decode('utf-8') == u"こんにちは世界!"

@patch('subprocess.Popen', side_effect=mocked_trans)
def test_get_translation_en_to_default(self, mock_trans):
query = "translate hello, world! from english"
assert get_translation(query).decode('utf-8') == u"こんにちは世界!"

@patch('subprocess.Popen', side_effect=mocked_trans)
def test_get_translation_default_to_id(self, mock_trans):
query = "translate hello, world! to bahasa"
assert get_translation(query).decode('utf-8') == "Halo Dunia!"

0 comments on commit 684d938

Please sign in to comment.