Fix unit tests

This commit is contained in:
Matteo Giantomassi 2016-08-01 18:00:40 +02:00
parent 5bbfc64d66
commit c54de183ea
5 changed files with 30 additions and 75 deletions

View File

@ -129,7 +129,7 @@ def expand_star_syntax(s):
assert tokens[1] != "*"
return " ".join(tokens)
#print(s)
#print(s, tokens)
l = []
while tokens:
c = tokens.pop(0)
@ -375,10 +375,13 @@ def parse_abinit_string(s):
#
# Step 3 is needed because we are gonna use python to evaluate the operators and
# in abinit `2*sqrt(0.75)` means `sqrt(0.75) sqrt(0.75)` and not math multiplication!
tokens = []
for t in " ".join(lines).split():
tokens.extend(expand_star_syntax(t).split())
tokens = eval_abinit_operators(tokens)
#tokens = []
#for t in " ".join(lines).split():
# tokens.extend(expand_star_syntax(t).split())
#tokens = eval_abinit_operators(tokens)
# The code above does not work everywhere e.g. typat 2 * 1
# I think this section should be performed afterwards when we have already constructed the dictionary
tokens = " ".join(lines).split()
# Get value of ndtset.
#try:

View File

@ -314,13 +314,26 @@ class AbinitInput(six.with_metaclass(abc.ABCMeta, AbstractInput, MSONable, Has_S
of the input objects. Note, indeed, that AbintInput is mutable and therefore
should not be used as keyword in dictionaries.
"""
# todo add the decorators, do we need to add them ?
s = str(sorted([(unicode(i[0]), i[1]) for i in self.as_dict()['abi_args']]))
# Use sha1 from hashlib because python builtin hash is not deterministic
# (hash is version- and machine-dependent)
import hashlib
sha1 = hashlib.sha1()
sha1.update(s)
# Add abi_args to sha1
# (not sure this is code is portable: roundoff errors and conversion to string)
# We could just compute the hash from the keys (hash equality does not necessarily imply eq!)
for key in sorted(self.keys()):
value = self[key]
if isinstance(value, np.ndarray): value = value.tolist()
sha1.update(unicode(key))
sha1.update(unicode(value))
sha1.update(str(self.comment))
# add pseudos (this is easy because we have md5)
sha1.update(str([p.md5 for p in self.pseudos]))
# add the decorators, do we need to add them ?
sha1.update(str([dec.__class__.__name__ for dec in self.decorators]))
return sha1.hexdigest()
@pmg_serialize
@ -330,7 +343,6 @@ class AbinitInput(six.with_metaclass(abc.ABCMeta, AbstractInput, MSONable, Has_S
abi_args = []
for key, value in self.items():
if isinstance(value, np.ndarray): value = value.tolist()
#vars[key] = value
abi_args.append((key, value))
return dict(structure=self.structure.as_dict(),

View File

@ -104,13 +104,12 @@ class FactoryTest(AbipyTest):
self.assertIsInstance(inputs[2][0], AbinitInput)
self.assertIsInstance(inputs[3][0], AbinitInput)
self.assertEqual(inputs[0][0].variable_checksum(), "6cf6121967a3cd08cc85efde3b50188df0b8067c")
self.assertEqual(inputs[1][0].variable_checksum(), "d1e2639f848204359e01a03d11e2dac146c7dbc6")
self.assertEqual(inputs[2][0].variable_checksum(), "fe1b410583611ce1ba799765f9182a9e6be6e822")
self.assertEqual(inputs[3][0].variable_checksum(), "d2ff935446e01d7af344e21c2fc7e0a9db201ce7")
self.assertEqual(inputs[0][0].variable_checksum(), "4b724717b4f39cf99e390e0e836c7c6df3738f0c")
self.assertEqual(inputs[1][0].variable_checksum(), "e79dbacccf019d59ab87f66599b89f808333064e")
self.assertEqual(inputs[2][0].variable_checksum(), "569ca521b0207f77fdbb9e4f6e1a7295273b7e5a")
self.assertEqual(inputs[3][0].variable_checksum(), "77f4e676b1532d517c562da4e7cc792b21c2b2eb")
#for input in [inputs[0][0], inputs[1][0], inputs[2][0], inputs[3][0]]:
for inp in [item for sublist in inputs for item in sublist]:
val = inp.abivalidate()
if val.retcode != 0:

View File

@ -1,61 +0,0 @@
#!/usr/bin/env python
# coding: utf-8
"""
This script tests the parser implemented in AbinitInputFile.
It tries to read and parse all the input files of the Abinit test suite."""
from __future__ import print_function, division, unicode_literals, absolute_import
import os
import numpy as np
from abipy import abilab
def is_abinit_input(path):
"""True if path is one of the input files used in the Test suite."""
#beg = "#%%<BEGIN TEST_INFO>"
#end = "#%%<END TEST_INFO>"
if not path.endswith(".in"): return False
with open(path, "rt") as fh:
for line in fh:
if "executable" in line and "abinit" in line: return True
return False
def test_abinit_input_parser(top):
inputs = []
nfiles, retcode = 0, 0
for dirpath, dirnames, filenames in os.walk(top):
for fname in filenames:
path = os.path.join(dirpath, fname)
if not is_abinit_input(path): continue
nfiles += 1
try:
inp = abilab.AbinitInputFile.from_file(path)
except Exception as exc:
retcode += 1
if "udtset and jdtset are not supported" not in str(exc):
print("[%s]: Exception:\n%s" % (path, exc))
print("failed: %d/%d [%.1f%%]" % (retcode, nfiles, 100 * retcode/nfiles))
return retcode
if __name__ == "__main__":
import sys
try:
top = sys.argv[1]
except IndexError:
print("Usage: test_abinit_parser.py directory_with_input_files")
sys.exit(1)
# loglevel is bound to the string value obtained from the command line argument.
# Convert to upper case to allow the user to specify --loglevel=DEBUG or --loglevel=debug
import logging
numeric_level = getattr(logging, "DEBUG", None)
#if not isinstance(numeric_level, int):
# raise ValueError('Invalid log level: %s' % options.loglevel)
logging.basicConfig(level=numeric_level)
sys.exit(test_abinit_input_parser(top))

View File

@ -4,6 +4,8 @@ This script remove all pyc files and all __pycache__ directory.
"""
from __future__ import print_function, division, unicode_literals
import sys
import os
import shutil