python-dwca-reader 0.16.2__py3-none-any.whl → 0.16.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,12 +13,12 @@ class TestDataFileDescriptor(unittest.TestCase):
13
13
  """Unit tests for DataFileDescriptor class."""
14
14
 
15
15
  def test_init_from_file(self):
16
- """ Ensure a DataFileDescriptor can be constructed directly from a CSV file.
16
+ """Ensure a DataFileDescriptor can be constructed directly from a CSV file.
17
17
 
18
18
  This is necessary for archives sans metafile.
19
19
  """
20
- with zipfile.ZipFile(sample_data_path('dwca-simple-csv.zip'), 'r') as archive:
21
- datafile_path = archive.extract('0008333-160118175350007.csv')
20
+ with zipfile.ZipFile(sample_data_path("dwca-simple-csv.zip"), "r") as archive:
21
+ datafile_path = archive.extract("0008333-160118175350007.csv")
22
22
 
23
23
  d = DataFileDescriptor.make_from_file(datafile_path)
24
24
  # Check basic metadata with the file
@@ -26,8 +26,8 @@ class TestDataFileDescriptor(unittest.TestCase):
26
26
  assert d.represents_corefile
27
27
  assert not d.represents_extension
28
28
  assert d.type is None
29
- assert d.file_location == '0008333-160118175350007.csv'
30
- assert d.file_encoding == 'utf-8'
29
+ assert d.file_location == "0008333-160118175350007.csv"
30
+ assert d.file_encoding == "utf-8"
31
31
  assert d.lines_terminated_by == "\n"
32
32
  assert d.fields_terminated_by == "\t"
33
33
  assert d.fields_enclosed_by == '"'
@@ -35,8 +35,10 @@ class TestDataFileDescriptor(unittest.TestCase):
35
35
  # Some checks on fields...
36
36
 
37
37
  # A few fields are checked
38
- expected_fields = ({'default': None, 'index': 0, 'term': 'gbifid'},
39
- {'default': None, 'index': 3, 'term': 'kingdom'})
38
+ expected_fields = (
39
+ {"default": None, "index": 0, "term": "gbifid"},
40
+ {"default": None, "index": 3, "term": "kingdom"},
41
+ )
40
42
 
41
43
  for ef in expected_fields:
42
44
  assert ef in d.fields
@@ -46,7 +48,7 @@ class TestDataFileDescriptor(unittest.TestCase):
46
48
 
47
49
  # No fields should have a default value (there's no metafile to set it!)
48
50
  for f in d.fields:
49
- assert f['default'] is None
51
+ assert f["default"] is None
50
52
 
51
53
  # Ensure .terms is also set:
52
54
  assert len(d.terms) == 42
@@ -67,7 +69,9 @@ class TestDataFileDescriptor(unittest.TestCase):
67
69
  </core>
68
70
  """
69
71
 
70
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
72
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
73
+ ET.fromstring(metaxml_section)
74
+ )
71
75
 
72
76
  assert core_descriptor.lines_to_ignore == 0
73
77
 
@@ -83,7 +87,9 @@ class TestDataFileDescriptor(unittest.TestCase):
83
87
  </core>
84
88
  """
85
89
 
86
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
90
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
91
+ ET.fromstring(metaxml_section)
92
+ )
87
93
 
88
94
  assert core_descriptor.lines_to_ignore == 1
89
95
 
@@ -99,7 +105,9 @@ class TestDataFileDescriptor(unittest.TestCase):
99
105
  </core>
100
106
  """
101
107
 
102
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
108
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
109
+ ET.fromstring(metaxml_section)
110
+ )
103
111
 
104
112
  assert core_descriptor.lines_to_ignore == 0
105
113
 
@@ -119,7 +127,9 @@ class TestDataFileDescriptor(unittest.TestCase):
119
127
  </core>
120
128
  """
121
129
 
122
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
130
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
131
+ ET.fromstring(metaxml_section)
132
+ )
123
133
 
124
134
  assert core_descriptor.file_location == "occurrence.txt"
125
135
  assert core_descriptor.file_encoding == "utf-8"
@@ -143,17 +153,22 @@ class TestDataFileDescriptor(unittest.TestCase):
143
153
  </core>
144
154
  """
145
155
 
146
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
156
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
157
+ ET.fromstring(metaxml_section)
158
+ )
147
159
 
148
160
  # .fields is supposed to return a list of dicts like those
