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
|
# Copyright (c) 2024, the cclib development team
#
# This file is part of cclib (http://cclib.github.io) and is distributed under
# the terms of the BSD 3-Clause License.
"""Unit tests for parser data module."""
from unittest import mock
import cclib
import numpy
class StringContains(str):
def __eq__(self, other):
return self in other
class ccDataTest:
"""Unit tests for the ccData class."""
check_array = ["atomcoords", "scfenergies"]
check_arrlist = ["mocoeffs", "scfvalues"]
check_arrdict = ["atomcharges", "atomspins"]
check_dictdict = []
def setup_method(self) -> None:
self.data = cclib.parser.logfileparser.ccData()
self.mock_logger = mock.Mock()
def _set_attributes(self, names, val):
for n in names:
setattr(self.data, n, val)
def test_valuecheck_negative_etenergies(self) -> None:
self.data.etenergies = [1, -1]
self.data.check_values(logger=self.mock_logger)
self.mock_logger.error.assert_called_once_with(
StringContains("At least one excitation energy is negative")
)
def test_listify_ndarray(self) -> None:
"""Does the method convert ndarrays as expected?"""
self._set_attributes(self.check_array, numpy.array([1, 2, 3]))
self.data.listify()
for attr in self.check_array:
assert isinstance(getattr(self.data, attr), list)
def test_listify_arraylist(self) -> None:
"""Does the method convert lists of arrays as expected?"""
self._set_attributes(self.check_arrlist, [numpy.array([1, 2, 3]), numpy.array([4, 5, 6])])
self.data.listify()
for attr in self.check_arrlist:
for a in getattr(self.data, attr):
assert isinstance(a, list)
def test_listify_arraydict(self) -> None:
"""Does the method convert dicts of arrays as expected?"""
self._set_attributes(
self.check_arrdict, {1: numpy.array([1, 2, 3]), 2: numpy.array([4, 5, 6])}
)
self.data.listify()
for attr in self.check_arrdict:
for a in getattr(self.data, attr).values():
assert isinstance(a, list)
def test_listify_dictdict(self) -> None:
"""Does the method convert dicts of dicts as expected?"""
self._set_attributes(
self.check_dictdict,
{1: {1: [1, 2, 3], 2: [4, 5, 6]}, 2: {3: [7, 8, 9], 4: [10, 11, 12]}},
)
self.data.listify()
for attr in self.check_dictdict:
for a in getattr(self.data, attr).values():
for b in getattr(attr, a).values():
assert isinstance(b, list)
def test_arrayify_ndarray(self) -> None:
"""Does the method convert lists as expected?"""
self._set_attributes(self.check_array, [1, 2, 3])
self.data.arrayify()
for attr in self.check_array:
assert isinstance(getattr(self.data, attr), numpy.ndarray)
def test_arrayify_arraylist(self) -> None:
"""Does the method convert lists of lists as expected?"""
self._set_attributes(self.check_arrlist, [[1, 2, 3], [4, 5, 6]])
self.data.arrayify()
for attr in self.check_arrlist:
for a in getattr(self.data, attr):
assert isinstance(a, numpy.ndarray)
def test_arrayify_arraydict(self) -> None:
"""Does the method convert dicts of lists as expected?"""
self._set_attributes(self.check_arrdict, {1: [1, 2, 3], 2: [4, 5, 6]})
self.data.arrayify()
for attr in self.check_arrdict:
for a in getattr(self.data, attr).values():
assert isinstance(a, numpy.ndarray)
def test_arrayify_dictdict(self) -> None:
"""Does the method convert dicts of lists as expected?"""
self._set_attributes(
self.check_dictdict,
{1: {1: [1, 2, 3], 2: [4, 5, 6]}, 2: {3: [7, 8, 9], 4: [10, 11, 12]}},
)
self.data.arrayify()
for attr in self.check_dictdict:
for a in getattr(self.data, attr).values():
for b in getattr(attr, a).values():
assert isinstance(b, numpy.ndarray)
|