11"""Tests for the pydantic dataset model module."""
2-
32import pytest
3+ from pydantic import ValidationError
44
5- from mdverse_scrapers .models .dataset import DatasetCoreMetadata , DatasetMetadata
5+ from mdverse_scrapers .models .dataset import (
6+ DatasetCoreMetadata ,
7+ DatasetMetadata ,
8+ SimulationMetadata ,
9+ )
610from mdverse_scrapers .models .enums import DatasetProjectName , DatasetRepositoryName
711
812
13+ # -------------------------------
14+ # Tests for dataset core metadata
15+ # -------------------------------
916def test_dataset_core_metadata_minimal ():
1017 """Test creation with only required fields."""
1118 dataset = DatasetCoreMetadata (
@@ -52,6 +59,9 @@ def test_dataset_core_metadata_full():
5259 assert "dataset_url_in_project" in dumped
5360
5461
62+ # --------------------------
63+ # Tests for dataset metadata
64+ # --------------------------
5565def test_dataset_metadata_with_additional_fields ():
5666 """Test DatasetMetadata including extra fields like title, date_created, etc."""
5767 dataset = DatasetMetadata (
@@ -68,12 +78,12 @@ def test_dataset_metadata_with_additional_fields():
6878
6979 # Optional simulation fields default to None
7080 assert dataset .software_name is None
71- assert dataset .nb_atoms is None
81+ assert dataset .number_of_atoms is None
7282
7383 dumped = dataset .model_dump (exclude_none = True )
7484 # Fields with None should be removed
7585 assert "software_name" not in dumped
76- assert "nb_atoms " not in dumped
86+ assert "number_of_atoms " not in dumped
7787 # Fields with values should remain
7888 assert "title" in dumped
7989 assert "dataset_repository_name" in dumped
@@ -99,3 +109,88 @@ def test_invalid_types():
99109 dataset_project_name = None ,
100110 )
101111 assert dataset .dataset_project_name is None
112+
113+
114+ # ------------------------------------------------
115+ # Tests for positive values in simulation metadata
116+ # ------------------------------------------------
117+ def test_validate_positive_simulation_values_float ():
118+ """Test numeric values are accepted if positive."""
119+ obj = SimulationMetadata (
120+ dataset_repository_name = DatasetRepositoryName .NOMAD ,
121+ dataset_id_in_repository = "dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
122+ dataset_url_in_repository = "https://nomad-lab.eu/prod/v1/gui/search/entries?entry_id=dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
123+ simulation_timestep = 0.5 ,
124+ simulation_time = [1.2 ]
125+ )
126+ assert obj .simulation_timestep == 0.5
127+ assert obj .simulation_time == [1.2 ]
128+
129+
130+ def test_validate_positive_simulation_values_str_with_units ():
131+ """Test string numeric values with units are accepted."""
132+ obj = SimulationMetadata (
133+ dataset_repository_name = DatasetRepositoryName .NOMAD ,
134+ dataset_id_in_repository = "dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
135+ dataset_url_in_repository = "https://nomad-lab.eu/prod/v1/gui/search/entries?entry_id=dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
136+ simulation_timestep = "0.1fs" ,
137+ simulation_time = ["0.5fs" , "1.0fs" ]
138+ )
139+ assert obj .simulation_timestep == "0.1fs"
140+ assert obj .simulation_time == ["0.5fs" , "1.0fs" ]
141+
142+
143+ def test_validate_positive_simulation_values_negative ():
144+ """Test negative numbers raise validation error."""
145+ with pytest .raises (ValidationError ):
146+ SimulationMetadata (
147+ dataset_repository_name = DatasetRepositoryName .NOMAD ,
148+ dataset_id_in_repository = "dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
149+ dataset_url_in_repository = "https://nomad-lab.eu/prod/v1/gui/search/entries?entry_id=dNdV1k67vGSN1DUhrBeOSvJeBnvv" ,
150+ simulation_timestep = - 1.0
151+ )
152+
153+
154+ # ---------------------------------------------------
155+ # Tests for temperature values in simulation metadata
156+ # ---------------------------------------------------
157+ def test_normalize_temperatures_single_kelvin ():
158+ """Test a single temperature given in Kelvin."""
159+ temp = "300K"
160+ normalized = SimulationMetadata .normalize_temperatures (temp )
161+ assert normalized == [300.0 ]
162+
163+
164+ def test_normalize_temperatures_single_celsius ():
165+ """Test a single temperature given in Celsius."""
166+ temp = "27°C"
167+ normalized = SimulationMetadata .normalize_temperatures (temp )
168+ assert normalized == [300.15 ]
169+
170+
171+ def test_normalize_temperatures_no_unit_above_273 ():
172+ """Test a temperature with no unit assumed to be Kelvin if >= 273."""
173+ temp = "280"
174+ normalized = SimulationMetadata .normalize_temperatures (temp )
175+ assert normalized == [280.0 ]
176+
177+
178+ def test_normalize_temperatures_no_unit_below_273 ():
179+ """Test a temperature with no unit assumed to be Celsius if < 273."""
180+ temp = "25"
181+ normalized = SimulationMetadata .normalize_temperatures (temp )
182+ assert normalized == [298.15 ]
183+
184+
185+ def test_normalize_temperatures_list_mixed_units ():
186+ """Test a list of temperatures with mixed units."""
187+ temps = ["25°C" , "300K" , "50" ]
188+ normalized = SimulationMetadata .normalize_temperatures (temps )
189+ expected = [298.15 , 300.0 , 323.15 ]
190+ assert normalized == expected
191+
192+
193+ def test_normalize_temperatures_none ():
194+ """Test that None input returns None."""
195+ normalized = SimulationMetadata .normalize_temperatures (None )
196+ assert normalized is None
0 commit comments