Show More
@@ -1,45 +1,107 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2016 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import pickle |
|
21 | import pickle | |
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from rhodecode.lib.jsonalchemy import MutationDict, MutationList |
|
24 | from sqlalchemy import Column, String, create_engine | |
|
25 | from sqlalchemy.orm import sessionmaker | |||
|
26 | from sqlalchemy.ext.declarative import declarative_base | |||
|
27 | ||||
|
28 | from rhodecode.lib.jsonalchemy import ( | |||
|
29 | MutationDict, MutationList, MutationObj, JsonType) | |||
|
30 | ||||
|
31 | ||||
|
32 | @pytest.fixture | |||
|
33 | def engine(): | |||
|
34 | return create_engine('sqlite://') | |||
|
35 | ||||
|
36 | ||||
|
37 | @pytest.fixture | |||
|
38 | def session(engine): | |||
|
39 | return sessionmaker(bind=engine)() | |||
25 |
|
40 | |||
26 |
|
41 | |||
27 | def test_mutation_dict_is_picklable(): |
|
42 | def test_mutation_dict_is_picklable(): | |
28 | mutation_dict = MutationDict({'key1': 'value1', 'key2': 'value2'}) |
|
43 | mutation_dict = MutationDict({'key1': 'value1', 'key2': 'value2'}) | |
29 | dumped = pickle.dumps(mutation_dict) |
|
44 | dumped = pickle.dumps(mutation_dict) | |
30 | loaded = pickle.loads(dumped) |
|
45 | loaded = pickle.loads(dumped) | |
31 | assert loaded == mutation_dict |
|
46 | assert loaded == mutation_dict | |
32 |
|
47 | |||
|
48 | ||||
33 | def test_mutation_list_is_picklable(): |
|
49 | def test_mutation_list_is_picklable(): | |
34 | mutation_list = MutationList(['a', 'b', 'c']) |
|
50 | mutation_list = MutationList(['a', 'b', 'c']) | |
35 | dumped = pickle.dumps(mutation_list) |
|
51 | dumped = pickle.dumps(mutation_list) | |
36 | loaded = pickle.loads(dumped) |
|
52 | loaded = pickle.loads(dumped) | |
37 | assert loaded == mutation_list |
|
53 | assert loaded == mutation_list | |
38 |
|
54 | |||
|
55 | ||||
39 | def test_mutation_dict_with_lists_is_picklable(): |
|
56 | def test_mutation_dict_with_lists_is_picklable(): | |
40 | mutation_dict = MutationDict({ |
|
57 | mutation_dict = MutationDict({ | |
41 | 'key': MutationList(['values', MutationDict({'key': 'value'})]) |
|
58 | 'key': MutationList(['values', MutationDict({'key': 'value'})]) | |
42 | }) |
|
59 | }) | |
43 | dumped = pickle.dumps(mutation_dict) |
|
60 | dumped = pickle.dumps(mutation_dict) | |
44 | loaded = pickle.loads(dumped) |
|
61 | loaded = pickle.loads(dumped) | |
45 | assert loaded == mutation_dict |
|
62 | assert loaded == mutation_dict | |
|
63 | ||||
|
64 | ||||
|
65 | def test_mutation_types_with_nullable(engine, session): | |||
|
66 | # TODO: dan: ideally want to make this parametrized python => sql tests eg: | |||
|
67 | # (MutationObj, 5) => '5' | |||
|
68 | # (MutationObj, {'a': 5}) => '{"a": 5}' | |||
|
69 | # (MutationObj, None) => 'null' <- think about if None is 'null' or NULL | |||
|
70 | ||||
|
71 | Base = declarative_base() | |||
|
72 | ||||
|
73 | class DummyModel(Base): | |||
|
74 | __tablename__ = 'some_table' | |||
|
75 | name = Column(String, primary_key=True) | |||
|
76 | json_list = Column(MutationList.as_mutable(JsonType('list'))) | |||
|
77 | json_dict = Column(MutationDict.as_mutable(JsonType('dict'))) | |||
|
78 | json_obj = Column(MutationObj.as_mutable(JsonType())) | |||
|
79 | ||||
|
80 | Base.metadata.create_all(engine) | |||
|
81 | ||||
|
82 | obj_nulls = DummyModel(name='nulls') | |||
|
83 | obj_stuff = DummyModel( | |||
|
84 | name='stuff', json_list=[1,2,3], json_dict={'a': 5}, json_obj=9) | |||
|
85 | ||||
|
86 | session.add(obj_nulls) | |||
|
87 | session.add(obj_stuff) | |||
|
88 | session.commit() | |||
|
89 | session.expire_all() | |||
|
90 | ||||
|
91 | assert engine.execute( | |||
|
92 | "select * from some_table where name = 'nulls';").first() == ( | |||
|
93 | (u'nulls', None, None, None) | |||
|
94 | ) | |||
|
95 | ret_nulls = session.query(DummyModel).get('nulls') | |||
|
96 | assert ret_nulls.json_list == [] | |||
|
97 | assert ret_nulls.json_dict == {} | |||
|
98 | assert ret_nulls.json_obj is None | |||
|
99 | ||||
|
100 | assert engine.execute( | |||
|
101 | "select * from some_table where name = 'stuff';").first() == ( | |||
|
102 | (u'stuff', u'[1, 2, 3]', u'{"a": 5}', u'9') | |||
|
103 | ) | |||
|
104 | ret_stuff = session.query(DummyModel).get('stuff') | |||
|
105 | assert ret_stuff.json_list == [1, 2, 3] | |||
|
106 | assert ret_stuff.json_dict == {'a': 5} | |||
|
107 | assert ret_stuff.json_obj == 9 |
General Comments 0
You need to be logged in to leave comments.
Login now