149
161
  expected_fields = (
150
- {'term': 'http://rs.tdwg.org/dwc/terms/country',
151
- 'index': None,
152
- 'default': 'Belgium'},
153
-
154
- {'term': 'http://rs.tdwg.org/dwc/terms/scientificName',
155
- 'index': 1,
156
- 'default': None}
162
+ {
163
+ "term": "http://rs.tdwg.org/dwc/terms/country",
164
+ "index": None,
165
+ "default": "Belgium",
166
+ },
167
+ {
168
+ "term": "http://rs.tdwg.org/dwc/terms/scientificName",
169
+ "index": 1,
170
+ "default": None,
171
+ },
157
172
  )
158
173
 
159
174
  for ef in expected_fields:
@@ -162,44 +177,56 @@ class TestDataFileDescriptor(unittest.TestCase):
162
177
  assert len(core_descriptor.fields) == 5
163
178
 
164
179
  def test_headers_simplecases(self):
165
- with DwCAReader(sample_data_path('dwca-2extensions.zip')) as dwca:
180
+ with DwCAReader(sample_data_path("dwca-2extensions.zip")) as dwca:
166
181
  descriptor = dwca.descriptor
167
182
 
168
183
  # With core file...
169
- expected_headers_core = ['id',
170
- 'http://rs.tdwg.org/dwc/terms/order',
171
- 'http://rs.tdwg.org/dwc/terms/class',
172
- 'http://rs.tdwg.org/dwc/terms/kingdom',
173
- 'http://rs.tdwg.org/dwc/terms/phylum',
174
- 'http://rs.tdwg.org/dwc/terms/genus',
175
- 'http://rs.tdwg.org/dwc/terms/family']
184
+ expected_headers_core = [
185
+ "id",
186
+ "http://rs.tdwg.org/dwc/terms/order",
187
+ "http://rs.tdwg.org/dwc/terms/class",
188
+ "http://rs.tdwg.org/dwc/terms/kingdom",
189
+ "http://rs.tdwg.org/dwc/terms/phylum",
190
+ "http://rs.tdwg.org/dwc/terms/genus",
191
+ "http://rs.tdwg.org/dwc/terms/family",
192
+ ]
176
193
 
177
194
  assert descriptor.core.headers == expected_headers_core
178
195
 
179
196
  # And with a first extension...
180
- expected_headers_description_ext = ['coreid',
181
- 'http://purl.org/dc/terms/type',
182
- 'http://purl.org/dc/terms/language',
183
- 'http://purl.org/dc/terms/description']
184
-
185
- desc_ext_descriptor = next(d for d in dwca.descriptor.extensions
186
- if d.type == 'http://rs.gbif.org/terms/1.0/Description')
197
+ expected_headers_description_ext = [
198
+ "coreid",
199
+ "http://purl.org/dc/terms/type",
200
+ "http://purl.org/dc/terms/language",
201
+ "http://purl.org/dc/terms/description",
202
+ ]
203
+
204
+ desc_ext_descriptor = next(
205
+ d
206
+ for d in dwca.descriptor.extensions
207
+ if d.type == "http://rs.gbif.org/terms/1.0/Description"
208
+ )
187
209
 
188
210
  assert desc_ext_descriptor.headers == expected_headers_description_ext
189
211
 
190
212
  # And another one
191
- expected_headers_vernacular_ext = ['coreid',
192
- 'http://rs.tdwg.org/dwc/terms/countryCode',
193
- 'http://purl.org/dc/terms/language',
194
- 'http://rs.tdwg.org/dwc/terms/vernacularName']
195
-
196
- vern_ext_descriptor = next(d for d in dwca.descriptor.extensions
197
- if d.type == 'http://rs.gbif.org/terms/1.0/VernacularName')
213
+ expected_headers_vernacular_ext = [
214
+ "coreid",
215
+ "http://rs.tdwg.org/dwc/terms/countryCode",
216
+ "http://purl.org/dc/terms/language",
217
+ "http://rs.tdwg.org/dwc/terms/vernacularName",
218
+ ]
219
+
220
+ vern_ext_descriptor = next(
221
+ d
222
+ for d in dwca.descriptor.extensions
223
+ if d.type == "http://rs.gbif.org/terms/1.0/VernacularName"
224
+ )
198
225
 
199
226
  assert vern_ext_descriptor.headers == expected_headers_vernacular_ext
200
227
 
201
228
  def test_headers_defaultvalue(self):
