| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2017, 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 the CJSON writer.""" |
| 9 |
|
| 10 |
import json |
| 11 |
import os |
| 12 |
import unittest |
| 13 |
from math import sqrt |
| 14 |
|
| 15 |
import cclib |
| 16 |
|
| 17 |
__filedir__ = os.path.dirname(__file__) |
| 18 |
__filepath__ = os.path.realpath(__filedir__) |
| 19 |
__datadir__ = os.path.join(__filepath__, "..", "..") |
| 20 |
|
| 21 |
|
| 22 |
class CJSONWriterTest(unittest.TestCase): |
| 23 |
"""Unit tests for the CJSON writer.""" |
| 24 |
|
| 25 |
def test_init(self): |
| 26 |
"""Does the class initialize correctly?""" |
| 27 |
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout") |
| 28 |
data = cclib.io.ccread(fpath) |
| 29 |
cjson = cclib.io.cjsonwriter.CJSON(data) |
| 30 |
|
| 31 |
# The object should keep the ccData instance passed to its constructor. |
| 32 |
self.assertEqual(cjson.ccdata, data) |
| 33 |
|
| 34 |
def test_cjson_generation(self): |
| 35 |
"""Does the CJSON format get generated properly?""" |
| 36 |
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/NH3.adfout") |
| 37 |
data = cclib.io.ccread(fpath) |
| 38 |
|
| 39 |
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr() |
| 40 |
|
| 41 |
# The data available in the cjson and ccdata objects should be equal. |
| 42 |
json_data = json.loads(cjson) |
| 43 |
number_of_atoms = json_data['properties']['number of atoms'] |
| 44 |
self.assertEqual(number_of_atoms, data.natom) |
| 45 |
|
| 46 |
dipole_moment = json_data['properties']['total dipole moment'] |
| 47 |
self.assertAlmostEqual( |
| 48 |
dipole_moment, |
| 49 |
sqrt(sum(data.moments[1] ** 2)) |
| 50 |
) |
| 51 |
|
| 52 |
# Ensure the bond connectivity index starts from 0 |
| 53 |
bonds = json_data.get('bonds', None) |
| 54 |
self.assertIsNotNone(bonds) |
| 55 |
indices = bonds['connections']['index'] |
| 56 |
self.assertEqual(min(indices), 0) |
| 57 |
self.assertTrue(max(indices) < number_of_atoms) |
| 58 |
|
| 59 |
def test_zero_dipole_moment(self): |
| 60 |
"""Does the CJSON writer handle zero dipole moment correctly?""" |
| 61 |
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out") |
| 62 |
data = cclib.io.ccopen(fpath).parse() |
| 63 |
|
| 64 |
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr() |
| 65 |
|
| 66 |
json_data = json.loads(cjson) |
| 67 |
self.assertAlmostEqual(json_data["properties"]['total dipole moment'], 0.0) |
| 68 |
|
| 69 |
def test_missing_dipole_moment(self): |
| 70 |
"""Does the CJSON writer handle missing properties correctly?""" |
| 71 |
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out") |
| 72 |
data = cclib.io.ccopen(fpath).parse() |
| 73 |
del data.moments |
| 74 |
|
| 75 |
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr() |
| 76 |
|
| 77 |
json_data = json.loads(cjson) |
| 78 |
self.assertFalse("total dipole moment" in json_data["properties"]) |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
unittest.main() |