Skip to content

Commit

Permalink
Update property tests for pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Jun 12, 2024
1 parent ebb4da2 commit 6a38590
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions tests/properties.py → tests/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
import os
import unittest

from pandas import DataFrame
import pytest

from abcd.backends.atoms_properties import Properties


class PropertiesTests(unittest.TestCase):
class TestProperties:
"""Testing properties data reader"""

@classmethod
def setUpClass(cls):
"""
Load example data file.
"""
@pytest.fixture(autouse=True)
def property(self):
"""Load example data file."""
class_path = os.path.normpath(os.path.abspath(__file__))
data_file = os.path.dirname(class_path) + "/data/examples.csv"
cls.property = Properties(data_file)
return Properties(data_file)

def test_dataframe(self):
def test_dataframe(self, property):
"""
Test data correctly stored in pandas DataFrame.
"""
assert isinstance(self.property.df, DataFrame)
assert len(self.property.df) == 3
assert isinstance(property.df, DataFrame)
assert len(property.df) == 3

def test_specify_units(self):
def test_specify_units(self, property):
"""
Test units can be specified manually, if they match existing fields.
"""
input_units_1 = {"Integers": "items", "Floating": "seconds"}
properties_1 = Properties(
data_file=self.property.data_file,
data_file=property.data_file,
units=input_units_1,
)
self.assertEqual(properties_1.units, input_units_1)
assert properties_1.units == input_units_1

input_units_2 = {"Fake": "m"}
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
properties_1 = Properties(
data_file=self.property.data_file,
data_file=property.data_file,
units=input_units_2,
)

def test_infer_units(self):
def test_infer_units(self, property):
"""
Test units can be inferred from field names.
"""
properties = Properties(
data_file=self.property.data_file,
data_file=property.data_file,
infer_units=True,
)
expected_units = {"Comma units": "m", "Bracket units": "s"}
Expand All @@ -61,17 +59,17 @@ def test_infer_units(self):
"Comma units",
"Bracket units",
]
self.assertEqual(properties.units, expected_units)
self.assertEqual(list(properties.df.columns.values), expected_fields)
assert properties.units == expected_units
assert list(properties.df.columns.values) == expected_fields

def test_struct_file(self):
def test_struct_file(self, property):
"""
Test structure file names can be inferred from a field.
"""
struct_file_template = "test_{struct_name}_file.txt"
struct_name_label = "Text"
properties_1 = Properties(
data_file=self.property.data_file,
data_file=property.data_file,
store_struct_file=True,
struct_file_template=struct_file_template,
struct_name_label=struct_name_label,
Expand All @@ -81,35 +79,35 @@ def test_struct_file(self):
"test_test_file.txt",
"test_data_file.txt",
]
self.assertIsInstance(properties_1.struct_files, list)
assert isinstance(properties_1.struct_files, list)
for i, file in enumerate(expected_struct_files):
self.assertEqual(properties_1.struct_files[i], file)
assert properties_1.struct_files[i] == file

invalid_template = "invalid_template"
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
Properties(
data_file=self.property.data_file,
data_file=property.data_file,
store_struct_file=True,
struct_file_template=invalid_template,
struct_name_label=struct_name_label,
)

invalid_label = "label"
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
Properties(
data_file=self.property.data_file,
data_file=property.data_file,
store_struct_file=True,
struct_file_template=struct_file_template,
struct_name_label=invalid_label,
)

def test_to_list(self):
def test_to_list(self, property):
"""
Test dataframe can be converted into a list of properties.
"""
self.assertEqual(len(self.property.to_list()), 3)
self.assertIsInstance(self.property.to_list(), list)
self.assertIsInstance(self.property.to_list()[0], dict)
assert len(property.to_list()) == 3
assert isinstance(property.to_list(), list)
assert isinstance(property.to_list()[0], dict)
expected_property = {
"Text": "Some",
"Integers": 1,
Expand All @@ -119,9 +117,9 @@ def test_to_list(self):
"Comma units, m": 0,
"Bracket units (s)": 0,
}
self.assertEqual(self.property.to_list()[0], expected_property)
assert property.to_list()[0] == expected_property

def test_missing_data(self):
def test_missing_data(self, property):
"""
Test missing data is not included in properties.
"""
Expand All @@ -133,14 +131,14 @@ def test_missing_data(self):
"Comma units, m": 1,
"Bracket units (s)": 1,
}
self.assertEqual(self.property.to_list()[1], expected_property)
assert property.to_list()[1] == expected_property

def test_to_list_units(self):
def test_to_list_units(self, property):
"""
Test units are included in properties when converting to a list.
"""
properties_1 = Properties(
data_file=self.property.data_file,
data_file=property.data_file,
infer_units=True,
)
expected_units = {"Comma units": "m", "Bracket units": "s"}
Expand All @@ -154,8 +152,4 @@ def test_to_list_units(self):
"Bracket units": 0,
"units": expected_units,
}
self.assertEqual(properties_1.to_list()[0], expected_property)


if __name__ == "__main__":
unittest.main()
assert properties_1.to_list()[0] == expected_property

0 comments on commit 6a38590

Please sign in to comment.