Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
20 changes: 20 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#### Background context for this PR

#### What does this PR do?
- [x] Something that's complete
- [ ] Something that's still in progress

#### Impacted areas in application
- A page affected
- A library affected

#### How should PR reviewers test this?
- Unit tests:
- Manual tests:
- What features need to be on or off?
- Detailed description of how to reproduce
- ...what page
- ...test data
- ...etc

#### Would you like specific feedback on anything?
36 changes: 19 additions & 17 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
from setuptools import setup

with open('wordsmith/__init__.py', 'r') as fd:
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1)
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
fd.read(),
re.MULTILINE).group(1)

if not version:
raise RuntimeError('Could not locate version information')
raise RuntimeError('Could not locate version information')

setup(
name = 'wordsmith',
version = version,
description = 'A wrapper around the Wordsmith API written in python',
author = 'John Hegele - Automated Insights',
packages = ['wordsmith'],
package_dir = {'wordsmith' : 'wordsmith'},
install_requires = ['requests', 'six'],
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Natural Language :: English',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5'
]
name='wordsmith',
version=version,
description='A wrapper around the Wordsmith API written in python',
author='John Hegele - Automated Insights',
packages=['wordsmith'],
package_dir={'wordsmith': 'wordsmith'},
install_requires=['requests', 'six'],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Natural Language :: English',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6'
]
)
9 changes: 9 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from wordsmith import Wordsmith

API_KEY = '923b278a6088675262af64ceb437bab31d7ebc6b07aaf89f88b0b88dd4fe2a97'


class TestWordsmith(object):

def initialize(self, **kwargs):
return Wordsmith(API_KEY, user_agent='python sdk test suite', **kwargs)
54 changes: 54 additions & 0 deletions tests/test_batch_generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest
from wordsmith import NarrativeGenerateError
from tests.fixtures import TestWordsmith


class TestProject(TestWordsmith):

def setup(self):
self.ws = super().initialize()

def test_batch_generate_narrative(self):
data = [{'a': i, 'b': i, 'c': i} for i in range(10)]
expected_outputs = ['The value of A is {}.'.format(i)
for i in range(10)]
narrs = self.ws.project('test').template('test').batch_narrative(data)
narrs.generate()
for expected, actual in zip(expected_outputs, narrs.narratives):
assert expected == actual.text

def test_bad_batch_generate_no_break(self):
data = [
{'a': 1, 'b': 1, 'c': 1},
{'d': 1, 'e': 1},
{'a': 1, 'b': 1, 'c': 1}
]
narrs = self.ws.project('test').template('test').batch_narrative(data)
narrs.break_on_error = False
narrs.generate()
expected_narratives = ['The value of A is 1.',
None,
'The value of A is 1.']
actual_narratives = []
for n in narrs.narratives:
actual_narratives.append(n.text if n is not None else None)
assert (expected_narratives == actual_narratives)\
and (len(narrs.errors) == 1)

def test_bad_batch_generate_break(self):
with pytest.raises(NarrativeGenerateError):
data = [
{'a': 1, 'b': 1, 'c': 1},
{'d': 1, 'e': 1},
{'a': 1, 'b': 1, 'c': 1}
]
narrs = self.ws.project('test')\
.template('test').batch_narrative(data)
narrs.break_on_error = True
narrs.generate()

def test_wordsmith_400_error(self):
with pytest.raises(NarrativeGenerateError):
data = {'not_a_valid_column': 0}
self.ws.project('test')\
.template('test').generate_narrative(data)
22 changes: 22 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from wordsmith import Wordsmith
from tests.fixtures import TestWordsmith


class TestConfig(TestWordsmith):

def setup(self):
self.ws = super().initialize()

def test_set_token_value(self):
token = 'my_token'
ws = Wordsmith(token)
assert ws.config.api_key == token

def test_default_url(self):
assert self.ws.config.base_url \
== 'https://api.automatedinsights.com/v1'

def test_set_new_version(self):
new_version = 'a_new_version'
ws = Wordsmith('my_token', version=new_version)
assert ws.config.version == new_version
43 changes: 43 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
from wordsmith import Wordsmith, ProjectSlugError
from tests.fixtures import TestWordsmith


class TestProject(TestWordsmith):

def setup(self):
self.ws = super().initialize()

def test_list_all_projects(self):
projects = [project.name for project in self.ws.projects]
assert projects == ['Test']

def test_list_all_projects_with_custom_base_url(self):
ws = Wordsmith(self.ws.config.api_key,
user_agent='python sdk test suite',
base_url='https://api.automatedinsights.com/v1')
projects = [project.name for project in ws.projects]
assert projects == ['Test']

def test_find_project_by_slug(self):
project = self.ws.project('test')
assert project.name == 'Test'

def test_bad_project_raises_error(self):
with pytest.raises(ProjectSlugError):
self.ws.project('fake project')

'''
TODO - these tests from ruby version
def test_schema
project = Wordsmith::Project.find 'test'
expected = {a: 'Number', b: 'Number', c: 'Number'}
assert_equal expected, project.schema
end

def test_template_collection_exists
project = Wordsmith::Project.find('test')
assert_instance_of Wordsmith::TemplateCollection, project.templates
end
end
'''
31 changes: 31 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from wordsmith import TemplateSlugError
from tests.fixtures import TestWordsmith


class TestProject(TestWordsmith):

def setup(self):
self.ws = super().initialize()

def test_bad_template_raises_error(self):
with pytest.raises(TemplateSlugError):
self.ws.project('test').template('fake template')

def test_find_project_by_name(self):
matches = self.ws.find_project('Test')
assert matches[0].name == 'Test'

def test_find_template_that_exists(self):
matches = self.ws.project('test').find_template('Test')
assert len(matches) > 0

def test_find_template_that_doesnt_exist(self):
matches = self.ws.project('test').find_template('Fake Template')
assert len(matches) == 0

def test_generate_narrative(self):
data = {'a': 1, 'b': 1, 'c': 1}
narr = self.ws.project('test')\
.template('test').generate_narrative(data).text
assert narr == 'The value of A is 1.'
103 changes: 0 additions & 103 deletions tests/test_wordsmith.py

This file was deleted.

15 changes: 9 additions & 6 deletions wordsmith/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
__title__ = 'wordsmith'
__version__ = '0.5'
__author__ = 'John Hegele'
__copyright__ = 'Copyright 2016 Automated Insights'

# flake8: noqa: F401
from .wordsmith import Wordsmith
from .configuration import Configuration
from .project import Project
from .template import Template
from .narrative import Narrative, Batch
from .exceptions import (ProjectSlugError, TemplateSlugError, NarrativeGenerateError)
from .exceptions import (ProjectSlugError,
TemplateSlugError,
NarrativeGenerateError)

__title__ = 'wordsmith'
__version__ = '0.5.1'
__author__ = 'John Hegele'
__copyright__ = 'Copyright 2016 Automated Insights'
Loading