File: test_application_specific_information.py

package info (click to toggle)
python-pykmip 0.10.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,796 kB
  • sloc: python: 102,456; makefile: 33; sh: 12
file content (263 lines) | stat: -rw-r--r-- 8,739 bytes parent folder | download | duplicates (4)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Copyright (c) 2019 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import sqlalchemy
import testtools

from kmip.pie import objects
from kmip.pie import sqltypes


class TestApplicationSpecificInformation(testtools.TestCase):
    """
    Test suite for ApplicationSpecificInformation.
    """

    def setUp(self):
        super(TestApplicationSpecificInformation, self).setUp()

    def tearDown(self):
        super(TestApplicationSpecificInformation, self).tearDown()

    def test_init(self):
        """
        Test that an ApplicationSpecificInformation object can be instantiated.
        """
        app_specific_info = objects.ApplicationSpecificInformation()

        self.assertIsNone(app_specific_info.application_namespace)
        self.assertIsNone(app_specific_info.application_data)

    def test_invalid_application_namespace(self):
        """
        Test that a TypeError is raised when an invalid application namespace
        value is used to construct an ApplicationSpecificInformation attribute.
        """
        kwargs = {"application_namespace": []}
        self.assertRaisesRegex(
            TypeError,
            "The application namespace must be a string.",
            objects.ApplicationSpecificInformation,
            **kwargs
        )

        args = (
            objects.ApplicationSpecificInformation(),
            "application_namespace",
            []
        )
        self.assertRaisesRegex(
            TypeError,
            "The application namespace must be a string.",
            setattr,
            *args
        )

    def test_invalid_application_data(self):
        """
        Test that a TypeError is raised when an invalid application data value
        is used to construct an ApplicationSpecificInformation attribute.
        """
        kwargs = {"application_data": []}
        self.assertRaisesRegex(
            TypeError,
            "The application data must be a string.",
            objects.ApplicationSpecificInformation,
            **kwargs
        )

        args = (
            objects.ApplicationSpecificInformation(),
            "application_data",
            []
        )
        self.assertRaisesRegex(
            TypeError,
            "The application data must be a string.",
            setattr,
            *args
        )

    def test_repr(self):
        """
        Test that repr can be applied to an ApplicationSpecificInformation
        attribute.
        """
        app_specific_info = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )

        args = [
            "application_namespace='{}'".format("ssl"),
            "application_data='{}'".format("www.example.com")
        ]

        expected = "ApplicationSpecificInformation({})".format(", ".join(args))
        observed = repr(app_specific_info)

        self.assertEqual(expected, observed)

    def test_str(self):
        """
        Test that str can be applied to an ApplicationSpecificInformation
        attribute.
        """
        app_specific_info = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )

        expected = str(
            {
                "application_namespace": "ssl",
                "application_data": "www.example.com"
            }
        )
        observed = str(app_specific_info)

        self.assertEqual(expected, observed)

    def test_comparison_on_equal(self):
        """
        Test that the equality/inequality operators return True/False when
        comparing two ApplicationSpecificInformation attributes with the same
        data.
        """
        a = objects.ApplicationSpecificInformation()
        b = objects.ApplicationSpecificInformation()

        self.assertTrue(a == b)
        self.assertTrue(b == a)
        self.assertFalse(a != b)
        self.assertFalse(b != a)

        a = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )
        b = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )

        self.assertTrue(a == b)
        self.assertTrue(b == a)
        self.assertFalse(a != b)
        self.assertFalse(b != a)

    def test_comparison_on_different_application_namespaces(self):
        """
        Test that the equality/inequality operators return False/True when
        comparing two ApplicationSpecificInformation attributes with different
        application namespaces.
        """
        a = objects.ApplicationSpecificInformation(
            application_namespace="a"
        )
        b = objects.ApplicationSpecificInformation(
            application_namespace="b"
        )

        self.assertFalse(a == b)
        self.assertFalse(b == a)
        self.assertTrue(a != b)
        self.assertTrue(b != a)

    def test_comparison_on_different_application_data(self):
        """
        Test that the equality/inequality operators return False/True when
        comparing two ApplicationSpecificInformation attributes with different
        application data.
        """
        a = objects.ApplicationSpecificInformation(
            application_data="a"
        )
        b = objects.ApplicationSpecificInformation(
            application_data="b"
        )

        self.assertFalse(a == b)
        self.assertFalse(b == a)
        self.assertTrue(a != b)
        self.assertTrue(b != a)

    def test_comparison_on_type_mismatch(self):
        """
        Test that the equality/inequality operators return False/True when
        comparing an ApplicationSpecificInformation attribute to a non
        ApplicationSpecificInformation attribute.
        """
        a = objects.ApplicationSpecificInformation()
        b = "invalid"

        self.assertFalse(a == b)
        self.assertFalse(b == a)
        self.assertTrue(a != b)
        self.assertTrue(b != a)

    def test_save(self):
        """
        Test that an ApplicationSpecificInformation attribute can be saved
        using SQLAlchemy. This test will add an attribute instance to the
        database, verify that no exceptions are thrown, and check that its
        ID was set.
        """
        app_specific_info = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )

        engine = sqlalchemy.create_engine("sqlite:///:memory:", echo=True)
        sqltypes.Base.metadata.create_all(engine)

        session = sqlalchemy.orm.sessionmaker(bind=engine)()
        session.add(app_specific_info)
        session.commit()

        self.assertIsNotNone(app_specific_info.id)

    def test_get(self):
        """
        Test that an ApplicationSpecificInformation attribute can be saved
        and then retrieved using SQLAlchemy. This test adds the attribute to
        the database and then retrieves it by ID and verifies its values.
        """
        app_specific_info = objects.ApplicationSpecificInformation(
            application_namespace="ssl",
            application_data="www.example.com"
        )

        engine = sqlalchemy.create_engine("sqlite:///:memory:", echo=True)
        sqltypes.Base.metadata.create_all(engine)

        session = sqlalchemy.orm.sessionmaker(bind=engine)()
        session.add(app_specific_info)
        session.commit()

        # Grab the ID now before making a new session to avoid a Detached error
        # See http://sqlalche.me/e/bhk3 for more info.
        app_specific_info_id = app_specific_info.id

        session = sqlalchemy.orm.sessionmaker(bind=engine)()
        retrieved_info = session.query(
            objects.ApplicationSpecificInformation
        ).filter(
            objects.ApplicationSpecificInformation.id == app_specific_info_id
        ).one()
        session.commit()

        self.assertEqual("ssl", retrieved_info.application_namespace)
        self.assertEqual("www.example.com", retrieved_info.application_data)