Skip to content

Commit 9c20ebf

Browse files
committed
Switch to pytest and remove relative pathing
1 parent b6aec5b commit 9c20ebf

6 files changed

Lines changed: 55 additions & 19 deletions

File tree

dataverse/connection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def __init__(self, host, token):
1414
self.host = host
1515
self.sd_uri = "https://{host}/dvn/api/data-deposit/v1.1/swordv2/service-document".format(host=self.host)
1616
self.service_document = None
17-
self.connected = False
1817

1918
self.connect()
2019

@@ -31,7 +30,6 @@ def connect(self):
3130
raise ConnectionError('Could not connect to the Dataverse')
3231

3332
self.service_document = etree.XML(resp.content)
34-
self.connected = True
3533

3634
def get_dataverses(self, refresh=False):
3735
if refresh:

dataverse/settings/defaults.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
TEST_HOST = "dataverse-demo.iq.harvard.edu"
1+
import os
2+
3+
TEST_HOST = "apitest.dataverse.org"
24
TEST_TOKEN = "changeme"
35

6+
HERE = os.path.dirname(os.path.abspath(__file__))
7+
BASE_PATH = os.path.abspath(os.path.join(HERE, os.pardir))
8+
49
EXAMPLE_DICT = {
510
"title": "ExampleTitle",
611
"id": "ExampleID",

dataverse/test/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import os
2+
from dataverse.settings import BASE_PATH
3+
14
PICS_OF_CATS_DATASET = {
25
"id": "1",
36
"title": "This Study is about Pictures of Cats",
47
"author": "Peter Bull",
58
"description": "In this study we prove there can be pictures of cats.",
69
}
710

8-
ATOM_DATASET = "../resources/atom-entry-study.xml"
11+
ATOM_DATASET = os.path.join(BASE_PATH, 'resources', 'atom-entry-study.xml')
912

13+
EXAMPLE_FILES = [
14+
os.path.join(BASE_PATH, 'test', '__init__.py'),
15+
os.path.join(BASE_PATH, 'test', 'config.py'),
16+
]

dataverse/test/test_dataverse.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import unittest
22

3-
import logging
4-
logging.basicConfig(level=logging.ERROR)
3+
import httpretty
54

6-
# local modules
75
from dataverse.connection import Connection
86
from dataverse.dataset import Dataset
9-
from dataverse.exceptions import DataverseError
107
from dataverse.settings import TEST_HOST, TEST_TOKEN
11-
from dataverse.test.config import PICS_OF_CATS_DATASET, ATOM_DATASET
8+
from dataverse.test.config import PICS_OF_CATS_DATASET, ATOM_DATASET, EXAMPLE_FILES
9+
from dataverse import exceptions
1210
from dataverse import utils
1311

12+
import logging
13+
logging.basicConfig(level=logging.ERROR)
14+
1415

1516
class TestUtils(unittest.TestCase):
1617

@@ -54,6 +55,31 @@ def test_format_term_replace(self):
5455
self.assertEqual(formatted_term, '{http://purl.org/dc/terms/}identifier')
5556

5657

58+
class TestConnection(unittest.TestCase):
59+
60+
def test_connect(self):
61+
c = Connection(TEST_HOST, TEST_TOKEN)
62+
63+
self.assertEqual(c.host, TEST_HOST)
64+
self.assertEqual(c.token, TEST_TOKEN)
65+
self.assertTrue(c.service_document)
66+
67+
def test_connect_unauthorized(self):
68+
with self.assertRaises(exceptions.UnauthorizedError):
69+
Connection(TEST_HOST, 'wrong-token')
70+
71+
@httpretty.activate
72+
def test_connect_unknown_failure(self):
73+
httpretty.register_uri(
74+
httpretty.GET,
75+
"https://{host}/dvn/api/data-deposit/v1.1/swordv2/service-document".format(host=TEST_HOST),
76+
status=400,
77+
)
78+
79+
with self.assertRaises(exceptions.ConnectionError):
80+
Connection(TEST_HOST, TEST_TOKEN)
81+
82+
5783
class TestDataset(unittest.TestCase):
5884

5985
def test_init(self):
@@ -98,7 +124,7 @@ def setUpClass(self):
98124
print "Getting Dataverse"
99125
dataverses = self.dvc.get_dataverses()
100126
if not dataverses:
101-
raise DataverseError(
127+
raise exceptions.DataverseError(
102128
'You must have a Dataverse to run these tests.'
103129
)
104130

@@ -136,10 +162,10 @@ def test_create_dataset_from_xml(self):
136162
self.dv.delete_dataset(retrieved_dataset)
137163

138164
def test_add_files(self):
139-
self.s.add_files(['test_dataverse.py', 'config.py'])
165+
self.s.add_files(EXAMPLE_FILES)
140166
actual_files = [f.name for f in self.s.get_files()]
141167

142-
self.assertIn('test_dataverse.py', actual_files)
168+
self.assertIn('__init__.py', actual_files)
143169
self.assertIn('config.py', actual_files)
144170

145171
def test_upload_file(self):

readme.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ files = dataset.get_files('latest')
3333

3434
### Configuration
3535

36-
Create a file at `settings/local.py`. The file should contain the following
36+
Create a file at `dataverse/settings/local.py`. The file should contain the following
3737
information:
3838

3939
```python
40-
DEFAULT_HOST = "apitest.dataverse.org"
41-
DEFAULT_TOKEN = "" # Token can be generated at {host}/account/apitoken
40+
TEST_HOST = "apitest.dataverse.org"
41+
TEST_TOKEN = "" # Token can be generated at {host}/account/apitoken
4242
```
4343

4444
Do not commit this file.
@@ -50,5 +50,4 @@ host you wish to test. Do not run tests on the production server.
5050

5151
To run tests:
5252

53-
$ cd dataverse/test
54-
$ python -m unittest test_dataverse
53+
$ py.test

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
REQUIRES = [
77
'bleach>=1.2.2',
88
'requests>=2.2.1',
9-
'lxml>=3.2.5'
9+
'lxml>=3.2.5',
1010
]
1111

1212
TEST_REQUIRES = [
13-
13+
'httpretty>=0.8.8',
14+
'pytest>=2.7.0',
1415
]
1516

1617

0 commit comments

Comments
 (0)