Skip to content

Commit f1b5f08

Browse files
committed
Improve the tests
1 parent ed20118 commit f1b5f08

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

pygmt/clib/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ def virtualfile_from_stringio(self, stringio: io.StringIO):
16901690
for i, segment in enumerate(segments):
16911691
seg = table.segment[i].contents
16921692
if segment["header"] != "":
1693-
seg.header = segment["header"].encode() # type: ignore[attr-defined]
1693+
seg.header = segment["header"].encode() # type: ignore[attr-defined]
16941694
seg.text = strings_to_ctypes_array(segment["data"])
16951695

16961696
with self.open_virtualfile(family, geometry, "GMT_IN", dataset) as vfile:

pygmt/tests/test_clib_virtualfiles.py

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -415,62 +415,99 @@ class TestVirtualfileFromStringIO:
415415
Test the virtualfile_from_stringio method.
416416
"""
417417

418-
def _check_virtualfile_from_stringio(self, data: str):
418+
def _stringio_to_dataset(self, data: io.StringIO):
419419
"""
420-
A helper function to check the output of the virtualfile_from_stringio method.
420+
A helper function for check the virtualfile_from_stringio method.
421+
422+
The function does the following:
423+
424+
1. Creates a virtual file from the input StringIO object.
425+
2. Pass the virtual file to the ``read`` module, which reads the virtual file
426+
and writes it to another virtual file.
427+
3. Reads the output virtual file as a GMT_DATASET object.
428+
4. Extracts the header and the trailing text from the dataset and returns it as
429+
a string.
421430
"""
422-
# The expected output is the data with all comment lines removed.
423-
expected = (
424-
"\n".join(line for line in data.splitlines() if not line.startswith("#"))
425-
+ "\n"
426-
)
427-
stringio = io.StringIO(data)
428-
with GMTTempFile() as outfile:
429-
with clib.Session() as lib:
430-
with lib.virtualfile_from_stringio(stringio) as vintbl:
431-
lib.call_module("write", args=[vintbl, f"->{outfile.name}", "-Td"])
432-
output = outfile.read()
433-
assert output == expected
431+
with clib.Session() as lib:
432+
with (
433+
lib.virtualfile_from_stringio(data) as vintbl,
434+
lib.virtualfile_out(kind="dataset") as vouttbl,
435+
):
436+
lib.call_module("read", args=[vintbl, vouttbl, "-Td"])
437+
ds = lib.read_virtualfile(vouttbl, kind="dataset").contents
438+
439+
output = []
440+
table = ds.table[0].contents
441+
for segment in table.segment[: table.n_segments]:
442+
seg = segment.contents
443+
output.append(f"> {seg.header.decode()}" if seg.header else ">")
444+
output.extend(np.char.decode(seg.text[: seg.n_rows]))
445+
return "\n".join(output) + "\n"
434446

435447
def test_virtualfile_from_stringio(self):
436448
"""
437449
Test the virtualfile_from_stringio method.
438450
"""
439-
data = (
451+
data = io.StringIO(
440452
"# Comment\n"
441453
"H 24p Legend\n"
442454
"N 2\n"
443455
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n"
444456
)
445-
self._check_virtualfile_from_stringio(data)
457+
expected = (
458+
">\n"
459+
"H 24p Legend\n"
460+
"N 2\n"
461+
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n"
462+
)
463+
assert self._stringio_to_dataset(data) == expected
446464

447465
def test_one_segment(self):
448466
"""
449467
Test the virtualfile_from_stringio method with one segment.
450468
"""
451-
data = (
469+
data = io.StringIO(
452470
"# Comment\n"
453471
"> Segment 1\n"
454-
"H 24p Legend\n"
455-
"N 2\n"
456-
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n"
472+
"1 2 3 ABC\n"
473+
"4 5 DE\n"
474+
"6 7 8 9 FGHIJK LMN OPQ\n"
475+
"RSTUVWXYZ\n"
457476
)
458-
self._check_virtualfile_from_stringio(data)
477+
expected = (
478+
"> Segment 1\n"
479+
"1 2 3 ABC\n"
480+
"4 5 DE\n"
481+
"6 7 8 9 FGHIJK LMN OPQ\n"
482+
"RSTUVWXYZ\n"
483+
)
484+
assert self._stringio_to_dataset(data) == expected
459485

460486
def test_multiple_segments(self):
461487
"""
462488
Test the virtualfile_from_stringio method with multiple segments.
463489
"""
464-
data = (
490+
data = io.StringIO(
465491
"# Comment line 1\n"
466492
"# Comment line 2\n"
467493
"> Segment 1\n"
468-
"H 24p Legend\n"
469-
"N 2\n"
470-
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n"
494+
"1 2 3 ABC\n"
495+
"4 5 DE\n"
496+
"6 7 8 9 FG\n"
497+
"# Comment line 3\n"
471498
"> Segment 2\n"
472-
"H 24p Legend\n"
473-
"N 2\n"
474-
"S 0.1i c 0.15i p300/12 0.25p 0.3i My circle\n"
499+
"1 2 3 ABC\n"
500+
"4 5 DE\n"
501+
"6 7 8 9 FG\n"
502+
)
503+
expected = (
504+
"> Segment 1\n"
505+
"1 2 3 ABC\n"
506+
"4 5 DE\n"
507+
"6 7 8 9 FG\n"
508+
"> Segment 2\n"
509+
"1 2 3 ABC\n"
510+
"4 5 DE\n"
511+
"6 7 8 9 FG\n"
475512
)
476-
self._check_virtualfile_from_stringio(data)
513+
assert self._stringio_to_dataset(data) == expected

0 commit comments

Comments
 (0)