Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
veni-vidi-vici-dormivi committed Nov 6, 2024
1 parent ac81c1b commit bb9f08b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
22 changes: 20 additions & 2 deletions filefinder/_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,12 +652,30 @@ def search(self, **query):
return type(self)(df)

def concat(self, other):
"""concatenate two FileContainers"""
"""concatenate two FileContainers
Parameters
----------
other : FileContainer
The other FileContainer to concatenate.
Returns
-------
FileContainer
The concatenated FileContainer.
Raises
------
ValueError
If the other object is not a FileContainer.
ValueError
If the two FileContainers do not have the same keys.
"""

if not isinstance(other, FileContainer):
raise ValueError("Can only concatenate two FileContainers.")

if not all(self.df.columns == other.df.columns):
if not self.df.columns is other.df.columns:
raise ValueError("FileContainers must have the same keys.")

ret = copy.copy(self)
Expand Down
17 changes: 17 additions & 0 deletions filefinder/tests/test_filecontainer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from numpy import diff
import pandas as pd
import pytest

Expand Down Expand Up @@ -140,6 +141,22 @@ def test_filecontainer_search(example_df, example_fc):
pd.testing.assert_frame_equal(result.df, expected)


def test_filecontainer_concat(example_fc):

with pytest.raises(ValueError, match="Can only concatenate two FileContainers."):
example_fc.concat("not a FileContainer")

with pytest.raises(ValueError, match="FileContainers must have the same keys"):
different_keys_fc = FileContainer(example_fc.df.loc[:, ["model", "scen"]])
example_fc.concat(different_keys_fc)

result = example_fc.concat(example_fc)
expected = pd.concat([example_fc.df, example_fc.df])

pd.testing.assert_frame_equal(result.df, expected)
assert result.__len__() == 10


def test_fc_combine_by_key_deprecated(example_fc):

with pytest.warns(FutureWarning, match="`combine_by_key` has been deprecated"):
Expand Down
17 changes: 0 additions & 17 deletions filefinder/tests/test_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,20 +689,3 @@ def test_find_unparsable():
ValueError, match="Could not parse 'a_b/' with the pattern '{cat}_{cat}/'"
):
ff.find_paths()


def test_concat_FileContainers():

ff = FileFinder("{a}", "{b}", test_paths=["a/b", "c/d"])
result1 = ff.find_files()
result2 = ff.find_files()

result = result1.concat(result2)
expected = pd.concat([result1.df, result2.df])

pd.testing.assert_frame_equal(result.df, expected)

ff1 = FileFinder("{a}", "{c}", test_paths=["a/b", "c/d"])
result_diffkeys = ff1.find_files()


0 comments on commit bb9f08b

Please sign in to comment.