/
test_object.py
69 lines (55 loc) · 2.38 KB
/
test_object.py
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
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
from pyiron_base.interfaces.object import HasStorage, HasDatabase, PyironObject
from pyiron_base import DataContainer
from pyiron_base.database.generic import DatabaseAccess
from pyiron_base._tests import TestWithCleanProject
class TestHasStorage(TestWithCleanProject):
def setUp(self):
super().setUp()
self.has_storage = HasStorage()
def test_storage(self):
self.assertIsInstance(
self.has_storage.storage,
DataContainer,
msg=f"Expected storage to be a {DataContainer.__name__} but got {type(self.has_storage.storage).__name__}"
)
with self.assertRaises(AttributeError, msg="Expected the storage field to be read-only."):
self.has_storage.storage = 42
def test_hdf(self):
self.has_storage.storage.foo = 'foo'
hdf = self.project.create_hdf(path=self.project.path, job_name='hasstorage')
self.has_storage.to_hdf(hdf=hdf)
new_instance = HasStorage()
new_instance.from_hdf(hdf=hdf)
self.assertEqual(
self.has_storage.storage.foo,
new_instance.storage.foo,
msg="Loaded object does not have same data."
)
class TestHasDatabase(TestWithCleanProject):
def setUp(self):
super().setUp()
self.has_database = HasDatabase()
def test_database(self):
self.assertIsInstance(
self.has_database.database,
DatabaseAccess,
msg=f"Expected storage to be a {DatabaseAccess.__name__} "
f"but got {type(self.has_database.database).__name__}"
)
with self.assertRaises(AttributeError, msg="Expected the database field to be read-only."):
self.has_database.database = 42
class TestPyironObject(TestWithCleanProject):
def setUp(self):
super().setUp()
self.obj = PyironObject()
def test_has_storage(self):
self.assertIsInstance(self.obj.storage, DataContainer, msg=f"Expected storage to be {DataContainer.__name__}.")
def test_has_database(self):
self.assertIsInstance(
self.obj.database,
DatabaseAccess,
msg=f"Expected database to be {DatabaseAccess.__name__}."
)