File: test_chained_join.py

package info (click to toggle)
python-sqlalchemy-utils 0.30.12-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,056 kB
  • sloc: python: 10,350; makefile: 160
file content (107 lines) | stat: -rw-r--r-- 3,705 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from sqlalchemy_utils.relationships import chained_join
from tests import TestCase
from tests.mixins import (
    ThreeLevelDeepManyToMany,
    ThreeLevelDeepOneToMany,
    ThreeLevelDeepOneToOne
)


class TestChainedJoinFoDeepToManyToMany(ThreeLevelDeepManyToMany, TestCase):
    dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
    create_tables = False

    def test_simple_join(self):
        assert str(chained_join(self.Catalog.categories)) == (
            'catalog_category JOIN category ON '
            'category._id = catalog_category.category_id'
        )

    def test_two_relations(self):
        sql = chained_join(
            self.Catalog.categories,
            self.Category.sub_categories
        )
        assert str(sql) == (
            'catalog_category JOIN category ON category._id = '
            'catalog_category.category_id JOIN category_subcategory ON '
            'category._id = category_subcategory.category_id JOIN '
            'sub_category ON sub_category._id = '
            'category_subcategory.subcategory_id'
        )

    def test_three_relations(self):
        sql = chained_join(
            self.Catalog.categories,
            self.Category.sub_categories,
            self.SubCategory.products
        )
        assert str(sql) == (
            'catalog_category JOIN category ON category._id = '
            'catalog_category.category_id JOIN category_subcategory ON '
            'category._id = category_subcategory.category_id JOIN sub_category'
            ' ON sub_category._id = category_subcategory.subcategory_id JOIN '
            'subcategory_product ON sub_category._id = '
            'subcategory_product.subcategory_id JOIN product ON product._id ='
            ' subcategory_product.product_id'
        )


class TestChainedJoinForDeepOneToMany(ThreeLevelDeepOneToMany, TestCase):
    dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
    create_tables = False

    def test_simple_join(self):
        assert str(chained_join(self.Catalog.categories)) == 'category'

    def test_two_relations(self):
        sql = chained_join(
            self.Catalog.categories,
            self.Category.sub_categories
        )
        assert str(sql) == (
            'category JOIN sub_category ON category._id = '
            'sub_category._category_id'
        )

    def test_three_relations(self):
        sql = chained_join(
            self.Catalog.categories,
            self.Category.sub_categories,
            self.SubCategory.products
        )
        assert str(sql) == (
            'category JOIN sub_category ON category._id = '
            'sub_category._category_id JOIN product ON sub_category._id = '
            'product._sub_category_id'
        )


class TestChainedJoinForDeepOneToOne(ThreeLevelDeepOneToOne, TestCase):
    dns = 'postgres://postgres@localhost/sqlalchemy_utils_test'
    create_tables = False

    def test_simple_join(self):
        assert str(chained_join(self.Catalog.category)) == 'category'

    def test_two_relations(self):
        sql = chained_join(
            self.Catalog.category,
            self.Category.sub_category
        )
        assert str(sql) == (
            'category JOIN sub_category ON category._id = '
            'sub_category._category_id'
        )

    def test_three_relations(self):
        sql = chained_join(
            self.Catalog.category,
            self.Category.sub_category,
            self.SubCategory.product
        )
        assert str(sql) == (
            'category JOIN sub_category ON category._id = '
            'sub_category._category_id JOIN product ON sub_category._id = '
            'product._sub_category_id'
        )