Ejemplo n.º 1
0
    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])
Ejemplo n.º 2
0
    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])
Ejemplo n.º 3
0
    def test_preprocessor_boolean_with_default_value(self):

        Base = declarative_base()

        class Foo(Base):
            __tablename__ = 'foo'

            id = Column(Integer, primary_key=True)
            visible = Column(Boolean, default=False)
            archive = Column(Boolean)

        prc = RequestPreprocessing({'visible': True})
        visible = prc.check_type(Foo, 'visible')
        self.assertEqual(visible, True)

        prc = RequestPreprocessing({'visible': False})
        visible = prc.check_type(Foo, 'visible')
        self.assertEqual(visible, False)
Ejemplo n.º 4
0
    def test_preprocessor_boolean_with_default_value(self):

        Base = declarative_base()

        class Foo(Base):
            __tablename__ = 'foo'

            id = Column(Integer, primary_key=True)
            visible = Column(Boolean, default=False)
            archive = Column(Boolean)

        prc = RequestPreprocessing({'visible': True})
        visible = prc.check_type(Foo, 'visible')
        self.assertEqual(visible, True)

        prc = RequestPreprocessing({'visible': False})
        visible = prc.check_type(Foo, 'visible')
        self.assertEqual(visible, False)
Ejemplo n.º 5
0
    def test_polymorphycall_in_preprocessor(self):
        class Foo(User):
            __tablename__ = 'foo'
            __table_args__ = {'extend_existing': True}
            __mapper_args__ = {
                'polymorphic_identity': 'foo',
            }

            id = Column(Integer, ForeignKey('user.id'), primary_key=True)
            foo = Column(String)

        prc = RequestPreprocessing({'name': 'lalala'})
        name = prc.check_type(Foo, 'name')
        self.assertEqual(name, 'lalala')
Ejemplo n.º 6
0
    def test_polymorphycall_in_preprocessor(self):

        class Foo(User):
            __tablename__ = 'foo'
            __table_args__ = {'extend_existing': True}
            __mapper_args__ = {
                'polymorphic_identity': 'foo',
            }

            id = Column(Integer, ForeignKey('user.id'), primary_key=True)
            foo = Column(String)

        prc = RequestPreprocessing({'name': 'lalala'})
        name = prc.check_type(Foo, 'name')
        self.assertEqual(name, 'lalala')
Ejemplo n.º 7
0
 def test_default_in_preprocessor(self):
     prc = RequestPreprocessing({'name': ''})
     foo = prc.check_type(User, 'name')
     self.assertEqual(foo, None)
Ejemplo n.º 8
0
 def test_default_in_preprocessor(self):
     prc = RequestPreprocessing({'name': ''})
     foo = prc.check_type(User, 'name')
     self.assertEqual(foo, None)