202
- """ Ensure headers work properly when confronted to default values (w/o column in file)"""
229
+ """Ensure headers work properly when confronted to default values (w/o column in file)"""
203
230
  metaxml_section = """
204
231
  <core encoding="utf-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n" fieldsEnclosedBy=""
205
232
  ignoreHeaderLines="0" rowType="http://rs.tdwg.org/dwc/terms/Occurrence">
@@ -215,13 +242,17 @@ class TestDataFileDescriptor(unittest.TestCase):
215
242
  </core>
216
243
  """
217
244
 
218
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
245
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
246
+ ET.fromstring(metaxml_section)
247
+ )
219
248
 
220
- expected_headers_core = ['id',
221
- 'http://rs.tdwg.org/dwc/terms/scientificName',
222
- 'http://rs.tdwg.org/dwc/terms/basisOfRecord',
223
- 'http://rs.tdwg.org/dwc/terms/family',
224
- 'http://rs.tdwg.org/dwc/terms/locality']
249
+ expected_headers_core = [
250
+ "id",
251
+ "http://rs.tdwg.org/dwc/terms/scientificName",
252
+ "http://rs.tdwg.org/dwc/terms/basisOfRecord",
253
+ "http://rs.tdwg.org/dwc/terms/family",
254
+ "http://rs.tdwg.org/dwc/terms/locality",
255
+ ]
225
256
 
226
257
  assert core_descriptor.headers == expected_headers_core
227
258
 
@@ -241,9 +272,17 @@ class TestDataFileDescriptor(unittest.TestCase):
241
272
  </core>
242
273
  """
243
274
 
244
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
275
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
276
+ ET.fromstring(metaxml_section)
277
+ )
245
278
 
246
- expected_short_headers_core = ['id', 'scientificName', 'basisOfRecord', 'family', 'locality']
279
+ expected_short_headers_core = [
280
+ "id",
281
+ "scientificName",
282
+ "basisOfRecord",
283
+ "family",
284
+ "locality",
285
+ ]
247
286
 
248
287
  assert core_descriptor.short_headers == expected_short_headers_core
249
288
 
@@ -263,24 +302,28 @@ class TestDataFileDescriptor(unittest.TestCase):
263
302
  <field index="5" term="http://rs.tdwg.org/dwc/terms/genus"/>
264
303
  </core>
265
304
  """
266
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
305
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
306
+ ET.fromstring(metaxml_section)
307
+ )
267
308
 
268
- expected_headers_core = ['id',
269
- 'http://rs.tdwg.org/dwc/terms/order',
270
- 'http://rs.tdwg.org/dwc/terms/class',
271
- 'http://rs.tdwg.org/dwc/terms/kingdom',
272
- 'http://rs.tdwg.org/dwc/terms/phylum',
273
- 'http://rs.tdwg.org/dwc/terms/genus',
274
- 'http://rs.tdwg.org/dwc/terms/family']
309
+ expected_headers_core = [
310
+ "id",
311
+ "http://rs.tdwg.org/dwc/terms/order",
312
+ "http://rs.tdwg.org/dwc/terms/class",
313
+ "http://rs.tdwg.org/dwc/terms/kingdom",
314
+ "http://rs.tdwg.org/dwc/terms/phylum",
315
+ "http://rs.tdwg.org/dwc/terms/genus",
316
+ "http://rs.tdwg.org/dwc/terms/family",
317
+ ]
275
318
 
276
319
  assert core_descriptor.headers == expected_headers_core
277
320
 
278
321
  def test_exposes_raw_element_tag(self):
279
- with DwCAReader(sample_data_path('dwca-simple-test-archive.zip')) as dwca:
322
+ with DwCAReader(sample_data_path("dwca-simple-test-archive.zip")) as dwca:
280
323
  assert isinstance(dwca.descriptor.core.raw_element, ET.Element)
281
324
 
282
325
  def test_content_raw_element_tag(self):
283
- """ Test the content of raw_element seems decent. """
326
+ """Test the content of raw_element seems decent."""
284
327
  ext_section = """
285
328
  <extension encoding="utf-8" fieldsTerminatedBy="\t" linesTerminatedBy="\n"
286
329
  fieldsEnclosedBy="" ignoreHeaderLines="1" rowType="http://rs.gbif.org/terms/1.0/Description">
@@ -292,15 +335,17 @@ class TestDataFileDescriptor(unittest.TestCase):
292
335
  </extension>
293
336
  """
294
337
 
295
- ext_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(ext_section))
338
+ ext_descriptor = DataFileDescriptor.make_from_metafile_section(
339
+ ET.fromstring(ext_section)
340
+ )
296
341
 
