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
|
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017, 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 the CJSON writer."""
import json
import os
import unittest
from math import sqrt
import cclib
__filedir__ = os.path.dirname(__file__)
__filepath__ = os.path.realpath(__filedir__)
__datadir__ = os.path.join(__filepath__, "..", "..")
class CJSONWriterTest(unittest.TestCase):
"""Unit tests for the CJSON writer."""
def test_init(self):
"""Does the class initialize correctly?"""
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
data = cclib.io.ccread(fpath)
cjson = cclib.io.cjsonwriter.CJSON(data)
# The object should keep the ccData instance passed to its constructor.
self.assertEqual(cjson.ccdata, data)
def test_cjson_generation(self):
"""Does the CJSON format get generated properly?"""
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/NH3.adfout")
data = cclib.io.ccread(fpath)
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
# The data available in the cjson and ccdata objects should be equal.
json_data = json.loads(cjson)
number_of_atoms = json_data['properties']['number of atoms']
self.assertEqual(number_of_atoms, data.natom)
dipole_moment = json_data['properties']['total dipole moment']
self.assertAlmostEqual(
dipole_moment,
sqrt(sum(data.moments[1] ** 2))
)
# Ensure the bond connectivity index starts from 0
bonds = json_data.get('bonds', None)
self.assertIsNotNone(bonds)
indices = bonds['connections']['index']
self.assertEqual(min(indices), 0)
self.assertTrue(max(indices) < number_of_atoms)
def test_zero_dipole_moment(self):
"""Does the CJSON writer handle zero dipole moment correctly?"""
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out")
data = cclib.io.ccopen(fpath).parse()
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
json_data = json.loads(cjson)
self.assertAlmostEqual(json_data["properties"]['total dipole moment'], 0.0)
def test_missing_dipole_moment(self):
"""Does the CJSON writer handle missing properties correctly?"""
fpath = os.path.join(__datadir__, "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out")
data = cclib.io.ccopen(fpath).parse()
del data.moments
cjson = cclib.io.cjsonwriter.CJSON(data).generate_repr()
json_data = json.loads(cjson)
self.assertFalse("total dipole moment" in json_data["properties"])
if __name__ == "__main__":
unittest.main()
|