Skip to content

Commit f8959d2

Browse files
committed
apply_overwrite_policy() should apply the removal of empty strings
1 parent c434777 commit f8959d2

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/osw/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ class _ApplyOverwriteParam(OswBaseModel):
780780
namespace: Optional[str]
781781
meta_category_title: Optional[str]
782782
meta_category_template_str: Optional[str]
783-
remove_empty_strings: Optional[bool] = False
783+
remove_empty_strings: Optional[bool] = True
784784
inplace: Optional[bool] = False
785785
debug: Optional[bool] = False
786786

@@ -892,6 +892,8 @@ def set_content(content_to_set: dict) -> None:
892892
for slot in ["jsondata", "header", "footer"]: # SLOTS:
893893
remote_content[slot] = page.get_slot_content(slot)
894894
# Todo: remote content does not contain properties that are not set
895+
if param.remove_empty_strings:
896+
remove_empty_strings(remote_content["jsondata"])
895897
if remote_content["header"]: # not None or {} or ""
896898
new_content["header"] = remote_content["header"]
897899
if remote_content["footer"]:
@@ -902,6 +904,8 @@ def set_content(content_to_set: dict) -> None:
902904
# Properties that are not set in the local content will be set to None
903905
# We want those not to be listed as keys
904906
local_content["jsondata"] = json.loads(param.entity.json(exclude_none=True))
907+
if param.remove_empty_strings:
908+
remove_empty_strings(local_content["jsondata"])
905909
if param.debug:
906910
print(f"'local_content': {str(remote_content)}")
907911
# Apply the overwrite logic

tests/test_osl.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import osw.model.entity as model
66
from osw.core import OSW, AddOverwriteClassOptions, OverwriteOptions
7+
from osw.utils.wiki import remove_empty_strings
78
from osw.wtsite import WtPage
89

910

@@ -55,7 +56,8 @@ def check_false(original: model.Entity, altered: model.Entity, stored: model.Ent
5556
but add those additionally present in the altered entity."""
5657
assert stored.label[0].text == original.label[0].text
5758
assert stored.name == original.name
58-
assert stored.iri == original.iri
59+
# empty string property is removed on store and load:
60+
assert stored.iri == altered.iri
5961
if len(original.description) == 0:
6062
assert stored.description == original.description
6163
else:
@@ -74,6 +76,7 @@ def check_only_empty(
7476
entity in the OSW that are empty, but are not empty in the altered entity."""
7577
assert stored.label[0].text == original.label[0].text
7678
assert stored.name == original.name
79+
# empty string property is removed on store and load:
7780
assert stored.iri == altered.iri
7881
assert stored.description[0].text == altered.description[0].text
7982
assert stored.query_label == altered.query_label
@@ -102,7 +105,9 @@ def check_keep_existing(
102105
'keep existing', which is supposed to keep the existing entity in the OSW."""
103106
assert stored.label[0].text == original.label[0].text
104107
assert stored.name == original.name
105-
assert stored.iri == original.iri
108+
# assert stored.iri == original.iri
109+
# empty string property is removed on store and load:
110+
assert getattr(stored, "iri", None) is None
106111
assert stored.description == original.description # empty list
107112
assert stored.query_label == original.query_label
108113
assert stored.image == original.image
@@ -135,7 +140,7 @@ def test_apply_overwrite_policy():
135140

136141
for check in checks:
137142
# Create a new item with some properties
138-
original_item = model.Item(
143+
original_item_local = model.Item(
139144
label=[model.Label(text="My Item")],
140145
name="MyItem",
141146
iri="", # Empty string property
@@ -146,11 +151,19 @@ def test_apply_overwrite_policy():
146151
original_page = OfflineWtPage(
147152
# wtSite=OSW.wt_site, # todo: missing
148153
title="Item:"
149-
+ OSW.get_osw_id(original_item.uuid)
150-
)
151-
original_page.set_slot_content(
152-
"jsondata", json.loads(original_item.json(exclude_none=True))
154+
+ OSW.get_osw_id(original_item_local.uuid)
153155
)
156+
jsondata = json.loads(original_item_local.json(exclude_none=True))
157+
# Emulate the default setting for StoreEntityParam.remove_empty_strings,
158+
# which is True and applied on store_entity
159+
remove_empty_strings(jsondata)
160+
161+
original_page.set_slot_content("jsondata", jsondata)
162+
# To reproduce what happens in the OSL instance, we need to get the content
163+
# from the page again
164+
original_content = original_page.get_slot_content("jsondata")
165+
# And create the original item from that content
166+
original_item = model.Item(**original_content)
154167
# Alter some of the property values
155168
altered_props = {
156169
"label": [model.Label(text="My Item Duplicate")],

0 commit comments

Comments
 (0)