-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
250 lines (194 loc) · 9.27 KB
/
Copy pathtest.py
File metadata and controls
250 lines (194 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from importlib.metadata import metadata
import unittest, tempfile, os, shutil
from dvcurator import dataverse, github, pdf, rename, readme, fs
harvard_host = "https://dataverse.harvard.edu"
harvard_doi = "doi:10.7910/DVN/CZYY1N"
qdr_doi = "doi:10.5064/F6YYA3O3"
curation_repo = "QualitativeDataRepository/testing-demos"
class TestFs(unittest.TestCase):
def test_check_dropbox(self):
self.assertFalse(fs.check_dropbox("/notarealfolder"))
f = tempfile.TemporaryDirectory()
# folders are named for the project, with no "QDR Project - " prefix
os.makedirs(os.path.join(f.name, "20240402 - Foobar"))
self.assertTrue(fs.check_dropbox(f.name, "20240402 - Foobar"))
self.assertFalse(fs.check_dropbox(f.name, "20240402 - DoesntExist"))
self.assertTrue(fs.check_dropbox(f.name))
f.cleanup()
def test_older_folder(self):
f = tempfile.TemporaryDirectory()
older = os.path.join(f.name, "QDR Project - Rabello Sodre - Memories about Colegio")
os.makedirs(older)
# a folder from before the "YYYYMMDD - Lastname" convention still gets found
self.assertEqual(fs.check_dropbox(f.name, "20240402 - Rabello Sodre"), os.path.normpath(older))
self.assertFalse(fs.check_dropbox(f.name, "20240402 - Somebody"))
# ambiguous: don't guess between two folders for the same author
os.makedirs(os.path.join(f.name, "QDR Project - Rabello Sodre - Another project"))
self.assertFalse(fs.check_dropbox(f.name, "20240402 - Rabello Sodre"))
f.cleanup()
def test_new_step(self):
self.assertFalse(fs.copy_new_step("/notarealfolder", "test"))
f = tempfile.TemporaryDirectory()
first_folder = os.path.join(f.name, "QDR Prepared", "1_extract")
self.assertFalse(fs.copy_new_step(f.name, "test"))
os.makedirs(first_folder)
self.assertTrue(fs.copy_new_step(f.name, "test"))
f.cleanup()
class TestDataverseAPI(unittest.TestCase):
def test_citation(self):
# these should all fail
self.assertIsNone(dataverse.get_metadata("foobar", host=harvard_host))
self.assertIsNone(dataverse.get_metadata("doi:foobar", host=harvard_host))
self.assertIsNone(dataverse.get_metadata("doi:10.5064/F6FHTB9H"))
self.assertIsNone(dataverse.get_metadata("doi:doi:doi:10.5064/F6FHTB9E"))
# now let's test a success case
metadata = dataverse.get_metadata(harvard_doi, host=harvard_host)
citation = dataverse.get_citation(metadata)
self.assertIsNotNone(citation)
self.assertEqual(citation['title'], "Replication Data for: Data policies of highly-ranked social science journals")
def test_deposit_date(self):
metadata = dataverse.get_metadata(qdr_doi)
self.assertEqual(dataverse.get_deposit_date(metadata), "20210803")
# this one only records a year, so we fall back on the create time
metadata = dataverse.get_metadata("doi:10.5064/F68G8HMM")
self.assertEqual(dataverse.get_deposit_date(metadata), "20191021")
def test_biblio(self):
self.assertTrue(dataverse.get_biblio_citation(qdr_doi).startswith("VandeVusse"))
def test_download(self):
f = tempfile.TemporaryDirectory()
metadata = dataverse.get_metadata(qdr_doi)
path = dataverse.download_dataset(metadata, f.name)
self.assertTrue(os.path.isdir(path))
self.assertTrue(os.path.exists(os.path.join(path, os.pardir, os.pardir, "Original Deposit.zip")))
self.assertTrue(os.path.exists(os.path.join(path, os.pardir, os.pardir, "Original metadata.json")))
self.assertTrue(os.path.exists(os.path.join(path, "README_VandeVusse-Mueller.txt")))
# Try again, we should fail the second time
self.assertIsNone(dataverse.download_dataset(metadata, f.name))
f.cleanup()
# try a harder example, with messed up filenames
f = tempfile.TemporaryDirectory()
metadata = dataverse.get_metadata("doi:10.5064/F68G8HMM")
path = dataverse.download_dataset(metadata, f.name)
self.assertTrue(os.path.isdir(path))
self.assertTrue(os.path.exists(os.path.join(path, os.pardir, os.pardir, "Original Deposit.zip")))
self.assertTrue(os.path.exists(os.path.join(path, os.pardir, os.pardir, "Original metadata.json")))
f.cleanup()
class TestGithubAPI(unittest.TestCase):
def test_check(self):
self.assertTrue(github.check_repo(repo="IQSS/Dataverse"))
self.assertFalse(github.check_repo(repo="Not/arealrepo"))
def test_search(self):
self.assertTrue(github.search_existing("Karcher - Anonymous Peer Review", repo="QualitativeDataRepository/testing-demos"))
self.assertFalse(github.search_existing("Nobody - Project that doesnt exist", repo="QualitativeDataRepository/testing-demos"))
def test_version(self):
self.assertFalse(github.check_version())
class TestRename(unittest.TestCase):
def test_projectname(self):
# name is "YYYYMMDD - Lastname", from the Deposit Date
metadata = dataverse.get_metadata("doi:10.5064/F6AQGERV")
self.assertEqual(rename.project_name(metadata), "20220608 - Haney")
metadata = dataverse.get_metadata("doi:10.5064/F6ZXIJS5")
self.assertEqual(rename.project_name(metadata), "20240326 - Guastaferro")
# test special character removal
metadata = dataverse.get_metadata("doi:10.5064/F6MBCJ8M")
self.assertEqual(rename.project_name(metadata), "20210423 - Berntzen")
# make a temporary directory named the citation and test it exists
d = tempfile.TemporaryDirectory()
new_folder_name = rename.project_name(metadata)
new_folder_path = os.path.join(d.name, new_folder_name, "QDR Prepared", "1_extract")
os.makedirs(new_folder_path)
self.assertTrue(os.path.exists(new_folder_path)) # Ensure it was successfully made
d.cleanup()
# test accented character removal in a two-word last name
metadata = dataverse.get_metadata("doi:10.5064/F6FHTB9E")
self.assertEqual(rename.project_name(metadata), "20240402 - Rabello Sodre")
def test_rename(self):
f = tempfile.TemporaryDirectory()
first_folder = os.path.join(f.name, "QDR Prepared", "1_extract")
os.makedirs(first_folder)
fake_file = "foobar.txt"
with open(os.path.join(first_folder, fake_file), 'w') as fp:
pass
metadata = dataverse.get_metadata(harvard_doi, host=harvard_host)
citation = dataverse.get_citation(metadata)
self.assertIsNotNone(citation)
new_path = rename.basic_rename(f.name, citation)
new_file = os.listdir(new_path)[0]
self.assertEqual(rename.last_name_prefix(citation) + "_" + fake_file,
new_file)
f.cleanup()
def test_nameprefix(self):
metadata = dataverse.get_metadata("doi:10.5064/F6YYA3O3")
citation = dataverse.get_citation(metadata)
self.assertIsNotNone(citation)
self.assertEqual(rename.last_name_prefix(citation), "VandeVusse-Mueller")
one_author_doi = "doi:10.7910/DVN/RHDI2C"
metadata = dataverse.get_metadata(one_author_doi, host=harvard_host)
citation = dataverse.get_citation(metadata)
self.assertEqual(rename.last_name_prefix(citation), "Gadarian")
class TestPDFMetadata(unittest.TestCase):
def test_nopdfs(self):
self.assertFalse(pdf.standard_metadata("/notarealfolder", None))
f = tempfile.TemporaryDirectory()
edit_path = os.path.join(f.name, "QDR Prepared/5_Rename")
os.makedirs(edit_path)
self.assertFalse(pdf.standard_metadata(edit_path, None))
f.cleanup()
def test_pdfmetadata(self):
# This test is to make sure author string gets written
# We read it back out from one of the files
import pikepdf
# Get author string from online citation
metadata = dataverse.get_metadata(harvard_doi, host=harvard_host)
citation = dataverse.get_citation(metadata)
self.assertIsNotNone(citation)
author_string = pdf.combine_author_names(citation)
d = tempfile.TemporaryDirectory()
temp_structure = os.path.normpath(os.path.join(d.name, "QDR Prepared/5_rename"))
os.makedirs(temp_structure)
empty_pdf = pikepdf.Pdf.new()
for i in range(1, 11):
empty_pdf.save(os.path.join(temp_structure, f'test{i}.pdf'))
edit_path = pdf.standard_metadata(d.name, citation)
one_file = os.path.join(edit_path, os.listdir(edit_path)[4])
example = pikepdf.open(one_file)
meta = example.open_metadata()
self.assertEqual(meta['pdf:Author'], author_string)
example.close()
d.cleanup()
def test_anonmetadata(self):
import pikepdf
d = tempfile.TemporaryDirectory()
temp_structure = os.path.normpath(os.path.join(d.name, "QDR Prepared", "5_something"))
os.makedirs(temp_structure)
empty_pdf = pikepdf.Pdf.new()
for i in range(1, 11):
empty_pdf.save(os.path.join(temp_structure, f'test{i}.pdf'))
metadata = dataverse.get_metadata(harvard_doi, host=harvard_host)
citation = dataverse.get_citation(metadata)
self.assertIsNotNone(citation)
new_path = rename.basic_rename(d.name, citation)
new_path = fs.anonymize_project(d.name, citation)
one_file = os.listdir(new_path)[4]
print(one_file)
self.assertTrue(one_file.startswith("ANONYMIZED"))
example = pikepdf.open(os.path.join(new_path, one_file))
meta = example.open_metadata()
self.assertEqual(meta['pdf:Author'], "ANONYMIZED")
example.close()
d.cleanup()
class TestREADME(unittest.TestCase):
def test_generateREADME(self):
metadata = dataverse.get_metadata(harvard_doi, host=harvard_host)
d=tempfile.TemporaryDirectory()
subfolder = os.path.join(d.name, "5_ready")
os.makedirs(subfolder)
with open(os.path.join(subfolder, "a_file.txt"), 'w') as f:
pass
generated = readme.generate_readme(metadata, d.name, repo=curation_repo)
self.assertTrue(os.path.exists(generated))
# Fail on the second time
self.assertIsNone(readme.generate_readme(metadata, d.name))
d.cleanup()
if __name__ == '__main__':
unittest.main()