Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_unicode_ddl.py190 linesDownload Raw Back to suite
1# testing/suite/test_unicode_ddl.py
2# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of SQLAlchemy and is released under
6# the MIT License: https://www.opensource.org/licenses/mit-license.php
7# mypy: ignore-errors
8
9
10from sqlalchemy import desc
11from sqlalchemy import ForeignKey
12from sqlalchemy import Integer
13from sqlalchemy import MetaData
14from sqlalchemy import testing
15from sqlalchemy.testing import eq_
16from sqlalchemy.testing import fixtures
17from sqlalchemy.testing.schema import Column
18from sqlalchemy.testing.schema import Table
19
20
21class UnicodeSchemaTest(fixtures.TablesTest):
22    __requires__ = ("unicode_ddl",)
23    __backend__ = True
24
25    @classmethod
26    def define_tables(cls, metadata):
27        global t1, t2, t3
28
29        t1 = Table(
30            "unitable1",
31            metadata,
32            Column("méil", Integer, primary_key=True),
33            Column("\u6e2c\u8a66", Integer),
34            test_needs_fk=True,
35        )
36        t2 = Table(
37            "Unitéble2",
38            metadata,
39            Column("méil", Integer, primary_key=True, key="a"),
40            Column(
41                "\u6e2c\u8a66",
42                Integer,
43                ForeignKey("unitable1.méil"),
44                key="b",
45            ),
46            test_needs_fk=True,
47        )
48
49        # Few DBs support Unicode foreign keys
50        if testing.against("sqlite"):
51            t3 = Table(
52                "\u6e2c\u8a66",
53                metadata,
54                Column(
55                    "\u6e2c\u8a66_id",
56                    Integer,
57                    primary_key=True,
58                    autoincrement=False,
59                ),
60                Column(
61                    "unitable1_\u6e2c\u8a66",
62                    Integer,
63                    ForeignKey("unitable1.\u6e2c\u8a66"),
64                ),
65                Column("Unitéble2_b", Integer, ForeignKey("Unitéble2.b")),
66                Column(
67                    "\u6e2c\u8a66_self",
68                    Integer,
69                    ForeignKey("\u6e2c\u8a66.\u6e2c\u8a66_id"),
70                ),
71                test_needs_fk=True,
72            )
73        else:
74            t3 = Table(
75                "\u6e2c\u8a66",
76                metadata,
77                Column(
78                    "\u6e2c\u8a66_id",
79                    Integer,
80                    primary_key=True,
81                    autoincrement=False,
82                ),
83                Column("unitable1_\u6e2c\u8a66", Integer),
84                Column("Unitéble2_b", Integer),
85                Column("\u6e2c\u8a66_self", Integer),
86                test_needs_fk=True,
87            )
88
89    def test_insert(self, connection):
90        connection.execute(t1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
91        connection.execute(t2.insert(), {"a": 1, "b": 1})
92        connection.execute(
93            t3.insert(),
94            {
95                "\u6e2c\u8a66_id": 1,
96                "unitable1_\u6e2c\u8a66": 5,
97                "Unitéble2_b": 1,
98                "\u6e2c\u8a66_self": 1,
99            },
100        )
101
102        eq_(connection.execute(t1.select()).fetchall(), [(1, 5)])
103        eq_(connection.execute(t2.select()).fetchall(), [(1, 1)])
104        eq_(connection.execute(t3.select()).fetchall(), [(1, 5, 1, 1)])
105
106    def test_col_targeting(self, connection):
107        connection.execute(t1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
108        connection.execute(t2.insert(), {"a": 1, "b": 1})
109        connection.execute(
110            t3.insert(),
111            {
112                "\u6e2c\u8a66_id": 1,
113                "unitable1_\u6e2c\u8a66": 5,
114                "Unitéble2_b": 1,
115                "\u6e2c\u8a66_self": 1,
116            },
117        )
118
119        row = connection.execute(t1.select()).first()
120        eq_(row._mapping[t1.c["méil"]], 1)
121        eq_(row._mapping[t1.c["\u6e2c\u8a66"]], 5)
122
123        row = connection.execute(t2.select()).first()
124        eq_(row._mapping[t2.c["a"]], 1)
125        eq_(row._mapping[t2.c["b"]], 1)
126
127        row = connection.execute(t3.select()).first()
128        eq_(row._mapping[t3.c["\u6e2c\u8a66_id"]], 1)
129        eq_(row._mapping[t3.c["unitable1_\u6e2c\u8a66"]], 5)
130        eq_(row._mapping[t3.c["Unitéble2_b"]], 1)
131        eq_(row._mapping[t3.c["\u6e2c\u8a66_self"]], 1)
132
133    def test_reflect(self, connection):
134        connection.execute(t1.insert(), {"méil": 2, "\u6e2c\u8a66": 7})
135        connection.execute(t2.insert(), {"a": 2, "b": 2})
136        connection.execute(
137            t3.insert(),
138            {
139                "\u6e2c\u8a66_id": 2,
140                "unitable1_\u6e2c\u8a66": 7,
141                "Unitéble2_b": 2,
142                "\u6e2c\u8a66_self": 2,
143            },
144        )
145
146        meta = MetaData()
147        tt1 = Table(t1.name, meta, autoload_with=connection)
148        tt2 = Table(t2.name, meta, autoload_with=connection)
149        tt3 = Table(t3.name, meta, autoload_with=connection)
150
151        connection.execute(tt1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
152        connection.execute(tt2.insert(), {"méil": 1, "\u6e2c\u8a66": 1})
153        connection.execute(
154            tt3.insert(),
155            {
156                "\u6e2c\u8a66_id": 1,
157                "unitable1_\u6e2c\u8a66": 5,
158                "Unitéble2_b": 1,
159                "\u6e2c\u8a66_self": 1,
160            },
161        )
162
163        eq_(
164            connection.execute(tt1.select().order_by(desc("méil"))).fetchall(),
165            [(2, 7), (1, 5)],
166        )
167        eq_(
168            connection.execute(tt2.select().order_by(desc("méil"))).fetchall(),
169            [(2, 2), (1, 1)],
170        )
171        eq_(
172            connection.execute(
173                tt3.select().order_by(desc("\u6e2c\u8a66_id"))
174            ).fetchall(),
175            [(2, 7, 2, 2), (1, 5, 1, 1)],
176        )
177
178    def test_repr(self):
179        meta = MetaData()
180        t = Table("\u6e2c\u8a66", meta, Column("\u6e2c\u8a66_id", Integer))
181        eq_(
182            repr(t),
183            (
184                "Table('測試', MetaData(), "
185                "Column('測試_id', Integer(), "
186                "table=<測試>), "
187                "schema=None)"
188            ),
189        )
190 
codekingpro/portable-devtools · Team Ai