Debian Codegraph @

test/io/testcjsonreader.py

raw
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 reader."""
9
10 import os
11 import unittest
12 import tempfile
13
14 import numpy as np
15
16 import cclib
17
18
19 __filedir__ = os.path.dirname(__file__)
20 __filepath__ = os.path.realpath(__filedir__)
21 __datadir__ = os.path.join(__filepath__, "..", "..")
22
23
24 class CJSONReaderTest(unittest.TestCase):
25 """Unit tests for the CJSON reader."""
26
27 def test_cjson_read(self):
28 """File->ccData->CJSON->attribute_dict, the attributes within ccData and attribute_dict
29 should be the same."""
30 fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout")
31 data = cclib.io.ccread(fpath)
32 self.assertIsNotNone(data, "The logfileparser failed to parse the output file")
33
34 cjson_obj = cclib.io.cjsonwriter.CJSON(data, terse=True)
35 self.assertEqual(cjson_obj.ccdata, data, "The ccData instance within the CJSON class should be the same as the one generated by the logfileparsers")
36
37 # Generate the CJSON object to be written into a file.
38 cjson_data = cjson_obj.generate_repr()
39
40 with tempfile.NamedTemporaryFile(mode='w') as fp:
41 fp.write(cjson_data)
42 fp.flush()
43 cjson_reader = cclib.io.cjsonreader.CJSON(fp.name)
44 read_cjson_data = cjson_reader.parse()
45 self.assertIsNotNone(read_cjson_data, "The CJSON reader failed to read attributes")
46
47 # The attribute values read by the CJSON reader will be a subset of the
48 # total attributes stored by the logfileparser in the ccData object.
49 ccdata_dict = data.getattributes()
50
51 # Check if each 'key:value' pair read by the CJSON reader is equal to
52 # the corresponding 'key:value' pair present in the ccData object.
53 for key in read_cjson_data:
54 ccdata_value = ccdata_dict[key]
55 cjson_value = read_cjson_data[key]
56
57 # The values in the ccData object might be of numpy types whereas
58 # the values obtained by the CJSON reader are of the inbuilt python
59 # types. Conversion of numpy types into python types happens here:
60 if isinstance(ccdata_value, (np.ndarray, list)):
61 ccdata_value = np.asarray(ccdata_value).tolist()
62 if isinstance(ccdata_value, dict):
63 temp_dict = {}
64 for ccdata_key in ccdata_value:
65 dict_value = ccdata_value[ccdata_key]
66 if isinstance(dict_value, np.ndarray):
67 dict_value = np.asarray(dict_value).tolist()
68 temp_dict[ccdata_key] = dict_value
69 ccdata_value = temp_dict
70
71 # The 'moments' attribute present in the CJSON is a post processed
72 # value obtained from the moments key within ccData. Hence the
73 # values for moments will be always different. TODO: Create a
74 # naming convention for post processed attributes within the CJSON
75 # structure.
76 if key != 'moments':
77 self.assertEqual(ccdata_value, cjson_value)
78
79
80 if __name__ == "__main__":
81 unittest.main()