Skip to content

Commit ba15a90

Browse files
committed
removes hardcoded filenames and test data, imports config and test data in the test rather than inserting them in to globals
1 parent daa2864 commit ba15a90

2 files changed

Lines changed: 45 additions & 41 deletions

File tree

dvn_client/src/dvn_client.py

100644100755
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# DVN client for SWORD API
24
# Prereqs: Python, sword2 Module (available using easy_install)
35
# Adapted from: https://bitbucket.org/beno/python-sword2/wiki/Quickstart
@@ -31,16 +33,19 @@ def parse_arguments():
3133
# parser.add_argument('-u','--username', default=None, help='Description for foo argument')
3234
# parser.add_argument('-p','--password', default=None, help='Description for bar argument')
3335

34-
parser.add_argument('--runTests', action="store", help='Path to a file with test definitions.')
35-
parser.add_argument('--config', action="store", help="Path to a file that contains configuration information.")
36+
parser.add_argument('--runTests', action="store", default='tests.py',
37+
help='Path to a file with test definitions. (default: tests.py)')
38+
parser.add_argument('--config', action="store", default='config.py',
39+
help="Path to a file that contains configuration information. (default: config.py)")
3640
return parser.parse_args()
3741

3842
def main():
3943
# Get the command line arguments.
4044
args = parse_arguments()
41-
42-
if args.runTests and args.config:
45+
46+
if args.config:
4347
execfile(args.config, globals())
48+
if args.runTests:
4449
execfile(args.runTests, globals())
4550

4651
dv = None #declare outside so except clause has access
@@ -63,7 +68,7 @@ def main():
6368
print "RELEASED: ", dv.is_released()
6469

6570
#s = Study.CreateStudyFromDict(PICS_OF_CATS_STUDY)
66-
s = Study.CreateStudyFromAtomEntryXmlFile("/home/vagrant/dvn_client/resources/atom-entry-study.xml")
71+
s = Study.CreateStudyFromAtomEntryXmlFile(ATOM_STUDY)
6772
dv.add_study(s)
6873
#s.add_files([INGEST_FILES])
6974
print s.get_citation()

dvn_client/src/dvn_test.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,44 @@
44
__author__="peterbull"
55
__date__ ="$Aug 21, 2013 2:56:25 PM$"
66

7-
from operator import eq
87
import os
98
import sys
109
from time import sleep
1110
import unittest
11+
import zipfile
1212

1313
import logging
1414
logging.basicConfig(level=logging.ERROR)
1515

1616
#local modules
17+
import config
18+
import tests as testdata
1719
from study import Study
1820
from connection import DvnConnection
19-
21+
22+
2023
class TestStudyOperations(unittest.TestCase):
24+
2125
@classmethod
2226
def setUpClass(self):
23-
# LOAD TEST DATA
24-
25-
print "Loading test data."
26-
testModulePath = os.path.dirname(__file__)
27-
execfile(os.path.join(testModulePath, "config.py"), globals()) #CREDS - This file is not committed.
28-
execfile(os.path.join(testModulePath, "tests.py"), globals()) #TEST DATA
29-
27+
28+
print "Verifying test data."
29+
assert zipfile.is_zipfile(testdata.INGEST_FILES), 'Invalid tests configuration. %s is not a zipfile' % testdata.INGEST_FILES
30+
assert os.path.isfile(testdata.PIC_OF_CAT), 'Invalid tests configuration. %s is not a file' % testdata.PIC_OF_CAT
31+
assert os.path.isfile(testdata.ATOM_STUDY), 'Invalid tests configuration. %s is not a file' % testdata.ATOM_STUDY
32+
3033
print "Connecting to DVN."
31-
self.dvc = DvnConnection(username=DEFAULT_USERNAME,
32-
password=DEFAULT_PASSWORD,
33-
host=DEFAULT_HOST,
34-
cert=DEFAULT_CERT)
35-
34+
self.dvc = DvnConnection(username=config.DEFAULT_USERNAME,
35+
password=config.DEFAULT_PASSWORD,
36+
host=config.DEFAULT_HOST,
37+
cert=config.DEFAULT_CERT)
38+
3639
print "Getting Dataverse"
3740
self.dv = self.dvc.get_dataverses()[0]
38-
39-
print "Removing any existing studies."
40-
self.dv.delete_all_studies(ignoreExceptions=True)
41-
41+
4242
def setUp(self):
43-
#runs before each test method
44-
4543
#create a study for each test
46-
s = Study.CreateStudyFromDict(PICS_OF_CATS_STUDY)
44+
s = Study.CreateStudyFromDict(testdata.PICS_OF_CATS_STUDY)
4745
self.dv.add_study(s)
4846
id = s.get_id()
4947
self.s = self.dv.get_study_by_hdl(id)
@@ -57,20 +55,20 @@ def tearDown(self):
5755
return
5856

