Skip to content

Commit

Permalink
Merge pull request #38 from rupa/master
Browse files Browse the repository at this point in the history
fix up data.source.update
  • Loading branch information
rupa authored Sep 4, 2019
2 parents 26b9684 + 52a3a23 commit ebae735
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.12.0 (Unreleased)

* Add (required) `sourcetype` arg to `source.update`. API requires it, although it cannot be changed.
* Add unit tests for data (source)

## 0.11.0 (August 05, 2019)

ENHANCEMENTS:
Expand Down
10 changes: 9 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ API Key at the bottom.
* `Documentation at ReadTheDocs <https://ns1-python.readthedocs.org/en/latest/>`_
* `NS1 REST API Documentation <https://ns1.com/api/>`_

Tests
=====

Unit tests use `pytest` (`pip install pytest`). 2.7 also requires `mock` to be
installed (`pip install mock`).

Tests should, of course, run and pass under 2.7 and 3.3. We use tox to automate
test runs and virtualenv setup, see `tox.ini` for config.

Contributions
=============

We welcome contributions! Please fork on GitHub and submit a Pull Request.

14 changes: 12 additions & 2 deletions ns1/rest/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def retrieve(self, sourceid, callback=None, errback=None):
errback=errback)

def create(self, name, sourcetype, callback=None, errback=None, **kwargs):
"""
The only supported kwarg is `config`.
"""
body = {
'name': name,
'sourcetype': sourcetype
Expand All @@ -34,9 +37,16 @@ def create(self, name, sourcetype, callback=None, errback=None, **kwargs):
callback=callback,
errback=errback)

def update(self, sourceid, callback=None, errback=None, **kwargs):
def update(self, sourceid, sourcetype, callback=None, errback=None,
**kwargs):
"""
Note that `sourcetype` is required, but cannot be changed by this
method.
Supported kwargs are: `name`, `config`.
"""
body = {
'id': sourceid
'sourcetype': sourcetype
}
self._buildStdBody(body, kwargs)
return self._make_request('POST',
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest

import ns1.rest.data

try: # Python 3.3 +
import unittest.mock as mock
except ImportError:
import mock


@pytest.fixture
def source_config(config):
config.loadFromDict({
'endpoint': 'api.nsone.net',
'default_key': 'test1',
'keys': {
'test1': {
'key': 'key-1',
'desc': 'test key number 1',
'writeLock': True
}
}
})
return config


def test_rest_data_source_list(source_config):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.list()
z._make_request.assert_called_once_with('GET',
'data/sources',
callback=None,
errback=None)


@pytest.mark.parametrize('sourceid, url', [('1', 'data/sources/1')])
def test_rest_data_source_retrieve(source_config, sourceid, url):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.retrieve(sourceid)
z._make_request.assert_called_once_with('GET',
url,
callback=None,
errback=None)


@pytest.mark.parametrize('source_name, sourcetype, url',
[('test_source', 'test_sourcetype', 'data/sources')])
def test_rest_data_source_create(source_config, source_name, sourcetype, url):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.create(source_name, sourcetype, config={})
z._make_request.assert_called_once_with('PUT',
url,
callback=None,
errback=None,
body={
'config': {},
'name': source_name,
'sourcetype': sourcetype
})


@pytest.mark.parametrize('sourceid, sourcetype, url',
[('1', 'ns1_v1', 'data/sources/1')])
def test_rest_data_source_update(source_config, sourceid, sourcetype, url):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.update(sourceid, sourcetype, name='test_source_name', config={})
z._make_request.assert_called_once_with('POST',
url,
callback=None,
errback=None,
body={
'config': {},
'name': 'test_source_name',
'sourcetype': sourcetype
})


@pytest.mark.parametrize('sourceid, url', [('1', 'data/sources/1')])
def test_rest_data_source_delete(source_config, sourceid, url):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.delete(sourceid)
z._make_request.assert_called_once_with('DELETE',
url,
callback=None,
errback=None)


@pytest.mark.parametrize('sourceid, data, url',
[('1', {'foo': 'foo', 'bar': 1}, 'feed/1')])
def test_rest_data_source_publish(source_config, sourceid, data, url):
z = ns1.rest.data.Source(source_config)
z._make_request = mock.MagicMock()
z.publish(sourceid, data=data)
z._make_request.assert_called_once_with('POST',
url,
callback=None,
errback=None,
body=data)

0 comments on commit ebae735

Please sign in to comment.