File: test_chained_join.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (106 lines) | stat: -rw-r--r-- 3,609 bytes parent folder | download | duplicates (5)
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
import pytest

from sqlalchemy_utils.relationships import chained_join

from ..mixins import (
    ThreeLevelDeepManyToMany,
    ThreeLevelDeepOneToMany,
    ThreeLevelDeepOneToOne
)


@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinFoDeepToManyToMany(ThreeLevelDeepManyToMany):

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

    def test_two_relations(self, Catalog, Category):
        sql = chained_join(
            Catalog.categories,
            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, Catalog, Category, SubCategory):
        sql = chained_join(
            Catalog.categories,
            Category.sub_categories,
            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'
        )


@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinForDeepOneToMany(ThreeLevelDeepOneToMany):

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

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

    def test_three_relations(self, Catalog, Category, SubCategory):
        sql = chained_join(
            Catalog.categories,
            Category.sub_categories,
            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'
        )


@pytest.mark.usefixtures('postgresql_dsn')
class TestChainedJoinForDeepOneToOne(ThreeLevelDeepOneToOne):

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

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

    def test_three_relations(self, Catalog, Category, SubCategory):
        sql = chained_join(
            Catalog.category,
            Category.sub_category,
            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'
        )