Skip to content

Commit

Permalink
Migrate tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Apr 13, 2022
1 parent 7aa0060 commit 2f2ef9e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ If you need to change the host or port, copy `deployment/datapusher_settings.py`

To run the tests:

nosetests
pytest

## Production deployment

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
argparse
ckanserviceprovider==0.0.10
ckanserviceprovider==1.0.0
html5lib==1.0.1
messytables==0.15.2
certifi
requests[security]==2.24.0
requests[security]==2.27.1
7 changes: 4 additions & 3 deletions tests/test_mocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import os
import json
import unittest

import pytest
import httpretty

import datapusher.main as main
Expand All @@ -27,7 +27,7 @@ def get_static_file(filename):
return open(join_static_path(filename)).read()


class TestImport(unittest.TestCase):
class TestImport():
@classmethod
def setup_class(cls):
cls.host = 'www.ckan.org'
Expand Down Expand Up @@ -110,4 +110,5 @@ def test_wrong_api_key(self):
}
}

self.assertRaises(util.JobError, jobs.push_to_datastore, 'fake_id', data)
with pytest.raises(util.JobError):
jobs.push_to_datastore('fake_id', data)
112 changes: 57 additions & 55 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
'''

import json
import unittest
import requests

from nose.tools import assert_equal, raises

import pytest
import httpretty

import datapusher.jobs as jobs
Expand All @@ -18,8 +15,8 @@
class TestChunky():
def test_simple(self):
chunks = jobs.chunky('abcdefg', 3)
assert_equal(
list(chunks),
assert (
list(chunks) ==
[
(['a', 'b', 'c'], False),
(['d', 'e', 'f'], False),
Expand All @@ -28,42 +25,42 @@ def test_simple(self):

def test_length_is_the_exact_multiple(self):
chunks = jobs.chunky('abcdef', 3)
assert_equal(
list(chunks),
assert (
list(chunks) ==
[
(['a', 'b', 'c'], False),
(['d', 'e', 'f'], True),
])

def test_empty(self):
chunks = jobs.chunky('', 3)
assert_equal(
list(chunks), [])
assert (
list(chunks) == [])


class TestGetUrl():
def test_get_action_url(self):
assert_equal(
jobs.get_url('datastore_create', 'http://www.ckan.org'),
assert (
jobs.get_url('datastore_create', 'http://www.ckan.org') ==
'http://www.ckan.org/api/3/action/datastore_create')

def test_get_action_url_with_stuff(self):
assert_equal(
jobs.get_url('datastore_create', 'http://www.ckan.org/'),
assert (
jobs.get_url('datastore_create', 'http://www.ckan.org/') ==
'http://www.ckan.org/api/3/action/datastore_create')

def test_get_action_url_with_https(self):
assert_equal(
jobs.get_url('datastore_create', 'https://www.ckan.org/'),
assert (
jobs.get_url('datastore_create', 'https://www.ckan.org/') ==
'https://www.ckan.org/api/3/action/datastore_create')

def test_get_action_url_missing_http(self):
assert_equal(
jobs.get_url('datastore_create', 'www.ckan.org/'),
assert (
jobs.get_url('datastore_create', 'www.ckan.org/') ==
'http://www.ckan.org/api/3/action/datastore_create')


class TestValidation(unittest.TestCase):
class TestValidation():
def test_validate_input(self):
jobs.validate_input({
'metadata': {
Expand All @@ -73,42 +70,46 @@ def test_validate_input(self):
'api_key': 'köi'
})

@raises(util.JobError)
def test_validate_input_raises_if_metadata_missing(self):
jobs.validate_input({
'foo': {},
'api_key': 'my-key'
})

@raises(util.JobError)
with pytest.raises(util.JobError):
jobs.validate_input({
'foo': {},
'api_key': 'my-key'
})

def test_validate_input_raises_if_res_id_missing(self):
jobs.validate_input({
'metadata': {
'ckan_url': 'http://www.ckan.org'
},
'api_key': 'my-key'
})

@raises(util.JobError)
with pytest.raises(util.JobError):
jobs.validate_input({
'metadata': {
'ckan_url': 'http://www.ckan.org'
},
'api_key': 'my-key'
})

def test_validate_input_raises_if_ckan_url_missing(self):
jobs.validate_input({
'metadata': {
'resource_id': 'h32jk4h34k5'
},
'api_key': 'my-key'
})

@raises(util.JobError)
with pytest.raises(util.JobError):
jobs.validate_input({
'metadata': {
'resource_id': 'h32jk4h34k5'
},
'api_key': 'my-key'
})

def test_validate_api_key(self):
jobs.validate_input({
'metadata': {
'resource_id': 'h32jk4h34k5',
'ckan_url': 'http://www.ckan.org'
}
})

with pytest.raises(util.JobError):
jobs.validate_input({
'metadata': {
'resource_id': 'h32jk4h34k5',
'ckan_url': 'http://www.ckan.org'
}
})

class TestCkanActionCalls(unittest.TestCase):

class TestCkanActionCalls():
@httpretty.activate
def test_get_resource(self):
url = 'http://www.ckan.org/api/3/action/resource_show'
Expand All @@ -120,7 +121,7 @@ def test_get_resource(self):
}}),
content_type="application/json")
resource = jobs.get_resource('an_id', 'http://www.ckan.org/', None)
assert_equal(resource, {'foo': 42})
assert resource == {'foo': 42}
assert json.loads(httpretty.last_request().body)['id'] == 'an_id'

@httpretty.activate
Expand Down Expand Up @@ -164,7 +165,7 @@ def test_send_resource_to_datastore(self):
jobs.send_resource_to_datastore({'id': 'an_id'}, [], [], False, 'my_key', 'http://www.ckan.org/')


class TestCheckResponse(unittest.TestCase):
class TestCheckResponse():
"""Unit tests for the check_response() function."""

@httpretty.activate
Expand Down Expand Up @@ -210,14 +211,15 @@ def test_text_500_with_false_success(self):
assert err.request_url == url

@httpretty.activate
@raises(util.JobError)
def test_text_404(self):
httpretty.register_uri(httpretty.GET, 'http://www.ckan.org/',
body='{"success": true}',
content_type='html/text',
status=404)
r = requests.get('http://www.ckan.org/')
jobs.check_response(r, 'http://www.ckan.org/', 'Me')

with pytest.raises(util.JobError):
httpretty.register_uri(httpretty.GET, 'http://www.ckan.org/',
body='{"success": true}',
content_type='html/text',
status=404)
r = requests.get('http://www.ckan.org/')
jobs.check_response(r, 'http://www.ckan.org/', 'Me')

@httpretty.activate
def test_text_404_ignore(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import os
import json
from nose.tools import assert_equal

import datapusher.main as main

Expand All @@ -21,5 +20,5 @@ class TestWeb():
def test_status(self):
rv = app.get('/status')
result_dict = json.loads(rv.data)
assert_equal(result_dict['job_types'], ['push_to_datastore'])
assert_equal(result_dict['name'], 'datapusher')
assert result_dict['job_types'] == ['push_to_datastore']
assert result_dict['name'] == 'datapusher'

0 comments on commit 2f2ef9e

Please sign in to comment.