예제 #1
0
 def test_basic_construct(self):
     struct = Construct({
         'a_id': self.a_cls.id,
         'a_name': self.a_cls.name,
     })
     self.assertEqual(set(struct._columns), {
         self.a_cls.__table__.c.id,
         self.a_cls.__table__.c.name,
     })
     result = {
         self.a_cls.__table__.c.id: 1,
         self.a_cls.__table__.c.name: 'a1',
     }
     row = [result[col] for col in struct._columns]
     s = struct._from_row(row)
     self.assertEqual(s.a_id, 1)
     self.assertEqual(s.a_name, 'a1')
예제 #2
0
 def test_nested_construct(self):
     struct = Construct({
         'a_id': apply_(operator.add, [self.a_cls.id, 5]),
         'a_name': apply_(operator.concat, [self.a_cls.name, '-test']),
     })
     self.assertEqual(set(struct._columns), {
         self.a_cls.__table__.c.id,
         self.a_cls.__table__.c.name,
     })
     result = {
         self.a_cls.__table__.c.id: 1,
         self.a_cls.__table__.c.name: 'a1',
     }
     row = [result[col] for col in struct._columns]
     s = struct._from_row(row)
     self.assertEqual(s.a_id, 1 + 5)
     self.assertEqual(s.a_name, 'a1' + '-test')
예제 #3
0
 def test_basic_construct(self):
     struct = Construct({
         'a_id': self.a_cls.id,
         'a_name': self.a_cls.name,
     })
     self.assertEqual(set(struct._columns), {
         self.a_cls.__table__.c.id,
         self.a_cls.__table__.c.name,
     })
     result = {
         self.a_cls.__table__.c.id: 1,
         self.a_cls.__table__.c.name: 'a1',
     }
     row = [result[col] for col in struct._columns]
     s = struct._from_row(row)
     self.assertEqual(s.a_id, 1)
     self.assertEqual(s.a_name, 'a1')
예제 #4
0
 def test_nested_construct(self):
     struct = Construct({
         'a_id':
         apply_(operator.add, [self.a_cls.id, 5]),
         'a_name':
         apply_(operator.concat, [self.a_cls.name, '-test']),
     })
     self.assertEqual(set(struct._columns), {
         self.a_cls.__table__.c.id,
         self.a_cls.__table__.c.name,
     })
     result = {
         self.a_cls.__table__.c.id: 1,
         self.a_cls.__table__.c.name: 'a1',
     }
     row = [result[col] for col in struct._columns]
     s = struct._from_row(row)
     self.assertEqual(s.a_id, 1 + 5)
     self.assertEqual(s.a_name, 'a1' + '-test')
예제 #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_scalar_construct(self):
     struct = Construct({'foo': 1, 'bar': '2'})
     s = struct._from_row([])
     self.assertEqual(s.foo, 1)
     self.assertEqual(s.bar, '2')
예제 #7
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)
예제 #8
0
 def test_scalar_construct(self):
     struct = Construct({'foo': 1, 'bar': '2'})
     s = struct._from_row([])
     self.assertEqual(s.foo, 1)
     self.assertEqual(s.bar, '2')