~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to test/sql/test_delete.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! coding:utf-8
 
2
 
 
3
from sqlalchemy import Column, Integer, String, Table, delete, select
 
4
from sqlalchemy.dialects import mysql
 
5
from sqlalchemy.testing import AssertsCompiledSQL, fixtures
 
6
 
 
7
 
 
8
class _DeleteTestBase(object):
 
9
    @classmethod
 
10
    def define_tables(cls, metadata):
 
11
        Table('mytable', metadata,
 
12
              Column('myid', Integer),
 
13
              Column('name', String(30)),
 
14
              Column('description', String(50)))
 
15
        Table('myothertable', metadata,
 
16
              Column('otherid', Integer),
 
17
              Column('othername', String(30)))
 
18
 
 
19
 
 
20
class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL):
 
21
    __dialect__ = 'default'
 
22
 
 
23
    def test_delete(self):
 
24
        table1 = self.tables.mytable
 
25
 
 
26
        self.assert_compile(
 
27
            delete(table1, table1.c.myid == 7),
 
28
            'DELETE FROM mytable WHERE mytable.myid = :myid_1')
 
29
 
 
30
        self.assert_compile(
 
31
            table1.delete().where(table1.c.myid == 7),
 
32
            'DELETE FROM mytable WHERE mytable.myid = :myid_1')
 
33
 
 
34
        self.assert_compile(
 
35
            table1.delete().
 
36
                where(table1.c.myid == 7).
 
37
                where(table1.c.name == 'somename'),
 
38
            'DELETE FROM mytable '
 
39
            'WHERE mytable.myid = :myid_1 '
 
40
            'AND mytable.name = :name_1')
 
41
 
 
42
    def test_prefix_with(self):
 
43
        table1 = self.tables.mytable
 
44
 
 
45
        stmt = table1.delete().\
 
46
            prefix_with('A', 'B', dialect='mysql').\
 
47
            prefix_with('C', 'D')
 
48
 
 
49
        self.assert_compile(stmt,
 
50
            'DELETE C D FROM mytable')
 
51
 
 
52
        self.assert_compile(stmt,
 
53
            'DELETE A B C D FROM mytable',
 
54
            dialect=mysql.dialect())
 
55
 
 
56
    def test_alias(self):
 
57
        table1 = self.tables.mytable
 
58
 
 
59
        talias1 = table1.alias('t1')
 
60
        stmt = delete(talias1).where(talias1.c.myid == 7)
 
61
 
 
62
        self.assert_compile(stmt,
 
63
            'DELETE FROM mytable AS t1 WHERE t1.myid = :myid_1')
 
64
 
 
65
    def test_correlated(self):
 
66
        table1, table2 = self.tables.mytable, self.tables.myothertable
 
67
 
 
68
        # test a non-correlated WHERE clause
 
69
        s = select([table2.c.othername], table2.c.otherid == 7)
 
70
        self.assert_compile(delete(table1, table1.c.name == s),
 
71
            'DELETE FROM mytable '
 
72
            'WHERE mytable.name = ('
 
73
                'SELECT myothertable.othername '
 
74
                'FROM myothertable '
 
75
                'WHERE myothertable.otherid = :otherid_1'
 
76
            ')')
 
77
 
 
78
        # test one that is actually correlated...
 
79
        s = select([table2.c.othername], table2.c.otherid == table1.c.myid)
 
80
        self.assert_compile(table1.delete(table1.c.name == s),
 
81
            'DELETE FROM mytable '
 
82
            'WHERE mytable.name = ('
 
83
                'SELECT myothertable.othername '
 
84
                'FROM myothertable '
 
85
                'WHERE myothertable.otherid = mytable.myid'
 
86
            ')')