Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more node hashing tests #591

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 58 additions & 31 deletions aiida/backends/tests/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
"""
import unittest

import numpy as np

from aiida.backends.testbase import AiidaTestCase
from aiida.common.exceptions import ModificationNotAllowed, UniquenessError
from aiida.common.links import LinkType
from aiida.orm.data import Data
from aiida.orm.data.array import ArrayData
from aiida.orm.node import Node
from aiida.orm.utils import load_node

Expand All @@ -24,31 +27,55 @@ class TestNodeHashing(AiidaTestCase):
"""
Tests the functionality of hashing a node
"""
def test_hashing_of_node_1(self):

n1 = Node()
n1._set_attr('a',1.0)
n1._set_attr('b',1.1)
n1._set_attr('c',1.2)
n1.store()


n2 = Node()
n2._set_attr('a',1.0)
n2._set_attr('b',1.1)
n2._set_attr('c',1.2)
n2.store(find_same=True)

self.assertEqual(n1.uuid, n2.uuid)
self.assertEqual(n1.folder.get_abs_path('.'), n2.folder.get_abs_path('.'))


n3 = Node()
n3._set_attr('a',2.0)
n3._set_attr('b',1.1)
n3._set_attr('c',1.2)
n3.store(find_same=True)
self.assertNotEquals(n2.uuid, n3.uuid)
@staticmethod
def create_simple_node(a, b=0, c=0):
n = Node()
n._set_attr('a', a)
n._set_attr('b', b)
n._set_attr('c', c)
return n

def test_simple_equal_nodes(self):
attributes = [
(1.0, 1.1, 1.2),
({'a': 'b', 'c': 'd'}, [1, 2, 3], {4, 1, 2})
]
for attr in attributes:
n1 = self.create_simple_node(*attr)
n2 = self.create_simple_node(*attr)
n1.store()
n2.store(find_same=True)
self.assertEqual(n1.uuid, n2.uuid)
self.assertEqual(n1.folder.get_abs_path('.'), n2.folder.get_abs_path('.'))

def test_simple_unequal_nodes(self):
attributes = [
[(1.0, 1.1, 1.2), (2.0, 1.1, 1.2)],
[(1e-14,), (2e-14,)],
]
for attr1, attr2 in attributes:
n1 = self.create_simple_node(*attr1)
n2 = self.create_simple_node(*attr2)
n1.store()
n2.store(find_same=True)
self.assertNotEquals(n1.uuid, n2.uuid)

def test_unequal_arrays(self):
arrays = [
(np.zeros(1001), np.zeros(1005)),
(np.array([1, 2, 3]), np.array([2, 3, 4]))
]
def create_arraydata(arr):
a = ArrayData()
a.set_array('a', arr)
return a

for arr1, arr2 in arrays:
a1 = create_arraydata(arr1)
a1.store()
a2 = create_arraydata(arr2)
a2.store(find_same=True)
self.assertNotEquals(a1.uuid, a2.uuid)

class TestDataNode(AiidaTestCase):
"""
Expand Down Expand Up @@ -796,7 +823,7 @@ def test_get_extras_with_default(self):
a = Node()
a.store()
a.set_extra('a', 'b')

self.assertEquals(a.get_extra('a'), 'b')
with self.assertRaises(AttributeError):
a.get_extra('c')
Expand Down Expand Up @@ -1025,7 +1052,7 @@ def test_basetype_as_attr(self):
self.assertIsInstance(p.get_attr('b'),basestring)
self.assertEqual(p.get_attr('c'), ['b', [1,2]])
self.assertIsInstance(p.get_attr('c'), (list, tuple))

# Check also before storing
n = Node()
n._set_attr('a', Str("sometext2"))
Expand All @@ -1034,15 +1061,15 @@ def test_basetype_as_attr(self):
self.assertIsInstance(n.get_attr('a'),basestring)
self.assertEqual(n.get_attr('b'), ['f', True, {'gg': None}])
self.assertIsInstance(n.get_attr('b'), (list, tuple))

# Check also deep in a dictionary/list
n = Node()
n._set_attr('a', {'b': [Str("sometext3")]})
self.assertEqual(n.get_attr('a')['b'][0], "sometext3")
self.assertIsInstance(n.get_attr('a')['b'][0],basestring)
self.assertIsInstance(n.get_attr('a')['b'][0],basestring)
n.store()
self.assertEqual(n.get_attr('a')['b'][0], "sometext3")
self.assertIsInstance(n.get_attr('a')['b'][0],basestring)
self.assertIsInstance(n.get_attr('a')['b'][0],basestring)

def test_basetype_as_extra(self):
"""
Expand Down Expand Up @@ -1077,7 +1104,7 @@ def test_basetype_as_extra(self):
n.store()
n.set_extra('a', {'b': [Str("sometext3")]})
self.assertEqual(n.get_extra('a')['b'][0], "sometext3")
self.assertIsInstance(n.get_extra('a')['b'][0],basestring)
self.assertIsInstance(n.get_extra('a')['b'][0],basestring)

def test_versioning_lowlevel(self):
"""
Expand Down