Skip to content

Commit 9e1ffa4

Browse files
BenjaminCharmesml-evs
authored andcommitted
Various test updates needed for pydantic v2:
- Reorder imports for better isolation - Additional test cases for schema functionality
1 parent bbe9683 commit 9e1ffa4

22 files changed

+272
-140
lines changed

pydatalab/tests/apps/test_chat.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from pydatalab.apps.chat import ChatBlock
2-
3-
41
def test_chatblock():
2+
from pydatalab.apps.chat import ChatBlock
3+
54
chat = ChatBlock(item_id="test")
65
chat.data["model"] = "langchain-debug"
76

pydatalab/tests/apps/test_echem_block.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
from pathlib import Path
22

33
import pytest
4-
from navani.echem import echem_file_loader
5-
6-
from pydatalab.apps.echem.utils import (
7-
compute_gpcl_differential,
8-
filter_df_by_cycle_index,
9-
reduce_echem_cycle_sampling,
10-
)
114

125

136
@pytest.fixture
147
def echem_dataframe():
158
"""Yields example echem data as a dataframe."""
9+
from navani.echem import echem_file_loader
10+
1611
df = echem_file_loader(
1712
Path(__file__)
1813
.parent.joinpath(
@@ -38,15 +33,27 @@ def echem_dataframe():
3833

3934
@pytest.fixture
4035
def reduced_echem_dataframe(echem_dataframe):
36+
from pydatalab.apps.echem.utils import (
37+
reduce_echem_cycle_sampling,
38+
)
39+
4140
return reduce_echem_cycle_sampling(echem_dataframe, 100)
4241

4342

4443
@pytest.fixture
4544
def reduced_and_filtered_echem_dataframe(reduced_echem_dataframe):
45+
from pydatalab.apps.echem.utils import (
46+
filter_df_by_cycle_index,
47+
)
48+
4649
return filter_df_by_cycle_index(reduced_echem_dataframe)
4750

4851

4952
def test_reduce_size(echem_dataframe):
53+
from pydatalab.apps.echem.utils import (
54+
reduce_echem_cycle_sampling,
55+
)
56+
5057
original_size = echem_dataframe.shape[0]
5158
for size in (1, 10, int(0.5 * len(echem_dataframe)), len(echem_dataframe)):
5259
number_of_cycles = echem_dataframe["half cycle"].nunique()
@@ -57,6 +64,10 @@ def test_reduce_size(echem_dataframe):
5764

5865

5966
def test_compute_gpcl_differential(reduced_and_filtered_echem_dataframe):
67+
from pydatalab.apps.echem.utils import (
68+
compute_gpcl_differential,
69+
)
70+
6071
df = reduced_and_filtered_echem_dataframe
6172

6273
dqdv_results = compute_gpcl_differential(df)
@@ -67,13 +78,20 @@ def test_compute_gpcl_differential(reduced_and_filtered_echem_dataframe):
6778

6879

6980
def test_filter_df_by_cycle_index(reduced_echem_dataframe):
81+
from pydatalab.apps.echem.utils import (
82+
filter_df_by_cycle_index,
83+
)
84+
7085
cycle_lists = ([1, 2, 3], [4.0, 6.0, 10.0], [-1, 5, 2])
7186
for cycle_list in cycle_lists:
7287
filtered_df = filter_df_by_cycle_index(reduced_echem_dataframe, cycle_list)
7388
assert {int(i) for i in filtered_df["full cycle"]}.issubset({int(i) for i in cycle_list})
7489

7590

7691
def test_plot(reduced_echem_dataframe):
92+
from pydatalab.apps.echem.utils import (
93+
compute_gpcl_differential,
94+
)
7795
from pydatalab.bokeh_plots import double_axes_echem_plot
7896

7997
layout = double_axes_echem_plot(reduced_echem_dataframe)

pydatalab/tests/apps/test_ftir_block.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import pytest
44

5-
from pydatalab.apps.ftir import FTIRBlock
6-
75

86
@pytest.fixture
97
def data_files():
108
return (Path(__file__).parent.parent.parent / "example_data" / "FTIR").glob("*")
119

1210

1311
def test_load(data_files):
12+
from pydatalab.apps.ftir import FTIRBlock
13+
1414
for f in data_files:
1515
df = FTIRBlock.parse_ftir_asp(f)
1616
assert len(df) == 1932
@@ -25,6 +25,8 @@ def test_load(data_files):
2525

2626

2727
def test_plot(data_files):
28+
from pydatalab.apps.ftir import FTIRBlock
29+
2830
f = next(data_files)
2931
ftir_data = FTIRBlock.parse_ftir_asp(f)
3032
layout = FTIRBlock._format_ftir_plot(ftir_data)

pydatalab/tests/apps/test_nmr.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
import pytest
66

7-
from pydatalab.apps.nmr.blocks import NMRBlock
8-
from pydatalab.apps.nmr.utils import read_bruker_1d, read_jcamp_dx_1d
9-
107

118
def _extract_example(filename, _dir):
129
with zipfile.ZipFile(filename, "r") as zip_ref:
@@ -64,6 +61,8 @@ def nmr_2d_matpass_example(tmpdir, nmr_2d_matpass_path):
6461

6562

6663
def test_bruker_reader_solution(nmr_1d_solution_example):
64+
from pydatalab.apps.nmr.utils import read_bruker_1d
65+
6766
df, a_dic, topspin_title, shape = read_bruker_1d(nmr_1d_solution_example)
6867
assert df is not None
6968
assert a_dic
@@ -72,6 +71,8 @@ def test_bruker_reader_solution(nmr_1d_solution_example):
7271

7372

7473
def test_bruker_reader_solid(nmr_1d_solid_example):
74+
from pydatalab.apps.nmr.utils import read_bruker_1d
75+
7576
df, a_dic, topspin_title, shape = read_bruker_1d(nmr_1d_solid_example)
7677
assert df is not None
7778
assert a_dic
@@ -80,6 +81,8 @@ def test_bruker_reader_solid(nmr_1d_solid_example):
8081

8182

8283
def test_bruker_reader_2D(nmr_2d_matpass_example):
84+
from pydatalab.apps.nmr.utils import read_bruker_1d
85+
8386
df, a_dic, topspin_title, shape = read_bruker_1d(nmr_2d_matpass_example)
8487
assert df is None
8588
assert a_dic
@@ -90,6 +93,8 @@ def test_bruker_reader_2D(nmr_2d_matpass_example):
9093
def test_nmr_block(
9194
nmr_1d_solution_path, nmr_1d_solution_path_renamed, nmr_1d_solid_path, nmr_2d_matpass_path
9295
):
96+
from pydatalab.apps.nmr.blocks import NMRBlock
97+
9398
block = NMRBlock(item_id="nmr-block")
9499
block.processed_data, block.data["metadata"] = block.read_bruker_nmr_data(nmr_1d_solid_path)
95100
assert block.data["metadata"]["topspin_title"].split("\n")[0] == "7Li 40 kHz 40 C hahn-echo"
@@ -124,6 +129,9 @@ def test_nmr_block(
124129

125130

126131
def test_read_jcamp_1h_1d(nmr_jcamp_1h_path):
132+
from pydatalab.apps.nmr.blocks import NMRBlock
133+
from pydatalab.apps.nmr.utils import read_jcamp_dx_1d
134+
127135
df, dic, title, shape = read_jcamp_dx_1d(nmr_jcamp_1h_path)
128136
assert df is not None
129137
assert dic[".OBSERVENUCLEUS"]
@@ -136,6 +144,9 @@ def test_read_jcamp_1h_1d(nmr_jcamp_1h_path):
136144

137145

138146
def test_read_jcamp_13c_1d(nmr_jcamp_13c_path):
147+
from pydatalab.apps.nmr.blocks import NMRBlock
148+
from pydatalab.apps.nmr.utils import read_jcamp_dx_1d
149+
139150
df, dic, title, shape = read_jcamp_dx_1d(nmr_jcamp_13c_path)
140151
assert df is not None
141152
assert dic[".OBSERVENUCLEUS"]

pydatalab/tests/apps/test_plugins.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Load block types first to avoid circular dependency issues
2-
from pydatalab.apps import BLOCK_TYPES, BLOCKS, load_block_plugins
32

43

54
def test_load_insitu_nmr_plugin():
65
from datalab_app_plugin_insitu import InsituBlock
76

7+
from pydatalab.apps import BLOCK_TYPES, BLOCKS, load_block_plugins
8+
89
plugins = load_block_plugins()
910

1011
assert plugins["insitu-nmr"] == InsituBlock
@@ -16,6 +17,8 @@ def test_load_insitu_nmr_plugin():
1617
def test_load_insitu_uvvis_plugin():
1718
from datalab_app_plugin_insitu import UVVisInsituBlock
1819

20+
from pydatalab.apps import BLOCK_TYPES, BLOCKS, load_block_plugins
21+
1922
plugins = load_block_plugins()
2023

2124
assert plugins["insitu-uvvis"] == UVVisInsituBlock

pydatalab/tests/apps/test_raman.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import numpy as np
44
import pytest
55

6-
from pydatalab.apps.raman.blocks import RamanBlock
7-
86

97
@pytest.fixture
108
def wdf_example():
@@ -24,6 +22,8 @@ def renishaw_txt_example():
2422

2523

2624
def test_load_wdf(wdf_example):
25+
from pydatalab.apps.raman.blocks import RamanBlock
26+
2727
df, metadata, y_options = RamanBlock.load(wdf_example)
2828
assert all(y in df.columns for y in y_options)
2929
assert df.shape == (1011, 11)
@@ -45,6 +45,8 @@ def test_load_wdf(wdf_example):
4545

4646

4747
def test_load_renishaw_txt(renishaw_txt_example):
48+
from pydatalab.apps.raman.blocks import RamanBlock
49+
4850
with pytest.warns(UserWarning, match="Unable to find wavenumber unit"):
4951
with pytest.warns(UserWarning, match="Unable to find wavenumber offset"):
5052
df, metadata, y_options = RamanBlock.load(renishaw_txt_example)
@@ -61,6 +63,8 @@ def test_load_renishaw_txt(renishaw_txt_example):
6163

6264

6365
def test_load_labspec_txt(labspec_txt_example):
66+
from pydatalab.apps.raman.blocks import RamanBlock
67+
6468
df, metadata, y_options = RamanBlock.load(labspec_txt_example)
6569
assert all(y in df.columns for y in y_options)
6670
assert df.shape == (341, 11)
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import pytest
22

3-
from pydatalab.blocks.common import TabularDataBlock
4-
53

64
def test_load_raman_txt(example_data_dir):
5+
from pydatalab.blocks.common import TabularDataBlock
6+
77
with pytest.warns(UserWarning):
88
df = TabularDataBlock.load(example_data_dir / "raman" / "raman_example.txt")
99
assert df.shape == (1011, 2)
1010

1111

1212
def test_load_labspec_raman_txt(example_data_dir):
13+
from pydatalab.blocks.common import TabularDataBlock
14+
1315
with pytest.warns(UserWarning):
1416
df = TabularDataBlock.load(example_data_dir / "raman" / "labspec_raman_example.txt")
1517
assert df.shape == (341, 2)
1618

1719

1820
def test_simple_csv(example_data_dir):
21+
from pydatalab.blocks.common import TabularDataBlock
22+
1923
df = TabularDataBlock.load(example_data_dir / "csv" / "simple.csv")
2024
assert df.shape == (2, 3)
2125
assert df.columns.tolist() == ["test", "test2", "test3"]
2226

2327

2428
def test_simple_xlsx(example_data_dir):
29+
from pydatalab.blocks.common import TabularDataBlock
30+
2531
df = TabularDataBlock.load(example_data_dir / "csv" / "simple.xlsx")
2632
assert df.shape == (4, 4)

pydatalab/tests/apps/test_uvvis.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
import pytest
55

6-
from pydatalab.apps.uvvis import UVVisBlock
7-
from pydatalab.apps.uvvis.utils import find_absorbance, parse_uvvis_txt
8-
96

107
@pytest.fixture
118
def data_files():
129
return (Path(__file__).parent.parent.parent / "example_data" / "UV-Vis").glob("*")
1310

1411

1512
def test_load(data_files):
13+
from pydatalab.apps.uvvis import UVVisBlock
14+
from pydatalab.apps.uvvis.utils import parse_uvvis_txt
15+
1616
logging.info("Testing UV-Vis data loading")
1717
for f in data_files:
1818
logging.info(f"Testing file: {f}")
@@ -30,6 +30,9 @@ def test_find_absorbance(
3030
reference_file="1908047U1_0000.Raw8.TXT",
3131
sample_files=["1908047U1_0001.Raw8.txt", "1908047U1_0060.Raw8.txt"],
3232
):
33+
from pydatalab.apps.uvvis import UVVisBlock
34+
from pydatalab.apps.uvvis.utils import find_absorbance, parse_uvvis_txt
35+
3336
# Tests two specific sample files with one reference file, will fail if files aren't present in the example data
3437
reference_path = (
3538
Path(__file__).parent.parent.parent / "example_data" / "UV-Vis" / reference_file
@@ -62,6 +65,9 @@ def test_plot(
6265
reference_file="1908047U1_0000.Raw8.TXT",
6366
sample_files=["1908047U1_0001.Raw8.txt", "1908047U1_0060.Raw8.txt"],
6467
):
68+
from pydatalab.apps.uvvis import UVVisBlock
69+
from pydatalab.apps.uvvis.utils import find_absorbance, parse_uvvis_txt
70+
6571
reference_path = (
6672
Path(__file__).parent.parent.parent / "example_data" / "UV-Vis" / reference_file
6773
)

pydatalab/tests/apps/test_xrd_block.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
import pytest
44

5-
from pydatalab.apps.xrd.blocks import XRDBlock
6-
75
XRD_DATA_FILES = list((Path(__file__).parent.parent.parent / "example_data" / "XRD").glob("*"))
86

97

108
@pytest.mark.parametrize("f", XRD_DATA_FILES)
119
def test_load(f):
10+
from pydatalab.apps.xrd.blocks import XRDBlock
11+
1212
if f.suffix in XRDBlock.accepted_file_extensions:
1313
df, y_options, metadata = XRDBlock.load_pattern(f)
1414
assert all(y in df.columns for y in y_options)
1515

1616

1717
def test_event():
18+
from pydatalab.apps.xrd.blocks import XRDBlock
19+
1820
block = XRDBlock(item_id="test-id")
1921
assert block.data["wavelength"] == 1.54060
2022
block.process_events({"event_name": "set_wavelength", "wavelength": 1.0})
@@ -28,6 +30,8 @@ def test_event():
2830

2931
@pytest.mark.parametrize("f", XRD_DATA_FILES)
3032
def test_single_plots(f):
33+
from pydatalab.apps.xrd.blocks import XRDBlock
34+
3135
if f.suffix in XRDBlock.accepted_file_extensions:
3236
block = XRDBlock(item_id="test")
3337
block.generate_xrd_plot(f)

pydatalab/tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66

7-
@pytest.fixture(scope="session")
7+
@pytest.fixture(autouse=True, scope="session")
88
def monkeypatch_session():
99
from _pytest.monkeypatch import MonkeyPatch
1010

@@ -13,13 +13,13 @@ def monkeypatch_session():
1313
m.undo()
1414

1515

16-
@pytest.fixture(scope="session")
16+
@pytest.fixture(autouse=True, scope="session")
1717
def secret_key():
1818
"""Fixture to provide a secret key for testing purposes."""
1919
return hashlib.sha512(b"test").hexdigest()
2020

2121

22-
@pytest.fixture(scope="session", autouse=True)
22+
@pytest.fixture(autouse=True)
2323
def override_environment_variables(monkeypatch_session, secret_key):
2424
"""Override the secret key and other environment variables that will
2525
otherwise fallover outside of testing mode.

0 commit comments

Comments
 (0)