Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoelfe committed Jul 23, 2024
1 parent 9baea61 commit 4e311c5
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/tests/data/from_list.star
/tests/data/test_overwrite_flag.star
/tests/data/test_write_with_float_format.star
/tests/data/test_overwrite_backup.star
/build/
/dist/
/m2relion/
Expand Down
2 changes: 1 addition & 1 deletion src/starfile/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .functions import read, write
from .functions import read, write, to_string
6 changes: 4 additions & 2 deletions src/starfile/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __init__(

if filename is not None:
self.filename = Path(filename)
else:
self.filename = None
self.float_format = float_format
self.sep = separator
self.na_rep = na_rep
Expand Down Expand Up @@ -69,7 +71,7 @@ def lines(self) -> Generator[str, None, None]:

def write(self):
if self.filename is None:
raise Exception('Cannot write nameless file!')
raise ValueError('Cannot write nameless file!')
with open(self.filename, mode='w+') as f:
f.writelines(line + '\n' for line in self.lines())

Expand All @@ -96,7 +98,7 @@ def data_block_generator(self) -> Generator[str, None, None]:
yield line

def backup_if_file_exists(self):
if self.filename.exists():
if self.filename and self.filename.exists():
new_name = self.filename.name + '~'
backup_path = self.filename.resolve().parent / new_name
if backup_path.exists():
Expand Down
8 changes: 8 additions & 0 deletions tests/test_functional_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ def test_read_non_existent_file():

with pytest.raises(FileNotFoundError):
starfile.read(f)


def test_generate_string():
star_string = starfile.to_string(test_df)
output_file = test_data_directory / "test_write.star"
starfile.write(test_df, output_file, overwrite=True)
with open(output_file, "r") as f:
assert f.read() == star_string
4 changes: 2 additions & 2 deletions tests/test_read_write_round_trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def test_round_trip_postprocess(tmp_path):
assert _actual == _expected


def test_write_read_write_read():
filename = 'tmp.star'
def test_write_read_write_read(tmp_path):
filename = tmp_path / 'tmp.star'
df_a = pd.DataFrame({'a': [0, 1], 'b': [2, 3]})
starfile.write(df_a, filename)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ def test_string_quoting_simple_datablock(quote_character, quote_all_strings,num_

s = StarParser(filename)
assert o == s.data_blocks[""]


def test_no_filename_error():
with pytest.raises(ValueError):
StarWriter(test_df).write()

0 comments on commit 4e311c5

Please sign in to comment.