297
- assert ext_descriptor.raw_element.tag == 'extension'
298
- assert ext_descriptor.raw_element.get('encoding') == 'utf-8'
299
- assert len(ext_descriptor.raw_element.findall('field')) == 3
342
+ assert ext_descriptor.raw_element.tag == "extension"
343
+ assert ext_descriptor.raw_element.get("encoding") == "utf-8"
344
+ assert len(ext_descriptor.raw_element.findall("field")) == 3
300
345
 
301
346
  def test_tell_if_represents_core(self):
302
347
  # 1. Test with core
303
- with DwCAReader(sample_data_path('dwca-simple-test-archive.zip')) as dwca:
348
+ with DwCAReader(sample_data_path("dwca-simple-test-archive.zip")) as dwca:
304
349
  core_descriptor = dwca.descriptor.core
305
350
  assert core_descriptor.represents_corefile
306
351
  assert not core_descriptor.represents_extension
@@ -317,7 +362,9 @@ class TestDataFileDescriptor(unittest.TestCase):
317
362
  """
318
363
 
319
364
  # 2. And with extension
320
- ext_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(ext_section))
365
+ ext_descriptor = DataFileDescriptor.make_from_metafile_section(
366
+ ET.fromstring(ext_section)
367
+ )
321
368
  assert not ext_descriptor.represents_corefile
322
369
  assert ext_descriptor.represents_extension
323
370
 
@@ -332,7 +379,9 @@ class TestDataFileDescriptor(unittest.TestCase):
332
379
  </extension>
333
380
  """
334
381
 
335
- ext_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(ext_section))
382
+ ext_descriptor = DataFileDescriptor.make_from_metafile_section(
383
+ ET.fromstring(ext_section)
384
+ )
336
385
 
337
386
  assert ext_descriptor.coreid_index == 0
338
387
 
@@ -355,7 +404,9 @@ class TestDataFileDescriptor(unittest.TestCase):
355
404
  </core>
356
405
  """
357
406
 
358
- core_descriptor = DataFileDescriptor.make_from_metafile_section(ET.fromstring(metaxml_section))
407
+ core_descriptor = DataFileDescriptor.make_from_metafile_section(
408
+ ET.fromstring(metaxml_section)
409
+ )
359
410
 
360
411
  assert core_descriptor.id_index == 0
361
412
 
@@ -365,15 +416,15 @@ class TestDataFileDescriptor(unittest.TestCase):
365
416
  def test_exposes_core_type(self):
366
417
  """Test that it exposes the Archive Core Type as type"""
367
418
 
368
- with DwCAReader(sample_data_path('dwca-simple-test-archive.zip')) as dwca:
419
+ with DwCAReader(sample_data_path("dwca-simple-test-archive.zip")) as dwca:
369
420
  coredescriptor = dwca.descriptor.core
370
421
  # dwca-simple-test-archive.zip should be of Occurrence type
371
- assert coredescriptor.type == 'http://rs.tdwg.org/dwc/terms/Occurrence'
422
+ assert coredescriptor.type == "http://rs.tdwg.org/dwc/terms/Occurrence"
372
423
  # Check that shortcuts also work
373
- assert coredescriptor.type == qn('Occurrence')
424
+ assert coredescriptor.type == qn("Occurrence")
374
425
 
375
426
  def test_exposes_core_terms(self):
376
- with DwCAReader(sample_data_path('dwca-star-test-archive.zip')) as star_dwca:
427
+ with DwCAReader(sample_data_path("dwca-star-test-archive.zip")) as star_dwca:
377
428
  # The Core file contains the following rows
378
429
  # <field index="1" term="http://rs.tdwg.org/dwc/terms/family"/>
379
430
  # <field index="2" term="http://rs.tdwg.org/dwc/terms/phylum"/>
@@ -390,12 +441,16 @@ class TestDataFileDescriptor(unittest.TestCase):
390
441
  assert 6 == len(descriptor.core.terms)
391
442
 
392
443
  # Assert correct content (should be a set, so unordered)
393
- fields = set(['http://rs.tdwg.org/dwc/terms/kingdom',
394
- 'http://rs.tdwg.org/dwc/terms/order',
395
- 'http://rs.tdwg.org/dwc/terms/class',
396
- 'http://rs.tdwg.org/dwc/terms/genus',
397
- 'http://rs.tdwg.org/dwc/terms/family',
398
- 'http://rs.tdwg.org/dwc/terms/phylum'])
444
+ fields = set(
445
+ [
446
+ "http://rs.tdwg.org/dwc/terms/kingdom",
447
+ "http://rs.tdwg.org/dwc/terms/order",
448
+ "http://rs.tdwg.org/dwc/terms/class",
449
+ "http://rs.tdwg.org/dwc/terms/genus",
450
+ "http://rs.tdwg.org/dwc/terms/family",
451
+ "http://rs.tdwg.org/dwc/terms/phylum",
452
+ ]
453
+ )
399
454
 
400
455
  assert fields == descriptor.core.terms
401
456
 
@@ -404,7 +459,7 @@ class TestArchiveDescriptor(unittest.TestCase):
404
459
  """Unit tests for ArchiveDescriptor class."""
405
460
 
406
461
  def test_exposes_coredescriptor(self):
407
- with DwCAReader(sample_data_path('dwca-simple-test-archive.zip')) as basic_dwca:
462
+ with DwCAReader(sample_data_path("dwca-simple-test-archive.zip")) as basic_dwca:
408
463
  assert isinstance(basic_dwca.descriptor.core, DataFileDescriptor)
409
464
 
410
465
  def test_exposes_extensions_2ext(self):
@@ -444,7 +499,7 @@ class TestArchiveDescriptor(unittest.TestCase):
444
499
  """