5957
def test_create_study_from_xml(self):
60-
xmlStudy = Study.CreateStudyFromAtomEntryXmlFile(ATOM_STUDY)
58+
xmlStudy = Study.CreateStudyFromAtomEntryXmlFile(testdata.ATOM_STUDY)
6159
self.dv.add_study(xmlStudy)
6260
atomStudy = self.dv.get_study_by_string_in_entry("The first study for the New England Journal of Coffee dataverse")
6361
self.assertTrue(atomStudy)
6462
self.dv.delete_study(atomStudy)
6563

6664
def test_add_files_to_study(self):
67-
expected_files = ["char_r.tab",
68-
"float_new_r.tab",
69-
"int_r.tab",
70-
"min_date_r.tab"]
71-
self.s.add_files([INGEST_FILES])
65+
zipped_file = zipfile.ZipFile(testdata.INGEST_FILES, 'r')
66+
expected_files = [os.path.basename(f) for f in zipped_file.namelist()]
67+
self.s.add_files([testdata.INGEST_FILES])
7268
sleep(3) #wait for ingest
7369
actual_files = [f.name for f in self.s.get_files()]
70+
print 'actual', actual_files
71+
print 'expected', expected_files
7472

7573
expected_files.sort()
7674
actual_files.sort()
@@ -90,21 +88,22 @@ def test_display_study_statement(self):
9088
self.assertTrue(self.s.get_statement())
9189

9290
def test_delete_a_file(self):
93-
self.s.add_file(PIC_OF_CAT)
91+
self.s.add_file(testdata.PIC_OF_CAT)
92+
test_file = os.path.basename(testdata.PIC_OF_CAT)
9493

9594
#add file and confirm
9695
files = self.s.get_files()
97-
catFile = [f for f in files if f.name == "cat.jpg"]
96+
catFile = [f for f in files if f.name == test_file]
9897
self.assertTrue(len(catFile) == 1)
9998

10099
#delete file and confirm
101100
self.s.delete_file(catFile[0])
102101
files = self.s.get_files()
103-
catFile = [f for f in files if f.name == "cat.jpg"]
102+
catFile = [f for f in files if f.name == test_file]
104103
self.assertTrue(len(catFile) == 0)
105-
104+
106105
def test_delete_a_study(self):
107-
xmlStudy = Study.CreateStudyFromAtomEntryXmlFile(ATOM_STUDY)
106+
xmlStudy = Study.CreateStudyFromAtomEntryXmlFile(testdata.ATOM_STUDY)
108107
self.dv.add_study(xmlStudy)
109108
atomStudy = self.dv.get_study_by_string_in_entry("The first study for the New England Journal of Coffee dataverse")
110109
self.assertTrue(atomStudy)
@@ -113,19 +112,19 @@ def test_delete_a_study(self):
113112
self.assertTrue(startingNumberOfStudies > 0)
114113
self.dv.delete_study(atomStudy)
115114
self.assertEqual(len(self.dv.get_studies()), startingNumberOfStudies - 1)
116-
115+
117116
def test_release_study(self):
118117
self.assertTrue(self.s.get_state() == "DRAFT")
119118
self.s.release()
120119
self.assertTrue(self.s.get_state() == "RELEASED")
121120
self.dv.delete_study(self.s) #this should deaccession
122121
self.assertTrue(self.s.get_state() == "DEACCESSIONED")
123-
122+
124123
def test_dataverse_released(self):
125124
self.assertTrue(self.dv.is_released())
126-
125+
126+
127127
if __name__ == "__main__":
128128
__file__ = sys.argv[0]
129129
suite = unittest.TestLoader().loadTestsFromTestCase(TestStudyOperations)
130130
unittest.TextTestRunner(verbosity=2).run(suite)
131-

0 commit comments

Comments
 (0)