def test_get_single_row_if_channel_missing() -> None: """ Test if we can convert a single row of data into a dictionary. """ csv_string = StringIO("""id,channel,value S1,single,foo """) df = pd.read_csv(csv_string, sep=",") row1 = _get_single_channel_row(df, None, "subject") assert isinstance(row1, dict) assert row1 == {"channel": "single", "id": "S1", "value": "foo"} csv_string2 = StringIO("""id,channel,value S1,double,foo S1,double,foo """) df2 = pd.read_csv(csv_string2, sep=",") with pytest.raises(ValueError) as ex: _get_single_channel_row(df2, None, "NoSubject") assert "NoSubject" in str(ex) assert "2 rows" in str(ex)
def test_get_single_row() -> None: """ Test if we can extract the unique row that has a given value in the "channel" column. """ csv_string = StringIO("""id,channel,value S1,single,foo S2,double,bar S2,double,baz """) df = pd.read_csv(csv_string, sep=",") row1 = _get_single_channel_row(df, "single", "subject") assert isinstance(row1, dict) # The returned dictionary appears to have its columns sorted by name, not in the order in the datafile! assert row1 == {"channel": "single", "id": "S1", "value": "foo"} with pytest.raises(ValueError) as ex: _get_single_channel_row(df, "notPresent", "NoSubject") assert "NoSubject" in str(ex) assert "0 rows" in str(ex) with pytest.raises(ValueError) as ex: _get_single_channel_row(df, "double", "NoSubject") assert "NoSubject" in str(ex) assert "2 rows" in str(ex)