Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
WardLT committed Nov 22, 2024
1 parent a753955 commit f4c92b6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/io/test_hdf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from pytest import raises
from pytest import raises, mark
import numpy as np
import pandas as pd
import tables
Expand Down Expand Up @@ -28,23 +28,31 @@ def test_store_df(tmpdir):
assert np.allclose(df_copy['b'], [1., 3.])


def test_append(tmpdir):
@mark.parametrize('prefix', [None, 'a'])
def test_append(tmpdir, prefix):
writer = HDF5Writer()
out_file = Path(tmpdir) / 'example.h5'

# Write the initial data
with tables.open_file(out_file, mode='w') as file:
writer.add_table(file, 'example_table', example_df, ColumnSchema())
if prefix is not None:
file.create_group(file.root, prefix)

writer.add_table(file, 'example_table', example_df, ColumnSchema(), prefix)

# Append the data again
with tables.open_file(out_file, mode='a') as file:
writer.append_to_table(file, 'example_table', example_df)
writer.append_to_table(file, 'example_table', example_df, prefix)

table = file.get_node('/example_table')
table = file.get_node('/example_table' if prefix is None else f'/{prefix}/example_table')
df_copy = read_df_from_table(table)
assert len(df_copy) == len(example_df) * 2
assert np.allclose(df_copy['a'], [1, 2, 1, 2])

# Test data check
with raises(ValueError, match='Existing and new'):
writer.append_to_table(file, 'example_table', pd.DataFrame({'a': [1., 2.]}))
writer.append_to_table(file, 'example_table', pd.DataFrame({'a': [1., 2.]}), prefix)

# Test bad prefix
with raises(ValueError, match='No data available for prefix'):
writer.append_to_table(file, 'example_table', pd.DataFrame({'a': [1., 2.]}), prefix='b')

0 comments on commit f4c92b6

Please sign in to comment.