Skip to content

Commit d18d167

Browse files
yt-msMidnighter
authored andcommitted
refactor(Workspace): loads from bytes as well as str
1 parent b72b294 commit d18d167

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/structurizr/workspace.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, Optional, Union
2323

2424
from pydantic import Field
25+
from pydantic.types import StrBytes
2526

2627
from .abstract_base import AbstractBase
2728
from .base_model import BaseModel
@@ -207,9 +208,9 @@ def load(cls, filename: Union[str, Path]) -> "Workspace":
207208
return cls.loads(handle.read())
208209

209210
@classmethod
210-
def loads(cls, json_string: str) -> "Workspace":
211-
"""Load a workspace from a JSON string."""
212-
ws_io = WorkspaceIO.parse_raw(json_string)
211+
def loads(cls, json: StrBytes) -> "Workspace":
212+
"""Load a workspace from a JSON string or bytes."""
213+
ws_io = WorkspaceIO.parse_raw(json)
213214
return cls.hydrate(ws_io)
214215

215216
def dump(

tests/integration/test_workspace_io.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ def test_save_and_load_workspace_to_string(monkeypatch):
101101
assert json.loads(actual.json()) == json.loads(expected.json())
102102

103103

104+
def test_load_workspace_from_bytes(monkeypatch):
105+
"""Test loading from bytes rather than string."""
106+
path = DEFINITIONS / "GettingStarted.json"
107+
with open(path, mode='rb') as file:
108+
binary_content = file.read()
109+
110+
workspace = Workspace.loads(binary_content)
111+
112+
assert workspace.model.software_systems != set()
113+
114+
104115
def test_save_and_load_workspace_to_file(monkeypatch, tmp_path: Path):
105116
"""Test saving as a JSON file and reloading."""
106117
monkeypatch.syspath_prepend(EXAMPLES)
@@ -117,7 +128,7 @@ def test_save_and_load_workspace_to_file(monkeypatch, tmp_path: Path):
117128
assert json.loads(actual.json()) == json.loads(expected.json())
118129

119130

120-
def test_save_and_load_workspace_to_zipped_file(monkeypatch, tmp_path: Path):
131+
def test_save_and_load_workspace_to_gzipped_file(monkeypatch, tmp_path: Path):
121132
"""Test saving as a zipped JSON file and reloading."""
122133
monkeypatch.syspath_prepend(EXAMPLES)
123134
example = import_module("getting_started")

0 commit comments

Comments
 (0)