Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mango_mdschema/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ def flattened_to_mango_avu(flattened: tuple, prefix: str = None) -> iRODSMeta:
name = re.sub(r"\[\d+\]", "", key) # remove list indices from the normalized key
units = ".".join(indices).rstrip(".0") # remove trailing zero indices
return iRODSMeta(
name=f"{prefix}.{name}" if prefix else name,
value=value,
units=units if units != "" else None,
f"{prefix}.{name}" if prefix else name,
value,
units if units != "" else None,
)


Expand Down
78 changes: 39 additions & 39 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def test_flattened_to_mango_avus(self):
# Test case 2: List of tuples with simple fields
input_list = [("a", 1), ("b", 2), ("c", 3), ("d", 4)]
expected_output = [
iRODSMeta(name=f"{prefix}.a", value="1", units=None),
iRODSMeta(name=f"{prefix}.b", value="2", units=None),
iRODSMeta(name=f"{prefix}.c", value="3", units=None),
iRODSMeta(name=f"{prefix}.d", value="4", units=None),
iRODSMeta(f"{prefix}.a", "1", None),
iRODSMeta(f"{prefix}.b", "2", None),
iRODSMeta(f"{prefix}.c", "3", None),
iRODSMeta(f"{prefix}.d", "4", None),
]
self.assertEqual(
list(map(lambda x: flattened_to_mango_avu(x, prefix), input_list)),
Expand All @@ -107,10 +107,10 @@ def test_flattened_to_mango_avus(self):
# Test case 3: List of tuples with nested fields
input_list = [("a.b.c", 1), ("a.b.d", 2), ("a.e", 3), ("f", 4)]
expected_output = [
iRODSMeta(name=f"{prefix}.a.b.c", value="1", units="1.1"),
iRODSMeta(name=f"{prefix}.a.b.d", value="2", units="1.1"),
iRODSMeta(name=f"{prefix}.a.e", value="3", units="1"),
iRODSMeta(name=f"{prefix}.f", value="4", units=None),
iRODSMeta(f"{prefix}.a.b.c", "1", "1.1"),
iRODSMeta(f"{prefix}.a.b.d", "2", "1.1"),
iRODSMeta(f"{prefix}.a.e", "3", "1"),
iRODSMeta(f"{prefix}.f", "4", None),
]
self.assertEqual(
list(map(lambda x: flattened_to_mango_avu(x, prefix), input_list)),
Expand All @@ -128,12 +128,12 @@ def test_flattened_to_mango_avus(self):
("g.h[1].j", 6),
]
expected_output = [
iRODSMeta(name=f"{prefix}.a.b.c", value="1", units="1.1"),
iRODSMeta(name=f"{prefix}.a.b.c", value="2", units="2.1"),
iRODSMeta(name=f"{prefix}.e.f", value="3", units="1"),
iRODSMeta(name=f"{prefix}.e.f", value="4", units="1"),
iRODSMeta(name=f"{prefix}.g.h.i", value="5", units="1.1"),
iRODSMeta(name=f"{prefix}.g.h.j", value="6", units="1.2"),
iRODSMeta(f"{prefix}.a.b.c", "1", "1.1"),
iRODSMeta(f"{prefix}.a.b.c", "2", "2.1"),
iRODSMeta(f"{prefix}.e.f", "3", "1"),
iRODSMeta(f"{prefix}.e.f", "4", "1"),
iRODSMeta(f"{prefix}.g.h.i", "5", "1.1"),
iRODSMeta(f"{prefix}.g.h.j", "6", "1.2"),
]
self.assertEqual(
list(map(lambda x: flattened_to_mango_avu(x, prefix), input_list)),
Expand Down Expand Up @@ -165,26 +165,26 @@ def test_dict_to_mango_avus(self):
"w": {"x": [{"y": 20}, {"y": 21}], "z": 22},
}
expected_output = [
iRODSMeta(name=f"{prefix}.a", value="1", units=None),
iRODSMeta(name=f"{prefix}.b.c", value="2", units="1"),
iRODSMeta(name=f"{prefix}.b.d.e", value="3", units="1.1"),
iRODSMeta(name=f"{prefix}.b.d.f", value="4", units="1.1"),
iRODSMeta(name=f"{prefix}.b.i", value="7", units="1"),
iRODSMeta(name=f"{prefix}.j.k", value="8", units="1"),
iRODSMeta(name=f"{prefix}.j.l", value="9", units="1"),
iRODSMeta(name=f"{prefix}.j.m", value="10", units="2"),
iRODSMeta(name=f"{prefix}.j.n", value="11", units="2"),
iRODSMeta(name=f"{prefix}.o", value="12", units=None),
iRODSMeta(name=f"{prefix}.o", value="13", units=None),
iRODSMeta(name=f"{prefix}.p.q.r", value="14", units="1.1"),
iRODSMeta(name=f"{prefix}.p.q.r", value="15", units="1.2"),
iRODSMeta(name=f"{prefix}.p.s", value="16", units="1"),
iRODSMeta(name=f"{prefix}.p.t.u", value="17", units="2.1"),
iRODSMeta(name=f"{prefix}.p.t.u", value="18", units="2.2"),
iRODSMeta(name=f"{prefix}.p.v", value="19", units="2"),
iRODSMeta(name=f"{prefix}.w.x.y", value="20", units="1.1"),
iRODSMeta(name=f"{prefix}.w.x.y", value="21", units="1.2"),
iRODSMeta(name=f"{prefix}.w.z", value="22", units="1"),
iRODSMeta(f"{prefix}.a", "1", None),
iRODSMeta(f"{prefix}.b.c", "2", "1"),
iRODSMeta(f"{prefix}.b.d.e", "3", "1.1"),
iRODSMeta(f"{prefix}.b.d.f", "4", "1.1"),
iRODSMeta(f"{prefix}.b.i", "7", "1"),
iRODSMeta(f"{prefix}.j.k", "8", "1"),
iRODSMeta(f"{prefix}.j.l", "9", "1"),
iRODSMeta(f"{prefix}.j.m", "10", "2"),
iRODSMeta(f"{prefix}.j.n", "11", "2"),
iRODSMeta(f"{prefix}.o", "12", None),
iRODSMeta(f"{prefix}.o", "13", None),
iRODSMeta(f"{prefix}.p.q.r", "14", "1.1"),
iRODSMeta(f"{prefix}.p.q.r", "15", "1.2"),
iRODSMeta(f"{prefix}.p.s", "16", "1"),
iRODSMeta(f"{prefix}.p.t.u", "17", "2.1"),
iRODSMeta(f"{prefix}.p.t.u", "18", "2.2"),
iRODSMeta(f"{prefix}.p.v", "19", "2"),
iRODSMeta(f"{prefix}.w.x.y", "20", "1.1"),
iRODSMeta(f"{prefix}.w.x.y", "21", "1.2"),
iRODSMeta(f"{prefix}.w.z", "22", "1"),
]
self.assertEqual(
list(map(lambda x: flattened_to_mango_avu(x, prefix), flatten(input_dict))),
Expand All @@ -208,11 +208,11 @@ def test_reversibility(self):
is converted to the following list of AVUs:

[
iRODSMeta(name="mgs.test.a", value=1, units=None),
iRODSMeta(name="mgs.test.b.c", value=2, units="1"),
iRODSMeta(name="mgs.test.b.d.e", value=3, units="1.1"),
iRODSMeta(name="mgs.test.b.d.f", value=4, units="1.1"),
iRODSMeta(name="mgs.test.b.i", value=7, units="1"),
iRODSMeta("mgs.test.a", 1, None),
iRODSMeta("mgs.test.b.c", 2, "1"),
iRODSMeta("mgs.test.b.d.e", 3, "1.1"),
iRODSMeta("mgs.test.b.d.f", 4, "1.1"),
iRODSMeta("mgs.test.b.i", 7, "1"),
]

When this list of AVUs is converted back to a nested dictionary, the
Expand Down
120 changes: 60 additions & 60 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,100 +136,100 @@ def setUp(self):
self.avus = [
[
iRODSMeta(
name="mgs.book.title", value=self.metadata[0]["title"], units=None
"mgs.book.title", self.metadata[0]["title"], None
),
iRODSMeta(
name="mgs.book.author.name",
value=self.metadata[0]["author"][0]["name"],
units="1",
"mgs.book.author.name",
self.metadata[0]["author"][0]["name"],
"1",
),
iRODSMeta(
name="mgs.book.author.email",
value=self.metadata[0]["author"][0]["email"][0],
units="1",
"mgs.book.author.email",
self.metadata[0]["author"][0]["email"][0],
"1",
),
iRODSMeta(
name="mgs.book.author.age",
value=str(self.metadata[0]["author"][0]["age"]),
units="1",
"mgs.book.author.age",
str(self.metadata[0]["author"][0]["age"]),
"1",
),
iRODSMeta(
name="mgs.book.publishing_date",
value=self.metadata[0]["publishing_date"],
units=None,
"mgs.book.publishing_date",
self.metadata[0]["publishing_date"],
None,
),
iRODSMeta(
name="mgs.book.cover.colors",
value=self.metadata[0]["cover"]["colors"][0],
units="1",
"mgs.book.cover.colors",
self.metadata[0]["cover"]["colors"][0],
"1",
),
iRODSMeta(
name="mgs.book.cover.colors",
value=self.metadata[0]["cover"]["colors"][1],
units="1",
"mgs.book.cover.colors",
self.metadata[0]["cover"]["colors"][1],
"1",
),
iRODSMeta(
name="mgs.book.cover.type",
value=self.metadata[0]["cover"]["type"],
units="1",
"mgs.book.cover.type",
self.metadata[0]["cover"]["type"],
"1",
),
iRODSMeta(
name="mgs.book.publisher",
value=self.metadata[0]["publisher"],
units=None,
"mgs.book.publisher",
self.metadata[0]["publisher"],
None,
),
],
[
iRODSMeta(
name="mgs.book.title", value=self.metadata[1]["title"], units=None
"mgs.book.title", self.metadata[1]["title"], None
),
iRODSMeta(
name="mgs.book.author.name",
value=self.metadata[1]["author"][0]["name"],
units="1",
"mgs.book.author.name",
self.metadata[1]["author"][0]["name"],
"1",
),
iRODSMeta(
name="mgs.book.author.email",
value=self.metadata[1]["author"][0]["email"][0],
units="1",
"mgs.book.author.email",
self.metadata[1]["author"][0]["email"][0],
"1",
),
iRODSMeta(
name="mgs.book.author.age",
value=str(self.metadata[1]["author"][0]["age"]),
units="1",
"mgs.book.author.age",
str(self.metadata[1]["author"][0]["age"]),
"1",
),
iRODSMeta(
name="mgs.book.author.name",
value=self.metadata[1]["author"][1]["name"],
units="2",
"mgs.book.author.name",
self.metadata[1]["author"][1]["name"],
"2",
),
iRODSMeta(
name="mgs.book.author.email",
value=self.metadata[1]["author"][1]["email"][0],
units="2",
"mgs.book.author.email",
self.metadata[1]["author"][1]["email"][0],
"2",
),
iRODSMeta(
name="mgs.book.author.age",
value=str(self.metadata[1]["author"][1]["age"]),
units="2",
"mgs.book.author.age",
str(self.metadata[1]["author"][1]["age"]),
"2",
),
iRODSMeta(
name="mgs.book.publishing_date",
value=self.metadata[1]["publishing_date"],
units=None,
"mgs.book.publishing_date",
self.metadata[1]["publishing_date"],
None,
),
iRODSMeta(
name="mgs.book.cover.type",
value=self.metadata[1]["cover"]["type"],
units="1",
"mgs.book.cover.type",
self.metadata[1]["cover"]["type"],
"1",
),
iRODSMeta(
name="mgs.book.ebook", value=self.metadata[1]["ebook"], units=None
"mgs.book.ebook", self.metadata[1]["ebook"], None
),
iRODSMeta(
name="mgs.book.publisher",
value=self.schema.fields["publisher"].default,
units=None,
"mgs.book.publisher",
self.schema.fields["publisher"].default,
None,
),
],
]
Expand Down Expand Up @@ -366,14 +366,14 @@ def test_empty_metadata(self):
self.data_object.reset_mock()
self.data_object.metadata.items.return_value = [
iRODSMeta(
name="mgs.other_schema.title",
value="Title for other schema",
units=None,
"mgs.other_schema.title",
"Title for other schema",
None,
),
iRODSMeta(
name="subject.length",
value="11",
units="cm",
"subject.length",
"11",
"cm",
),
]

Expand Down
Loading