test_session.py 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright 2016 ZEROFAIL
#
# This file is part of Goblin.
#
# Goblin is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Goblin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Goblin.  If not, see <http://www.gnu.org/licenses/>.

18
19
"""Functional sessions tests"""

davebshow's avatar
davebshow committed
20
21
import pytest

davebshow's avatar
davebshow committed
22
from goblin import element
23
from goblin.traversal import bindprop
davebshow's avatar
davebshow committed
24

davebshow's avatar
davebshow committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

@pytest.mark.asyncio
async def test_session_close(session):
    assert not session.conn.closed
    await session.close()
    assert session.conn.closed


@pytest.mark.asyncio
async def test_session_ctxt_mngr(session):
    async with session:
        assert not session.conn.closed
    assert session.conn.closed


class TestCreationApi:

    @pytest.mark.asyncio
    async def test_create_vertex(self, session, person_class):
        async with session:
            jon = person_class()
            jon.name = 'jonathan'
            jon.age = 38
            leif = person_class()
49
            leif.name = 'leifur'
davebshow's avatar
davebshow committed
50
51
52
53
54
55
56
57
            leif.age = 28
            session.add(jon, leif)
            assert not hasattr(jon, 'id')
            assert not hasattr(leif, 'id')
            await session.flush()
            assert hasattr(jon, 'id')
            assert session.current[jon.id] is jon
            assert jon.name == 'jonathan'
58
            assert jon.age == 38
davebshow's avatar
davebshow committed
59
60
            assert hasattr(leif, 'id')
            assert session.current[leif.id] is leif
61
62
            assert leif.name == 'leifur'
            assert leif.age == 28
davebshow's avatar
davebshow committed
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

    @pytest.mark.asyncio
    async def test_create_edge(self, session, person_class, place_class,
                               lives_in_class):
        async with session:
            jon = person_class()
            jon.name = 'jonathan'
            jon.age = 38
            montreal = place_class()
            montreal.name = 'Montreal'
            lives_in = lives_in_class(jon, montreal)
            session.add(jon, montreal, lives_in)
            await session.flush()
            assert hasattr(lives_in, 'id')
            assert session.current[lives_in.id] is lives_in
            assert lives_in.source is jon
            assert lives_in.target is montreal
            assert lives_in.source.__label__ == 'person'
            assert lives_in.target.__label__ == 'place'

    @pytest.mark.asyncio
    async def test_create_edge_no_source(self, session, lives_in, person):
        async with session:
            lives_in.source = person
            with pytest.raises(Exception):
                await session.save(lives_in)

    @pytest.mark.asyncio
    async def test_create_edge_no_target(self, session, lives_in, place):
        async with session:
            lives_in.target = place
            with pytest.raises(Exception):
                await session.save(lives_in)

    @pytest.mark.asyncio
    async def test_create_edge_no_source_target(self, session, lives_in):
        async with session:
            with pytest.raises(Exception):
                await session.save(lives_in)

    @pytest.mark.asyncio
    async def test_get_vertex(self, session, person_class):
        async with session:
            jon = person_class()
            jon.name = 'jonathan'
            jon.age = 38
            await session.save(jon)
            jid = jon.id
            result = await session.get_vertex(jon)
            assert result.id == jid
            assert result is jon

    @pytest.mark.asyncio
    async def test_get_edge(self, session, person_class, place_class,
                            lives_in_class):
        async with session:
            jon = person_class()
            jon.name = 'jonathan'
            jon.age = 38
            montreal = place_class()
            montreal.name = 'Montreal'
            lives_in = lives_in_class(jon, montreal)
            session.add(jon, montreal, lives_in)
            await session.flush()
            lid = lives_in.id
            result = await session.get_edge(lives_in)
            assert result.id == lid
            assert result is lives_in

    @pytest.mark.asyncio
    async def test_get_vertex_doesnt_exist(self, session, person):
        async with session:
135
            person._id = 1000000000000000000000000000000000000000000000
davebshow's avatar
davebshow committed
136
137
138
139
140
141
142
143
144
145
146
            result = await session.get_vertex(person)
            assert not result

    @pytest.mark.asyncio
    async def test_get_edge_doesnt_exist(self, session, knows, person_class):
        async with session:
            jon = person_class()
            leif = person_class()
            works_with = knows
            works_with.source = jon
            works_with.target = leif
147
            works_with._id = 1000000000000000000000000000000000000000000000
davebshow's avatar
davebshow committed
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
            result = await session.get_edge(works_with)
            assert not result

    @pytest.mark.asyncio
    async def test_remove_vertex(self, session, person):
        async with session:
            person.name = 'dave'
            person.age = 35
            await session.save(person)
            result = await session.g.V(person.id).one_or_none()
            assert result is person
            rid = result.id
            await session.remove_vertex(person)
            result = await session.g.V(rid).one_or_none()
            assert not result

    @pytest.mark.asyncio
    async def test_remove_edge(self, session, person_class, place_class,
                               lives_in_class):
        async with session:
            jon = person_class()
            jon.name = 'jonathan'
            jon.age = 38
            montreal = place_class()
            montreal.name = 'Montreal'
            lives_in = lives_in_class(jon, montreal)
            session.add(jon, montreal, lives_in)
            await session.flush()
            result = await session.g.E(lives_in.id).one_or_none()
            assert result is lives_in
            rid = result.id
            await session.remove_edge(lives_in)
            result = await session.g.E(rid).one_or_none()
            assert not result

davebshow's avatar
davebshow committed
183
184
185
186
187
188
    @pytest.mark.asyncio
    async def test_update_vertex(self, session, person):
        async with session:
            person.name = 'dave'
            person.age = 35
            result = await session.save(person)
