Skip to content

Commit ff669e7

Browse files
committed
added mime type test
1 parent 46364ac commit ff669e7

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

fileformats/core/classifier.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ def namespace(cls) -> ty.Optional[str]:
2020
module_parts = cls.__module__.split(".")
2121
if module_parts[0] != "fileformats":
2222
raise FormatDefinitionError(
23-
f"Cannot create reversible MIME type for {cls} as it is not in the "
24-
"fileformats namespace"
23+
f"Cannot determine namespace for {cls} format as it is not in the "
24+
"fileformats namespace package"
2525
)
2626
namespace = module_parts[1]
2727
if namespace == "vendor":
2828
if len(module_parts) < 4:
2929
raise FormatDefinitionError(
30-
f"Cannot create reversible MIME type for {cls} as it is not in the "
31-
"fileformats namespace"
30+
f"Cannot determine namespace for vendor-specific format, {cls} it needs "
31+
"to be in a subpackage of the form `fileformats.vendor.<vendor-name>.<namespace>`,"
32+
f"found `{'.'.join(module_parts)}`"
3233
)
3334
namespace = module_parts[3]
3435
return namespace.replace("_", "-")

fileformats/core/tests/test_classifiers.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
from __future__ import annotations
2+
23
import decimal
4+
35
import pytest
46
from pydra.compose import python
5-
from fileformats.core import from_mime, DataType, FileSet
6-
from fileformats.core import converter
7+
78
from fileformats.application import Zip
8-
from fileformats.generic import DirectoryOf
9-
from fileformats.field import Array, Integer, Decimal, Text, Boolean
9+
from fileformats.core import DataType, FileSet, converter, from_mime
1010
from fileformats.core.exceptions import (
11-
FormatDefinitionError,
1211
FormatConversionError,
13-
FormatRecognitionError,
12+
FormatDefinitionError,
1413
FormatMismatchError,
14+
FormatRecognitionError,
1515
)
16+
from fileformats.field import Array, Boolean, Decimal, Integer, Text
17+
from fileformats.generic import DirectoryOf
18+
from fileformats.testing import J # Y,
1619
from fileformats.testing import (
1720
A,
1821
B,
1922
C,
23+
Classified,
2024
D,
2125
E,
2226
F,
2327
G,
2428
H,
25-
J,
2629
K,
2730
L,
2831
M,
@@ -31,16 +34,13 @@
3134
Q,
3235
R,
3336
TestField,
34-
Classified,
3537
U,
3638
V,
3739
W,
3840
X,
39-
# Y,
4041
Z,
4142
)
4243

43-
4444
SpecificDataType = DataType.type_var("SpecificDataType")
4545
SpecificFileSet = FileSet.type_var("SpecificFileSet")
4646

@@ -242,9 +242,28 @@ def test_mime_roundtrips():
242242
assert from_mime("testing/b.a+k") is K[B, A]
243243
assert from_mime("testing/b.a+k") is not K[A, B]
244244

245-
with pytest.raises(FormatRecognitionError) as e:
245+
with pytest.raises(
246+
FormatRecognitionError, match="Cannot create reversible MIME type"
247+
):
246248
Array[TestField].mime_like
247-
assert "Cannot create reversible MIME type for " in str(e)
249+
250+
251+
def test_mime_fail():
252+
class BadFormat(DataType):
253+
pass
254+
255+
with pytest.raises(FormatDefinitionError, match="Cannot determine namespace"):
256+
BadFormat.namespace
257+
258+
259+
def test_vendor_mime_fail():
260+
class BadVendorFormat(DataType):
261+
pass
262+
263+
BadVendorFormat.__module__ = "fileformats.vendor.badnamespace"
264+
265+
with pytest.raises(FormatDefinitionError, match="Cannot determine namespace"):
266+
BadVendorFormat.namespace
248267

249268

250269
def test_inherited_classifiers():
@@ -430,3 +449,4 @@ def test_classifier_categories6():
430449
match="Cannot have more than one occurrence of a classifier ",
431450
):
432451
Classified[C, E]
452+
Classified[C, E]

0 commit comments

Comments
 (0)