445
500
 
446
501
  d = ArchiveDescriptor(all_metaxml)
447
- expected_extensions_files = ('description.txt', 'vernacularname.txt')
502
+ expected_extensions_files = ("description.txt", "vernacularname.txt")
448
503
  for ext in d.extensions:
449
504
  assert ext.file_location in expected_extensions_files
450
505
 
@@ -490,7 +545,7 @@ class TestArchiveDescriptor(unittest.TestCase):
490
545
  d = ArchiveDescriptor(all_metaxml, files_to_ignore="description.txt")
491
546
 
492
547
  assert len(d.extensions) == 1
493
- assert d.extensions[0].file_location == 'vernacularname.txt'
548
+ assert d.extensions[0].file_location == "vernacularname.txt"
494
549
 
495
550
  def test_exposes_extensions_none(self):
496
551
  all_metaxml = """
@@ -511,32 +566,31 @@ class TestArchiveDescriptor(unittest.TestCase):
511
566
  assert len(d.extensions) == 0
512
567
 
513
568
  def test_exposes_extensions_type(self):
514
- vn = 'http://rs.gbif.org/terms/1.0/VernacularName'
515
- td = 'http://rs.gbif.org/terms/1.0/Description'
569
+ vn = "http://rs.gbif.org/terms/1.0/VernacularName"
570
+ td = "http://rs.gbif.org/terms/1.0/Description"
516
571
 
517
572
  # This archive has no extension, we should get an empty list
518
- with DwCAReader(sample_data_path('dwca-simple-test-archive.zip')) as dwca:
573
+ with DwCAReader(sample_data_path("dwca-simple-test-archive.zip")) as dwca:
519
574
  descriptor = dwca.descriptor
520
575
  assert [] == descriptor.extensions_type
521
576
 
522
577
  # This archive only contains the VernacularName extension
523
- with DwCAReader(sample_data_path('dwca-star-test-archive.zip')) as dwca:
578
+ with DwCAReader(sample_data_path("dwca-star-test-archive.zip")) as dwca:
524
579
  descriptor = dwca.descriptor
525
580
  assert descriptor.extensions_type[0] == vn
526
581
  assert 1 == len(descriptor.extensions_type)
527
582
 
528
583
  # TODO: test with more complex archive
529
- with DwCAReader(sample_data_path('dwca-2extensions.zip')) as dwca:
584
+ with DwCAReader(sample_data_path("dwca-2extensions.zip")) as dwca:
530
585
  descriptor = dwca.descriptor
531
586
  # 2 extensions are in use : vernacular names and taxon descriptions
532
587
  assert 2 == len(descriptor.extensions_type)
533
588
  # USe of frozenset to lose ordering
534
589
  supposed_extensions = frozenset([vn, td])
535
- assert supposed_extensions == \
536
- frozenset(descriptor.extensions_type)
590
+ assert supposed_extensions == frozenset(descriptor.extensions_type)
537
591
 
538
592
  def test_exposes_metadata_filename(self):
539
- with DwCAReader(sample_data_path('dwca-2extensions.zip')) as dwca:
593
+ with DwCAReader(sample_data_path("dwca-2extensions.zip")) as dwca:
540
594
  descriptor = dwca.descriptor
541
595
 
542
596
  assert descriptor.metadata_filename == "eml.xml"