File: test_atom.py

package info (click to toggle)
pyxrd 0.8.4-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,848 kB
  • sloc: python: 26,500; sh: 301; makefile: 128
file content (115 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python

# coding=UTF-8
# ex:ts=4:sw=4:et=on

# Copyright (c) 2013, Mathijs Dumon
# All rights reserved.
# Complete license can be found in the LICENSE file.

import unittest
from unittest.mock import Mock

from test.utils import create_object_attribute_test

from pyxrd.atoms.models import Atom

__all__ = [
    'TestAtom',
]

class TestAtom(unittest.TestCase):

    atom_type = None

    def setUp(self):
        self.atom = Atom()

    def tearDown(self):
        del self.atom

    def get_mocked_hierarchy(self):       
        oxygen = Mock()
        oxygen.name = "O1-"
        hydrogen = Mock()
        hydrogen.name = "H+"
                
        project = Mock()
        project.atom_types = [oxygen, hydrogen]
        phase = Mock()
        phase.attach_mock(project, 'project')
        component = Mock()
        component.attach_mock(phase, 'phase')
        
        return oxygen, hydrogen, component, phase, project

    def test_not_none(self):
        self.assertIsNotNone(self.atom)

    def test_data_object(self):
        self.assertIsNotNone(self.atom.data_object)

    test_name = create_object_attribute_test("atom", "name", "Test Name")
    test_pn = create_object_attribute_test("atom", "pn", 3)
    test_default_z = create_object_attribute_test("atom", "default_z", 5.3)
    test_stretch_values = create_object_attribute_test("atom", "stretch_values", True)

    def test_parent(self):
        parent_atom = Atom(name="Parent")
        self.atom.parent = parent_atom
        self.assertEqual(self.atom.parent, parent_atom)

    def test_set_atom_type(self):
        oxygen, hydrogen, component, _, _ = self.get_mocked_hierarchy()
        atom = Atom(
            parent = component,
            name = "O",
            atom_type = oxygen,
        )
        atom.atom_type = hydrogen
        self.assertEqual(atom.atom_type, hydrogen)

    def test_z_calculations(self):
        # Checks wether the atom can calculate stretched values:
        # 1. When everything is set up the way it should be:
        default_z = 9.0
        lattice_d = 5.4
        factor = 0.5
        
        parent = Mock()
        parent.configure_mock(**{
            'get_interlayer_stretch_factors.return_value': (lattice_d, factor)
        })
        
        atom = Atom(parent=parent)
        atom.stretch_values = True
        atom.default_z = default_z
        z = atom.z
        self.assertEqual(z, lattice_d + (default_z - lattice_d) * factor)
        # 2. When no component is set, but stretched is True: should not raise an error, but simple ignore the stretching
        atom.parent = None
        z = atom.z

    def test_structure_factors(self):
        import numpy as np
        rng = 2.0 * np.sin(np.arange(30)) / 0.154056
        res = self.atom.get_structure_factors(rng)
        self.assertIsNotNone(res)
        
    def test_loads_atom_type_by_name(self):
        atom_json_dict = {
            "uuid": "878341b04e9e11e2b238150ae229a525", 
            "name": "O", 
            "default_z": 0.66, 
            "pn": 6.0, 
            "atom_type_name": "O1-"
        }
        
        oxygen, _, component, _, _ = self.get_mocked_hierarchy()
        
        atom = Atom.from_json(parent = component, **atom_json_dict)
        atom.resolve_json_references()
        self.assertEqual(atom.atom_type, oxygen)
        

    pass # end of class