189
            assert result.name == 'dave'
davebshow's avatar
davebshow committed
190
191
192
193
194
195
196
            assert result.age == 35
            person.name = 'david'
            person.age = None
            result = await session.save(person)
            assert result is person
            assert result.name == 'david'
            assert not result.age
davebshow's avatar
davebshow committed
197

davebshow's avatar
davebshow committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    @pytest.mark.asyncio
    async def test_update_edge(self, session, person_class, knows):
        async with session:
            dave = person_class()
            leif = person_class()
            knows.source = dave
            knows.target = leif
            knows.notes = 'online'
            session.add(dave, leif)
            await session.flush()
            result = await session.save(knows)
            assert knows.notes == 'online'
            knows.notes = None
            result = await session.save(knows)
            assert result is knows
            assert not result.notes
davebshow's avatar
davebshow committed
214
215
216
217


class TestTraversalApi:

davebshow's avatar
davebshow committed
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
    @pytest.mark.asyncio
    async def test_traversal_source_generation(self, session, person_class,
                                               knows_class):
        async with session:
            traversal = session.traversal(person_class)
            assert repr(traversal) == 'g.V().hasLabel("person")'
            traversal = session.traversal(knows_class)
            assert repr(traversal) == 'g.E().hasLabel("knows")'


    @pytest.mark.asyncio
    async def test_all(self, session, person_class):
        async with session:
            dave = person_class()
            leif = person_class()
            jon = person_class()
            session.add(dave, leif, jon)
            await session.flush()
            resp = await session.traversal(person_class).all()
            results = []
            async for msg in resp:
                assert isinstance(msg, person_class)
                results.append(msg)
            assert len(results) > 2

    @pytest.mark.asyncio
    async def test_one_or_none_one(self, session, person_class):
        async with session:
            dave = person_class()
            leif = person_class()
            jon = person_class()
            session.add(dave, leif, jon)
            await session.flush()
            resp = await session.traversal(person_class).one_or_none()
            assert isinstance(resp, person_class)
davebshow's avatar
davebshow committed
253

254
255
256
257
258
259
260
261
262
263
    @pytest.mark.asyncio
    async def test_traversal_bindprop(self, session, person_class):
        async with session:
            itziri = person_class()
            itziri.name = 'itziri'
            result1 = await session.save(itziri)
            bound_name = bindprop(person_class, 'name', 'itziri', binding='v1')
            p1 = await session.traversal(person_class).has(
                *bound_name).one_or_none()

davebshow's avatar
davebshow committed
264
265
266
267
268
269
270
271
272
273
274
    @pytest.mark.asyncio
    async def test_one_or_none_none(self, session):
        async with session:
            none = await session.g.V().hasLabel(
                'a very unlikey label').one_or_none()
            assert not none

    @pytest.mark.asyncio
    async def test_vertex_deserialization(self, session, person_class):
        async with session:
            resp = await session.g.addV('person').property(
275
                person_class.name, 'leif').property('place_of_birth', 'detroit').one_or_none()
davebshow's avatar
davebshow committed
276
277
            assert isinstance(resp, person_class)
            assert resp.name == 'leif'
278
            assert resp.place_of_birth == 'detroit'
davebshow's avatar
davebshow committed
279

davebshow's avatar
davebshow committed
280
281
282
283
284
285
286
287
288
289
290
291
    @pytest.mark.asyncio
    async def test_edge_desialization(self, session, knows_class):
        async with session:
            p1 = await session.g.addV('person').one_or_none()
            p2 = await session.g.addV('person').one_or_none()
            e1 = await session.g.V(p1.id).addE('knows').to(
                session.g.V(p2.id)).property(
                    knows_class.notes, 'somehow').property(
                    'how_long', 1).one_or_none()
            assert isinstance(e1, knows_class)
            assert e1.notes == 'somehow'
            assert e1.how_long == 1
davebshow's avatar
davebshow committed
292

davebshow's avatar
davebshow committed
293
294
295
296
297
298
299
300
    @pytest.mark.asyncio
    async def test_unregistered_vertex_deserialization(self, session):
        async with session:
            dave = await session.g.addV(
                'unregistered').property('name', 'dave').one_or_none()
            assert isinstance(dave, element.GenericVertex)
            assert dave.name == 'dave'
            assert dave.__label__ == 'unregistered'
davebshow's avatar
davebshow committed
301

davebshow's avatar
davebshow committed
302
303
304
305
306
307
308
309
310
311
    @pytest.mark.asyncio
    async def test_unregistered_edge_desialization(self, session):
        async with session:
            p1 = await session.g.addV('person').one_or_none()
            p2 = await session.g.addV('person').one_or_none()
            e1 = await session.g.V(p1.id).addE('unregistered').to(
                session.g.V(p2.id)).property('how_long', 1).one_or_none()
            assert isinstance(e1, element.GenericEdge)
            assert e1.how_long == 1
            assert e1.__label__ == 'unregistered'
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

    @pytest.mark.asyncio
    async def test_property_deserialization(self, session):
        async with session:
            p1 = await session.g.addV('person').property(
                'name', 'leif').one_or_none()
            name = await session.g.V(p1.id).properties('name').one_or_none()
            assert name['value'] == 'leif'
            assert name['label'] == 'name'

    @pytest.mark.asyncio
    async def test_non_element_deserialization(self, session):
        async with session:
            p1 = await session.g.addV('person').property(
                'name', 'leif').one_or_none()
            one = await session.g.V(p1.id).count().one_or_none()
            assert one == 1