1+ import re
2+ from io import StringIO
13from pathlib import Path
24
5+ import pandas as pd
36import pytest
47
58from ..client import Context , from_context
811from ..server .app import build_app_from_config
912
1013
14+ def load_data (text : str ) -> tuple [list [str ], pd .DataFrame ]:
15+ # This regular expression matches and removes a block of comment lines
16+ #
17+ # Pattern breakdown:
18+ # - #\s*\/+\r?\n
19+ # Matches a line starting with '#', optional whitespace, one or more '/',
20+ # then a newline(unix or windows).
21+ # Example # //////// newline
22+ # - (?:#\s+.*\r?\n)*
23+ # Matches zero or more lines starting with '#', at least one space, then any text,
24+ # then a newline(unix or windows).
25+ # Example # Comment here newline
26+ # - #\s*-+\r?\n
27+ # Matches a line starting with '#', optional whitespace, one or more '-',
28+ # then a newline(unix or windows).
29+ # Example # -----------newline
30+ # Example matched block:
31+ # # ////////
32+ # # Comment Here
33+ # # More Comments
34+ # # -------
35+ text = re .sub (r"#\s*\/+\r?\n(?:#\s+.*\r?\n)*#\s*-+\r?\n" , "" , text )
36+ lines = text .strip ().splitlines ()
37+ metadata_lines = [line .replace (" " , "" ) for line in lines if line .startswith ("#" )]
38+ data_lines = [line .strip () for line in lines if not line .startswith ("#" )]
39+ return metadata_lines , pd .read_csv (
40+ StringIO ("\n " .join (data_lines )), sep = r"\s+" , header = None
41+ )
42+
43+
1144@pytest .mark .asyncio
12- async def test_xdi_round_trip (tmpdir ):
45+ async def test_xdi_round_trip (tmp_path : Path ):
1346 """
1447 Steps:
1548
@@ -20,17 +53,17 @@ async def test_xdi_round_trip(tmpdir):
2053 Compare result of (3) to result of (1).
2154 """
2255 # Write example data file.
23- Path (tmpdir / "files" ).mkdir ()
24- with open (tmpdir / "files" / "example.xdi" , "w" ) as file :
56+ Path (tmp_path / "files" ).mkdir ()
57+ with open (tmp_path / "files" / "example.xdi" , "w" ) as file :
2558 file .write (data )
2659 config = {
2760 "trees" : [
2861 {
2962 "tree" : "catalog" ,
3063 "path" : "/" ,
3164 "args" : {
32- "uri" : tmpdir / "catalog.db" ,
33- "readable_storage" : [tmpdir / "files" ],
65+ "uri" : tmp_path / "catalog.db" ,
66+ "readable_storage" : [tmp_path / "files" ],
3467 "init_if_not_exists" : True ,
3568 "adapters_by_mimetype" : {
3669 "application/x-xdi" : "tiled.examples.xdi:XDIAdapter"
@@ -45,13 +78,16 @@ async def test_xdi_round_trip(tmpdir):
4578 client = from_context (context )
4679 await register (
4780 client ,
48- tmpdir / "files" ,
81+ tmp_path / "files" ,
4982 adapters_by_mimetype = {"application/x-xdi" : "tiled.examples.xdi:XDIAdapter" },
5083 mimetypes_by_file_ext = {".xdi" : "application/x-xdi" },
5184 )
52- client ["example" ].export (str (tmpdir / "exported.xdi" ))
53- actual = Path (tmpdir / "exported.xdi" ).read_text ()
54- assert actual is not None
55- # XDI uses a two-space spacing that pandas.to_csv does not support.
56- # Need thought to get this exactly right.
57- # assert actual == data
85+
86+ client ["example" ].export (str (tmp_path / "exported.xdi" ))
87+ actual = Path (tmp_path / "exported.xdi" ).read_text ()
88+ metadata_actual , df_actual = load_data (actual )
89+ metadata_expected , df_expected = load_data (data )
90+ assert df_actual .equals (df_expected )
91+ assert len (metadata_actual ) == len (metadata_expected ) and set (
92+ metadata_actual
93+ ) == set (metadata_expected )
0 commit comments