| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2019, the cclib development team |
| 4 |
# |
| 5 |
# This file is part of cclib (http://cclib.github.io) and is distributed under |
| 6 |
# the terms of the BSD 3-Clause License. |
| 7 |
|
| 8 |
"""Unit tests for parser data module.""" |
| 9 |
|
| 10 |
import unittest |
| 11 |
|
| 12 |
import numpy |
| 13 |
|
| 14 |
import cclib |
| 15 |
|
| 16 |
from six import add_move, MovedModule |
| 17 |
add_move(MovedModule('mock', 'mock', 'unittest.mock')) |
| 18 |
from six.moves import mock |
| 19 |
|
| 20 |
|
| 21 |
class StringContains(str): |
| 22 |
def __eq__(self, other): |
| 23 |
return self in other |
| 24 |
|
| 25 |
|
| 26 |
class ccDataTest(unittest.TestCase): |
| 27 |
"""Unit tests for the ccData class.""" |
| 28 |
|
| 29 |
check_array = ['atomcoords', 'scfenergies'] |
| 30 |
check_arrlist = ['mocoeffs', 'scfvalues'] |
| 31 |
check_arrdict = ['atomcharges', 'atomspins'] |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
self.data = cclib.parser.logfileparser.ccData() |
| 35 |
self.mock_logger = mock.Mock() |
| 36 |
|
| 37 |
def _set_attributes(self, names, val): |
| 38 |
for n in names: |
| 39 |
setattr(self.data, n, val) |
| 40 |
|
| 41 |
def test_valuecheck_negative_etenergies(self): |
| 42 |
self.data.etenergies = [1, -1] |
| 43 |
self.data.check_values(logger=self.mock_logger) |
| 44 |
self.mock_logger.error.assert_called_once_with( |
| 45 |
StringContains("At least one excitation energy is negative")) |
| 46 |
|
| 47 |
def test_listify_ndarray(self): |
| 48 |
"""Does the method convert ndarrays as expected?""" |
| 49 |
self._set_attributes(self.check_array, numpy.array([1,2,3])) |
| 50 |
self.data.listify() |
| 51 |
for attr in self.check_array: |
| 52 |
self.assertIsInstance(getattr(self.data, attr), list) |
| 53 |
|
| 54 |
def test_listify_arraylist(self): |
| 55 |
"""Does the method convert lists of arrays as expected?""" |
| 56 |
self._set_attributes(self.check_arrlist, [numpy.array([1,2,3]), numpy.array([4,5,6])]) |
| 57 |
self.data.listify() |
| 58 |
for attr in self.check_arrlist: |
| 59 |
for a in getattr(self.data, attr): |
| 60 |
self.assertIsInstance(a, list) |
| 61 |
|
| 62 |
def test_listify_arraydict(self): |
| 63 |
"""Does the method convert dicts of arrays as expected?""" |
| 64 |
self._set_attributes(self.check_arrdict, {1: numpy.array([1,2,3]), 2: numpy.array([4,5,6])}) |
| 65 |
self.data.listify() |
| 66 |
for attr in self.check_arrdict: |
| 67 |
for a in getattr(self.data, attr).values(): |
| 68 |
self.assertIsInstance(a, list) |
| 69 |
|
| 70 |
def test_arrayify_ndarray(self): |
| 71 |
"""Does the method convert lists as expected?""" |
| 72 |
self._set_attributes(self.check_array, [1,2,3]) |
| 73 |
self.data.arrayify() |
| 74 |
for attr in self.check_array: |
| 75 |
self.assertIsInstance(getattr(self.data, attr), numpy.ndarray) |
| 76 |
|
| 77 |
def test_arrayify_arraylist(self): |
| 78 |
"""Does the method convert lists of lists as expected?""" |
| 79 |
self._set_attributes(self.check_arrlist, [[1,2,3], [4,5,6]]) |
| 80 |
self.data.arrayify() |
| 81 |
for attr in self.check_arrlist: |
| 82 |
for a in getattr(self.data, attr): |
| 83 |
self.assertIsInstance(a, numpy.ndarray) |
| 84 |
|
| 85 |
def test_arrayify_arraydict(self): |
| 86 |
"""Does the method convert dicts of lists as expected?""" |
| 87 |
self._set_attributes(self.check_arrdict, {1: [1,2,3], 2: [4,5,6]}) |
| 88 |
self.data.arrayify() |
| 89 |
for attr in self.check_arrdict: |
| 90 |
for a in getattr(self.data, attr).values(): |
| 91 |
self.assertIsInstance(a, numpy.ndarray) |
| 92 |
|
| 93 |
|
| 94 |
if __name__ == "__main__": |
| 95 |
unittest.main() |