示例#1
0
    def test_one_to_one(self):

        class A(self.base_cls):
            name = Column(String)
            b = relationship('B', uselist=False)

        class B(self.base_cls):
            name = Column(String)
            a_id = Column(Integer, ForeignKey('a.id'))

        session = self.init()
        session.add_all([
            A(name='a1', b=B(name='b1')),
            A(name='a2'),
            B(name='b2'),
            A(name='a3', b=B(name='b3')),
        ])
        session.commit()

        query = (
            ConstructQuery({
                'a_name': A.name,
                'b_name': get_(if_(B.id, apply_(capitalize, [B.name]), '~'),
                               A.b),
            })
            .with_session(session.registry())
        )
        self.assertEqual(
            tuple(dict(obj) for obj in query.all()),
            ({'a_name': 'a1', 'b_name': 'B1'},
             {'a_name': 'a2', 'b_name': '~'},
             {'a_name': 'a3', 'b_name': 'B3'}),
        )
示例#2
0
    def test_if(self):
        add = lambda a, b: a + b
        c1 = self.a_cls.__table__.c.id
        c2 = self.a_cls.__table__.c.name
        c3 = self.b_cls.__table__.c.id
        c4 = self.b_cls.__table__.c.name

        if1 = if_(True, then_=1, else_=2)
        self.assertEqual(columns_set(if1), set())
        self.assertEqual(proceed(if1, {}), 1)

        if2 = if_(False, then_=1, else_=2)
        self.assertEqual(columns_set(if2), set())
        self.assertEqual(proceed(if2, {}), 2)

        if3 = if_(c1, then_=c2, else_=c3)
        self.assertEqual(columns_set(if3), {c1, c2, c3})
        self.assertEqual(proceed(if3, {c1: 0, c2: 3, c3: 6}), 6)
        self.assertEqual(proceed(if3, {c1: 1, c2: 3, c3: 6}), 3)

        if4 = if_(c1, then_=apply_(add, [c2, c3]), else_=apply_(add, [c3, c4]))
        self.assertEqual(columns_set(if4), {c1, c2, c3, c4})
        self.assertEqual(proceed(if4, {c1: 0, c2: 2, c3: 3, c4: 4}), 3 + 4)
        self.assertEqual(proceed(if4, {c1: 1, c2: 2, c3: 3, c4: 4}), 2 + 3)
示例#3
0
    def test_if(self):
        add = lambda a, b: a + b
        c1 = self.a_cls.__table__.c.id
        c2 = self.a_cls.__table__.c.name
        c3 = self.b_cls.__table__.c.id
        c4 = self.b_cls.__table__.c.name

        if1 = if_(True, then_=1, else_=2)
        self.assertEqual(columns_set(if1), set())
        self.assertEqual(proceed(if1, {}), 1)

        if2 = if_(False, then_=1, else_=2)
        self.assertEqual(columns_set(if2), set())
        self.assertEqual(proceed(if2, {}), 2)

        if3 = if_(c1, then_=c2, else_=c3)
        self.assertEqual(columns_set(if3), {c1, c2, c3})
        self.assertEqual(proceed(if3, {c1: 0, c2: 3, c3: 6}), 6)
        self.assertEqual(proceed(if3, {c1: 1, c2: 3, c3: 6}), 3)

        if4 = if_(c1, then_=apply_(add, [c2, c3]), else_=apply_(add, [c3, c4]))
        self.assertEqual(columns_set(if4), {c1, c2, c3, c4})
        self.assertEqual(proceed(if4, {c1: 0, c2: 2, c3: 3, c4: 4}), 3 + 4)
        self.assertEqual(proceed(if4, {c1: 1, c2: 2, c3: 3, c4: 4}), 2 + 3)
示例#4
0
    def test_one_to_one(self):
        class A(self.base_cls):
            name = Column(String)
            b = relationship('B', uselist=False)

        class B(self.base_cls):
            name = Column(String)
            a_id = Column(Integer, ForeignKey('a.id'))

        session = self.init()
        session.add_all([
            A(name='a1', b=B(name='b1')),
            A(name='a2'),
            B(name='b2'),
            A(name='a3', b=B(name='b3')),
        ])
        session.commit()

        query = (ConstructQuery({
            'a_name':
            A.name,
            'b_name':
            get_(if_(B.id, apply_(capitalize, [B.name]), '~'), A.b),
        }).with_session(session.registry()))
        self.assertEqual(
            tuple(dict(obj) for obj in query.all()),
            ({
                'a_name': 'a1',
                'b_name': 'B1'
            }, {
                'a_name': 'a2',
                'b_name': '~'
            }, {
                'a_name': 'a3',
                'b_name': 'B3'
            }),
        )
