Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Merge branch 'feature/github-processor-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed Apr 27, 2017
2 parents 0fc31a3 + be99279 commit 259abf1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
65 changes: 64 additions & 1 deletion tests/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import re
import json
import datetime
import unittest

import requests_mock

from datapackage_pipelines.utilities.lib_test_helpers import (
ProcessorFixtureTestsBase,
rejsonize
rejsonize,
mock_processor_test
)

import datapackage_pipelines_measure.processors

import logging
log = logging.getLogger(__name__)

Expand All @@ -18,6 +24,63 @@
ENV['PYTHONPATH'] = ROOT_PATH


class TestMeasureProcessors(unittest.TestCase):

@requests_mock.mock()
def test_my_processor(self, mock_request):
# mock the github repsonse
mock_github_response = {
'name': 'my-repository',
'subscribers_count': 4,
'stargazers_count': 1
}
mock_request.get(requests_mock.ANY, json=mock_github_response)

# input arguments used by our mock `ingest`
datapackage = {
'name': 'my-datapackage',
'project': 'my-project',
'resources': []
}
params = {
'name': 'hello',
'repo': 'my_github_repo',
'map_fields': {
'repository': 'name',
'watchers': 'subscribers_count',
'stars': 'stargazers_count'
}
}

# Path to the processor we want to test
processor_dir = \
os.path.dirname(datapackage_pipelines_measure.processors.__file__)
processor_path = os.path.join(processor_dir, 'add_github_resource.py')

# Trigger the processor with our mock `ingest` and capture what it will
# returned to `spew`.
spew_args, _ = \
mock_processor_test(processor_path,
(params, datapackage, []))

spew_dp = spew_args[0]
spew_res_iter = spew_args[1]

# Asserts for the datapackage
dp_resources = spew_dp['resources']
assert len(dp_resources) == 1
assert dp_resources[0]['name'] == 'hello'
field_names = \
[field['name'] for field in dp_resources[0]['schema']['fields']]
assert field_names == ['repository', 'watchers', 'stars']

# Asserts for the res_iter
spew_res_iter_contents = list(spew_res_iter)
assert len(spew_res_iter_contents) == 1
assert list(spew_res_iter_contents[0]) == \
[{'repository': 'my-repository', 'watchers': 4, 'stars': 1}]


class MeasureProcessorsFixturesTest(ProcessorFixtureTestsBase):

def _get_procesor_env(self):
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ envlist=
[testenv]
deps=
mock
requests-mock
pytest
pytest-cov
coverage
Expand All @@ -18,6 +19,8 @@ passenv=
TRAVIS_BRANCH
setenv=
MEASURE_TIMESTAMP_DEFAULT_FORMAT=%Y-%m-%dT%H:%M:%SZ
MEASURE_GITHUB_API_BASE_URL=https://api.github.com/repos/
MEASURE_GITHUB_API_TOKEN=fake_token
commands=
py.test \
--cov {[tox]package} \
Expand Down

0 comments on commit 259abf1

Please sign in to comment.