Skip to content

Commit

Permalink
Fix some test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 23, 2024
1 parent 0a6350a commit d2b18dd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
9 changes: 2 additions & 7 deletions mscxyz/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,23 @@ def get_field(self, name: str) -> Field:

def get(self, name: str) -> Any | None:
field = self.get_field(name)

attrs = field.attr_path.split(".")
value = self.score
for attr in attrs:
value = getattr(value, attr)
if value is None:
break

return value

def set(self, name: str, value: Any) -> Any | None:
def set(self, name: str, value: Any) -> None:
field = self.get_field(name)
attrs = field.attr_path.split(".")

last = attrs.pop()

obj = self.score
for attr in attrs:
obj = getattr(obj, attr)
if obj is None:
raise Exception(f"Cannot set attribute {field.attr_path}")

setattr(obj, last, value)

def show(self, pre: dict[str, str], post: dict[str, str]) -> None:
Expand Down Expand Up @@ -302,7 +297,7 @@ def show(self, pre: dict[str, str], post: dict[str, str]) -> None:
# print("{}: {}".format(utils.color(field, field_color), " ".join(line)))

def export_to_dict(self) -> dict[str, FieldValue]:
output: FieldsExport = {}
output: dict[str, FieldValue] = {}
for field in self.names:
value = self.get(field)
if value is not None:
Expand Down
2 changes: 1 addition & 1 deletion mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ def sync_fields(self) -> None:

def write_to_log_file(self, log_file: str, format_string: str) -> None:
log = open(log_file, "w")
log.write(tmep.parse(format_string, self.interface.export_to_dict()) + "\n")
log.write(tmep.parse(format_string, self.score.fields.export_to_dict()) + "\n")
log.close()

def set_field(self, destination_field: str, format_string: str) -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_method_set(self, fields: FieldsManager) -> None:
assert fields.get("title") == new

def test_distribute(self, fields: FieldsManager) -> None:
assert fields.distribute("title,compose", "$title - $composer") == {
"composer": "Queen",
"title": "We are the champions",
}
fields.set("title", "We are the champions - Queen")
fields.distribute("title,composer", "$title - $composer")
assert fields.get("title") == "We are the champions"
assert fields.get("composer") == "Queen"
64 changes: 29 additions & 35 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,38 +391,32 @@ def test_set(self, filename: str, version: int) -> None:


class TestOptionDistributeField:
def test_distribute_field(self) -> None:
c = (
Cli(
"--distribute-field",
"vbox_title",
"$title - $composer",
)
.append_score("meta-distribute-field.mscz")
.execute()
)
def test_distribute_field(self, score: Score) -> None:
score.meta.vbox.title = "Mozart: Alla Turca"
score.save()
c = Cli(
"--distribute-field", "vbox_title", "$composer: $title", score
).execute()
f = c.post.fields
assert f.get("vbox_composer") == "Mozart"
assert f.get("metatag_composer") == "Mozart"
assert f.get("vbox_title") == "Alla Turca"
assert f.get("metatag_work_title") == "Alla Turca"

assert f.get("vbox_composer") == "Composer"
assert f.get("metatag_composer") == "Composer"
assert f.get("vbox_title") == "Title"
assert f.get("metatag_work_title") == "Title"

def test_distribute_field_multple_source_fields(self) -> None:
c = (
Cli(
"--distribute-field",
"vbox_title,readonly_basename",
"$title - $composer",
)
.append_score("Title - Composer.mscz")
.execute()
)
i = c.post.meta.interface
assert i.vbox_composer == "Composer"
assert i.metatag_composer == "Composer"
assert i.vbox_title == "Title"
assert i.metatag_work_title == "Title"
def test_distribute_field_multiple_source_fields(self, score: Score) -> None:
score.meta.vbox.title = "Für Elise - Ludwig van Beethoven"
score.save()
c = Cli(
"--distribute-field",
"vbox_title,metatag_poet",
"$title - $composer",
score,
).execute()
f = c.post.fields
assert f.get("vbox_composer") == "Ludwig van Beethoven"
assert f.get("metatag_composer") == "Ludwig van Beethoven"
assert f.get("vbox_title") == "Für Elise"
assert f.get("metatag_work_title") == "Für Elise"

def test_distribute_field_multiple_values(self) -> None:
c = (
Expand All @@ -437,11 +431,11 @@ def test_distribute_field_multiple_values(self) -> None:
.append_score("meta-distribute-field.mscz")
.execute()
)
i = c.post.meta.interface
assert i.metatag_lyricist == "Composer"
assert i.metatag_composer == "Composer"
assert i.metatag_movement_title == "Title"
assert i.metatag_work_title == "Title"
f = c.post.fields
assert f.get("metatag_lyricist") == "Composer"
assert f.get("metatag_composer") == "Composer"
assert f.get("metatag_movement_title") == "Title"
assert f.get("metatag_work_title") == "Title"

def test_distribute_field_invalid_format_string(self) -> None:
with pytest.raises(meta.FormatStringNoFieldError):
Expand Down

0 comments on commit d2b18dd

Please sign in to comment.