示例#5
0
    def test_performance(self):
        @define
        def test_func(a, b):
            def body(a_id, a_name, b_id, b_name):
                pass

            return body, [a.id, a.name, b.id, b.name]

        struct = Construct({
            'r1':
            if_(self.a_cls.id, then_=test_func.defn(self.a_cls, self.b_cls)),
            'r2':
            if_(self.a_cls.name, then_=test_func.defn(self.a_cls, self.b_cls)),
            'r3':
            if_(self.b_cls.id, then_=test_func.defn(self.a_cls, self.b_cls)),
            'r4':
            if_(self.b_cls.name, then_=test_func.defn(self.a_cls, self.b_cls)),
        })

        row = (self.session.query(*struct._columns).join(self.b_cls.a).first())

        # warm-up
        for _ in _range(5000):
            struct._from_row(row)

        profile1 = Profile()
        profile1.enable()

        for _ in _range(5000):
            struct._from_row(row)

        profile1.disable()
        out1 = StringIO()
        stats1 = Stats(profile1, stream=out1)
        stats1.strip_dirs()
        stats1.sort_stats('calls').print_stats(10)
        print(out1.getvalue().lstrip())
        out1.close()

        row = (self.session.query(
            self.a_cls.id.label('a_id'),
            self.a_cls.name.label('a_name'),
            self.b_cls.id.label('b_id'),
            self.b_cls.name.label('b_name'),
        ).join(self.b_cls.a).first())

        def make_object(row):
            Object(
                dict(
                    r1=(test_func.func(row.a_id, row.a_name, row.b_id,
                                       row.b_name) if row.a_id else None),
                    r2=(test_func.func(row.a_id, row.a_name, row.b_id,
                                       row.b_name) if row.a_name else None),
                    r3=(test_func.func(row.a_id, row.a_name, row.b_id,
                                       row.b_name) if row.b_id else None),
                    r4=(test_func.func(row.a_id, row.a_name, row.b_id,
                                       row.b_name) if row.b_name else None),
                ))

        # warm-up
        for _ in _range(5000):
            make_object(row)

        profile2 = Profile()
        profile2.enable()

        for _ in _range(5000):
            make_object(row)

        profile2.disable()
        out2 = StringIO()
        stats2 = Stats(profile2, stream=out2)
        stats2.strip_dirs()
        stats2.sort_stats('calls').print_stats(10)
        print(out2.getvalue().lstrip())
        out2.close()

        self.assertEqual(stats1.total_calls, stats2.total_calls)
示例#6
0
    def test_performance(self):

        @define
        def test_func(a, b):
            def body(a_id, a_name, b_id, b_name):
                pass
            return body, [a.id, a.name, b.id, b.name]

        struct = Construct({
            'r1': if_(self.a_cls.id,
                      then_=test_func.defn(self.a_cls, self.b_cls)),
            'r2': if_(self.a_cls.name,
                      then_=test_func.defn(self.a_cls, self.b_cls)),
            'r3': if_(self.b_cls.id,
                      then_=test_func.defn(self.a_cls, self.b_cls)),
            'r4': if_(self.b_cls.name,
                      then_=test_func.defn(self.a_cls, self.b_cls)),
        })

        row = (
            self.session.query(*struct._columns)
            .join(self.b_cls.a)
            .first()
        )

        # warm-up
        for _ in _range(5000):
            struct._from_row(row)

        profile1 = Profile()
        profile1.enable()

        for _ in _range(5000):
            struct._from_row(row)

        profile1.disable()
        out1 = StringIO()
        stats1 = Stats(profile1, stream=out1)
        stats1.strip_dirs()
        stats1.sort_stats('calls').print_stats(10)
        print(out1.getvalue().lstrip())
        out1.close()

        row = (
            self.session.query(
                self.a_cls.id.label('a_id'),
                self.a_cls.name.label('a_name'),
                self.b_cls.id.label('b_id'),
                self.b_cls.name.label('b_name'),
            )
            .join(self.b_cls.a)
            .first()
        )

        def make_object(row):
            Object(dict(
                r1=(
                    test_func.func(row.a_id, row.a_name, row.b_id, row.b_name)
                    if row.a_id else None
                ),
                r2=(
                    test_func.func(row.a_id, row.a_name, row.b_id, row.b_name)
                    if row.a_name else None
                ),
                r3=(
                    test_func.func(row.a_id, row.a_name, row.b_id, row.b_name)
                    if row.b_id else None
                ),
                r4=(
                    test_func.func(row.a_id, row.a_name, row.b_id, row.b_name)
                    if row.b_name else None
                ),
            ))

        # warm-up
        for _ in _range(5000):
            make_object(row)

        profile2 = Profile()
        profile2.enable()

        for _ in _range(5000):
            make_object(row)

        profile2.disable()
        out2 = StringIO()
        stats2 = Stats(profile2, stream=out2)
        stats2.strip_dirs()
        stats2.sort_stats('calls').print_stats(10)
        print(out2.getvalue().lstrip())
        out2.close()

        self.assertEqual(stats1.total_calls, stats2.total_calls)