def test_preprocessor_hstore(self): prc = RequestPreprocessing({}) foo = prc._check_hstore("{\"foo\":\"bar\"}") self.assertEqual(foo, {'foo': 'bar'}) foo = prc._check_hstore("{'foo':\"bar\"}") self.assertEqual(foo, {'foo': 'bar'}) with self.assertRaises(TypeError): prc._check_hstore("blablabla")
def test_postgres_json_and_hstore_fields(self): from sqlalchemy.dialects.postgresql import JSON, JSONB, HSTORE Base = declarative_base() class Foo(Base): __tablename__ = 'foo' id = Column(Integer, primary_key=True) json = Column(JSON) jsonb = Column(JSONB) hstore = Column(HSTORE) prc = RequestPreprocessing({ 'json': '{"a": 1, "c": 3, "b": {"foo": true}}', 'jsonb': '{"a": 1, "c": 3, "b": {"foo": true}}', 'hstore': '{"a": 1, "c": 3, "b": 2}', }) json = prc.check_type(Foo, 'json') jsonb = prc.check_type(Foo, 'jsonb') hstore = prc.check_type(Foo, 'hstore') self.assertEqual(json, {'a': 1, 'c': 3, 'b': {'foo': True}}) self.assertEqual(jsonb, {'a': 1, 'c': 3, 'b': {'foo': True}}) self.assertEqual(hstore, {'a': 1, 'c': 3, 'b': 2}) prc = RequestPreprocessing({ 'json': '', 'jsonb': '', 'hstore': '', }) json = prc.check_type(Foo, 'json') jsonb = prc.check_type(Foo, 'jsonb') hstore = prc.check_type(Foo, 'hstore') self.assertEqual(json, None) self.assertEqual(jsonb, None) self.assertEqual(hstore, None) with self.assertRaises(TypeError): prc._check_hstore("blablabla") prc = RequestPreprocessing({ 'json': [1, 2, 3], 'jsonb': [1, 2, 3], 'hstore': [1, 2, 3], }) json = prc.check_type(Foo, 'json') jsonb = prc.check_type(Foo, 'jsonb') hstore = prc.check_type(Foo, 'hstore') self.assertEqual(json, [1, 2, 3]) self.assertEqual(jsonb, [1, 2, 3]) self.assertEqual(hstore, [1, 2, 3])