Skip to content

Commit 5cd2ab1

Browse files
committed
squash: rework tests (use fixture, assert_frame_equal)
1 parent 065a1f9 commit 5cd2ab1

File tree

1 file changed

+49
-53
lines changed

1 file changed

+49
-53
lines changed

pandas/tests/io/test_pytables.py

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5170,73 +5170,69 @@ def test_dst_transitions(self):
51705170
assert_frame_equal(result, df)
51715171

51725172

5173-
class TestReadPyTablesHDF5(Base):
5174-
"""
5175-
A group of tests which covers reading HDF5 files written by plain PyTables
5176-
(not written by pandas).
5177-
"""
5178-
5179-
def _create_simple_hdf5_file_with_pytables(self):
5180-
5181-
table_schema = {
5182-
'c0': tables.Time64Col(pos=0),
5183-
'c1': tables.StringCol(5, pos=1),
5184-
'c2': tables.UInt32Col(pos=2),
5185-
}
5173+
@pytest.fixture(scope='module')
5174+
def pytables_hdf5_file():
5175+
"""Use PyTables to create a simple HDF5 file."""
51865176

5187-
t0 = time.time()
5177+
table_schema = {
5178+
'c0': tables.Time64Col(pos=0),
5179+
'c1': tables.StringCol(5, pos=1),
5180+
'c2': tables.Int64Col(pos=2),
5181+
}
51885182

5189-
testsamples = [
5190-
{'c0': t0, 'c1': 'aaaaa', 'c2': 1},
5191-
{'c0': t0 + 1, 'c1': 'bbbbb', 'c2': 2},
5192-
{'c0': t0 + 2, 'c1': 'ccccc', 'c2': 10**5},
5193-
{'c0': t0 + 3, 'c1': 'ddddd', 'c2': 4294967295},
5194-
]
5183+
t0 = time.time()
51955184

5196-
# This returns a path and does not open the file.
5197-
tmpfilepath = create_tempfile(self.path)
5198-
objectname = 'pandas_test_timeseries'
5185+
testsamples = [
5186+
{'c0': t0, 'c1': 'aaaaa', 'c2': 1},
5187+
{'c0': t0 + 1, 'c1': 'bbbbb', 'c2': 2},
5188+
{'c0': t0 + 2, 'c1': 'ccccc', 'c2': 10**5},
5189+
{'c0': t0 + 3, 'c1': 'ddddd', 'c2': 4294967295},
5190+
]
51995191

5200-
with tables.open_file(tmpfilepath, mode='w') as hf:
5201-
t = hf.create_table('/', name=objectname, description=table_schema)
5202-
for sample in testsamples:
5203-
for key, value in sample.items():
5204-
t.row[key] = value
5205-
t.row.append()
5192+
# This returns a path and does not open the file.
5193+
tmpfilepath = create_tempfile('pytables_hdf5_file')
5194+
objectname = 'pandas_test_timeseries'
52065195

5207-
return tmpfilepath, objectname, testsamples
5196+
with tables.open_file(tmpfilepath, mode='w') as hf:
5197+
t = hf.create_table('/', name=objectname, description=table_schema)
5198+
for sample in testsamples:
5199+
for key, value in sample.items():
5200+
t.row[key] = value
5201+
t.row.append()
52085202

5209-
def _compare(self, df, samples):
5210-
"""Compare the reference `samples` with the contents in DataFrame `df`.
5211-
"""
5212-
for idx, row in df.iterrows():
5213-
# Compare Time64Col values with tolerance.
5214-
tm.assert_almost_equal(samples[idx]['c0'], row['c0'])
5203+
return tmpfilepath, objectname, pd.DataFrame(testsamples)
52155204

5216-
# Compare a short string.
5217-
assert samples[idx]['c1'] == row['c1']
52185205

5219-
# Compare an unsigned 32 bit integer.
5220-
assert samples[idx]['c2'] == row['c2']
5206+
class TestReadPyTablesHDF5(Base):
5207+
"""
5208+
A group of tests which covers reading HDF5 files written by plain PyTables
5209+
(not written by pandas).
5210+
"""
52215211

5222-
def test_read_complete(self):
5223-
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5224-
self._compare(pd.read_hdf(path, key=objname), samples)
5212+
def test_read_complete(self, pytables_hdf5_file):
5213+
path, objname, expected_df = pytables_hdf5_file
5214+
assert_frame_equal(pd.read_hdf(path, key=objname), expected_df)
52255215

5226-
def test_read_with_start(self):
5227-
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5216+
def test_read_with_start(self, pytables_hdf5_file):
5217+
path, objname, expected_df = pytables_hdf5_file
52285218
# This is a regression test for pandas-dev/pandas/issues/11188
5229-
self._compare(pd.read_hdf(path, key=objname, start=1), samples[1:])
5219+
assert_frame_equal(
5220+
pd.read_hdf(path, key=objname, start=1),
5221+
expected_df[1:].reset_index(drop=True)
5222+
)
52305223

5231-
def test_read_with_stop(self):
5232-
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5224+
def test_read_with_stop(self, pytables_hdf5_file):
5225+
path, objname, expected_df = pytables_hdf5_file
52335226
# This is a regression test for pandas-dev/pandas/issues/11188
5234-
self._compare(pd.read_hdf(path, key=objname, stop=1), samples[0:1])
5227+
assert_frame_equal(
5228+
pd.read_hdf(path, key=objname, stop=1),
5229+
expected_df[:1].reset_index(drop=True)
5230+
)
52355231

5236-
def test_read_with_startstop(self):
5237-
path, objname, samples = self._create_simple_hdf5_file_with_pytables()
5232+
def test_read_with_startstop(self, pytables_hdf5_file):
5233+
path, objname, expected_df = pytables_hdf5_file
52385234
# This is a regression test for pandas-dev/pandas/issues/11188
5239-
self._compare(
5235+
assert_frame_equal(
52405236
pd.read_hdf(path, key=objname, start=1, stop=2),
5241-
samples[1:2]
5237+
expected_df[1:2].reset_index(drop=True)
52425238
)

0 commit comments

Comments
 (0)