##// END OF EJS Templates
jsonalchemy: add tests for using the json types in an sqlalchemy...
dan -
r871:b9e2864a default
parent child Browse files
Show More
@@ -21,7 +21,22 b''
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():
@@ -30,12 +45,14 b' def test_mutation_dict_is_picklable():'
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'})])
@@ -43,3 +60,48 b' def test_mutation_dict_with_lists_is_pic'
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