-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_tests.py
148 lines (125 loc) · 6.74 KB
/
Main_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# MIT License
#
# Copyright (c) 2023 Nicolette Shaw
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import unittest
from Main import *
cwd = os.getcwd()
path_periodicTable = cwd + "/PeriodicInfo.csv"
path_totESCSdata = cwd + "/TotalESCS.csv"
class TestLoadData(unittest.TestCase):
def test_get_totESCS_data(self):
# Test with a known element, Carbon.
element = "Carbon"
expected_output = {1: 5.28E-01, 2: 2.86E-01, 4: 1.50E-01, 8: 7.75E-02, 16: 4.01E-02, 32: 2.11E-02, 64: 1.15E-02,
128: 6.71E-03, 256: 4.35E-03}
self.assertEqual(loadData.get_totESCS_data(element, path_totESCSdata), expected_output)
# Test with an element that does not exist in the data.
element = 'X'
expected_output = "Element X is not available in TotalESCS data."
with self.assertRaises(SystemExit) as cm:
loadData.get_totESCS_data(element, path_totESCSdata)
self.assertEqual(cm.exception, expected_output)
# Test with an empty string as the element.
element = ''
expected_output = "Element given to get_totESCS_data is empty."
with self.assertRaises(SystemExit) as cm:
loadData.get_totESCS_data(element, path_totESCSdata)
self.assertEqual(cm.exception, expected_output)
def test_get_element_data(self):
# Test that the function correctly retrieves element data for a given element.
elem = 'Carbon'
expected_output = {'Z': 6, 'MW': 12.011}
self.assertEqual(loadData.get_element_data(elem, path_periodicTable), expected_output)
# Test with an element that does not exist in the data.
element = 'X'
expected_output = "Element X is not available in PeriodicInfo data."
with self.assertRaises(SystemExit) as cm:
loadData.get_totESCS_data(element, path_totESCSdata)
self.assertEqual(cm.exception, expected_output)
# Test with an empty string as the element.
element = ''
expected_output = "Element given to get_element_data is empty."
with self.assertRaises(SystemExit) as cm:
loadData.get_totESCS_data(element, path_totESCSdata)
self.assertEqual(cm.exception, expected_output)
class TestMathHelper(unittest.TestCase):
def test_TotESCStoTotSCS(self):
# Test that the function correctly converts totESCS to totSCS.
totESCS = 2.0
Znum = 6
expected_output = 8.0
self.assertEqual(mathHelper.TotESCStoTotSCS(totESCS, Znum), expected_output)
# Test that the function handles totESCS = 0 correctly.
totESCS = 0
Znum = 6
expected_output = 0
self.assertEqual(mathHelper.TotESCStoTotSCS(totESCS, Znum), expected_output)
class TestCompoundCalculationHelper(unittest.TestCase):
def assertcompoundDictAlmostEqual(self, d1, d2):
# First layer.
# check if both inputs are dicts
self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
# check if both inputs have the same keys
self.assertEqual(d1.keys(), d2.keys())
# Check that internal 'totSCS' and 'atomic' dicts are equal.
for key, value in d1.items():
# Check periodic info dict.
self.assertEqual(d1[key]['atomic'], d2[key]['atomic'])
# Check keys for d1[key]['totSCS'] (energies).
self.assertEqual(d1[key]['totSCS'].keys(), d2[key]['totSCS'].keys())
for i in d1[key]['totSCS'].keys():
self.assertAlmostEqual(d1[key]['totSCS'][i], d2[key]['totSCS'][i], places=5)
def test_converttotESCStoSCS(self):
# Test for single element in compound.
compoundDict = compoundInfo(name='Carbon', density=2.0, molecularFormulaDict={"Carbon": 1})
compoundCalcHelper = compoundCalculationHelper(compoundDict)
expected_output = {'Carbon': {
'totSCS': {1: 2.112, 2: 1.144, 4: 0.6, 8: 0.31, 16: 0.1604, 32: 0.0844, 64: 0.046, 128: 0.02684,
256: 0.0174},
'atomic': {'Z': 6, 'MW': 12.011}}}
self.assertcompoundDictAlmostEqual(compoundCalcHelper.converttotESCStoSCS(), expected_output)
# Test for multiple elements in compound.
compoundDict = compoundInfo(name='Silicon nitride', density=3.44,
molecularFormulaDict={"Silicon": 3, "Nitrogen": 4})
compoundCalcHelper = compoundCalculationHelper(compoundDict)
expected_output = {'Silicon': {
'totSCS': {1: 3.08571428571429, 2: 1.86742857142857, 4: 1.07885714285714, 8: 0.596571428571429,
16: 0.322285714285714, 32: 0.173485714285714, 64: 0.0957714285714286, 128: 0.0562285714285714,
256: 0.0365714285714286},
'atomic': {'Z': 14, 'MW': 28.0855}},
'Nitrogen': {'totSCS': {1: 1.91071428571429, 2: 1.06071428571429, 4: 0.567857142857143,
8: 0.295357142857143, 16: 0.153571428571429, 32: 0.0810714285714286,
64: 0.0442857142857143, 128: 0.0258214285714286, 256: 0.01675},
'atomic': {'Z': 7, 'MW': 14.0067}}
}
self.assertcompoundDictAlmostEqual(compoundCalcHelper.converttotESCStoSCS(), expected_output)
# def test_calcNumDensity(self):
# compoundDict = {'density': 1.2}
# compoundCalcHelper = compoundCalculationHelper(compoundDict)
#
# # Test that the method correctly calculates number density
# elementMW = 12.0107
# expected_output = 4.988667836854447e+22
# self.assertEqual(compoundCalcHelper.calcNumDensity(elementMW), expected_output)
if __name__ == '__main__':
unittest.main()