Skip to content

Commit

Permalink
Updated all tests to be run on GitHub actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcurbelo committed Sep 1, 2024
1 parent 8c2e83d commit 9e861c5
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 248 deletions.
5 changes: 4 additions & 1 deletion TESTS/run_all_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Add the parent directory to the system path
sys.path.append(parent_dir)

from UTILS import ROOT_DIR

def load_tests_from_file(test_file):
# Load the test suite from the given file
loader = unittest.TestLoader()
Expand All @@ -30,5 +32,6 @@ def discover_and_run_tests(test_dir):
print(f"Total tests run: {tot_test}")

if __name__ == "__main__":
test_dir = "."

test_dir = ROOT_DIR + '/TESTS'
discover_and_run_tests(test_dir)
4 changes: 3 additions & 1 deletion TESTS/test_data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ def __init__(self, methodName: str = "runTest") -> None:
super().__init__(methodName)

self.root_dir = ROOT_DIR

self.filename1 = 'Pim7uc.all'
self.filename2 = 'Pim7uc.pkl'
self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1

self.filename2 = 'Pim7uc.pkl'
self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2


Expand Down
102 changes: 41 additions & 61 deletions TESTS/test_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
importlib.reload(ms)
import numpy as np

from UTILS import ROOT_DIR

# Define epsilon using np.finfo(float).eps
EPS = np.sqrt(np.finfo(float).eps)

Expand All @@ -22,6 +24,36 @@
# into the terminal

class TestDensityProfile(unittest.TestCase):

def __init__(self, methodName: str = "runTest") -> None:
super().__init__(methodName)

self.root_dir = ROOT_DIR

self.filename1 = 'simple_density.pkl'
self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1

self.filename2 = 'var_density.pkl'
self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2

self.filename3 = 'surfaceImpurity_density.pkl'
self.my_path3 = self.root_dir + '/TESTS/test_data/' + self.filename3

self.filename4 = 'unitCell_density.pkl'
self.my_path4 = self.root_dir + '/TESTS/test_data/' + self.filename4

self.filename5 = 'linked_density.pkl'
self.my_path5 = self.root_dir + '/TESTS/test_data/' + self.filename5

self.filename6 = 'mag_density.pkl'
self.my_path6 = self.root_dir + '/TESTS/test_data/' + self.filename6

self.filename7 = 'dummy_density.pkl'
self.my_path7 = self.root_dir + '/TESTS/test_data/' + self.filename7

self.filename8 = 'negative_density.pkl'
self.my_path8 = self.root_dir + '/TESTS/test_data/' + self.filename8


def test_simple_profile(self):
# Test a simple density profile
Expand All @@ -39,22 +71,14 @@ def test_simple_profile(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'simple_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path1, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
#Counter(loaded_data)
self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements
self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same

total = sum(sum(abs(test_case[key]-solution[key])) for key in test_case.keys())
print('total', total)

# self.assertEqual(total, 0) # checks to make sure that the values are correct
# Assert that the total is close to 0 with an epsilon difference
Expand All @@ -78,13 +102,7 @@ def test_elementVar(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'var_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path2, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
Expand All @@ -108,13 +126,7 @@ def test_surfaceImpurity(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'surfaceImpurity_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path3, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
Expand Down Expand Up @@ -155,13 +167,7 @@ def test_unitCell(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'unitCell_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path4, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
Expand All @@ -188,16 +194,9 @@ def test_linkedRoughness(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'linked_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path5, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
# Counter(loaded_data)
self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements
self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same
Expand Down Expand Up @@ -225,13 +224,7 @@ def test_magnetic(self):
for key in mag_density:
test_case['Mag:' + key] = mag_density[key]

filename = 'mag_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path6, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
Expand All @@ -256,16 +249,9 @@ def test_dummy(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'dummy_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path7, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
# Counter(loaded_data)
self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements
self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same
Expand All @@ -289,16 +275,9 @@ def test_negative_test(self):
test_case = density
test_case['Thickness'] = thickness

filename = 'negative_density.pkl'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

with open(my_path, 'rb') as file:
with open(self.my_path8, 'rb') as file:
solution = pickle.load(file)

# len(loaded_data)
# Counter(loaded_data)
self.assertEqual(len(test_case), len(solution)) # make sure there are the same number of elements
self.assertEqual(Counter(test_case.keys()), Counter(solution.keys())) # checks that all keys are the same
Expand All @@ -307,5 +286,6 @@ def test_negative_test(self):

self.assertEqual(total,0) # checks to make sure that the values are correct


if __name__ == '__main__':
unittest.main()
43 changes: 18 additions & 25 deletions TESTS/test_material_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
import UTILS.material_model as mm
import numpy as np
import UTILS.material_structure as ms
from UTILS import ROOT_DIR
import unittest

# This test script can be executed by inputting
# -> python -m unittest -v test_material_model.py
# into the terminal

class TestMaterialModel(unittest.TestCase):

def __init__(self, methodName: str = "runTest") -> None:
super().__init__(methodName)

self.root_dir = ROOT_DIR

self.filename1 = 'optical_energy.txt'
self.my_path1 = self.root_dir + '/TESTS/test_data/' + self.filename1

self.filename2 = 'optical_theta.txt'
self.my_path2 = self.root_dir + '/TESTS/test_data/' + self.filename2


def test_form_factors_E(self):
form_factors = ['La','Mn','O']
Expand Down Expand Up @@ -87,13 +100,8 @@ def test_form_factors_Elist_mag(self):
self.assertTrue(my_sum <1e-7)

def test_MOC(self):
filename = 'optical_energy.txt'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

solution = np.loadtxt(my_path)
solution = np.loadtxt(self.my_path1)

sample = ms.slab(3)
sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5])
Expand Down Expand Up @@ -136,13 +144,7 @@ def test_MOC(self):

def test_magnetic_optical_constant(self):

filename = 'optical_theta.txt'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

solution = np.loadtxt(my_path)
solution = np.loadtxt(self.my_path2)

sample = ms.slab(3)
sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5])
Expand Down Expand Up @@ -178,13 +180,8 @@ def test_magnetic_optical_constant(self):
self.assertTrue(total_beta<1e-7)

def test_IoR(self):
filename = 'optical_energy.txt'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

solution = np.loadtxt(my_path)
solution = np.loadtxt(self.my_path1)

sample = ms.slab(3)
sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5])
Expand Down Expand Up @@ -220,13 +217,8 @@ def test_IoR(self):
self.assertTrue(total < 1e-7)

def test_index_of_refraction(self):
filename = 'optical_theta.txt'
if os.getcwd().split('\\')[-1] == 'Testing':
my_path = os.getcwd() + '/test_data/' + filename
else:
my_path = os.getcwd() + '/test_data/' + filename

solution = np.loadtxt(my_path)
solution = np.loadtxt(self.my_path2)

sample = ms.slab(3)
sample.addlayer(0, 'SrTiO3', 50, density=[0.028, 0.028, 0.084], roughness=[1.5, 5, 2.5])
Expand Down Expand Up @@ -263,5 +255,6 @@ def test_index_of_refraction(self):
self.assertTrue(total_delta < 1e-7)
self.assertTrue(total_beta < 1e-7)


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit 9e861c5

Please sign in to comment.