44__author__ = "peterbull"
55__date__ = "$Aug 21, 2013 2:56:25 PM$"
66
7- from operator import eq
87import os
98import sys
109from time import sleep
1110import unittest
11+ import zipfile
1212
1313import logging
1414logging .basicConfig (level = logging .ERROR )
1515
1616#local modules
17+ import config
18+ import tests as testdata
1719from study import Study
1820from connection import DvnConnection
19-
21+
22+
2023class 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+
127127if __name__ == "__main__" :
128128 __file__ = sys .argv [0 ]
129129 suite = unittest .TestLoader ().loadTestsFromTestCase (TestStudyOperations )
130130 unittest .TextTestRunner (verbosity = 2 ).run (suite )
131-
0 commit comments