summaryrefslogtreecommitdiffstats
path: root/test/tkt-7bbfb7d442.test
blob: c020b515e258d1c2bfde710a890ef80a138c7766 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 2011 December 9
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests to verify that ticket [7bbfb7d442] has been
# fixed.  
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix tkt-7bbfb7d442

do_execsql_test 1.1 {
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 'one');
  INSERT INTO t1 VALUES(2, 'two');
  INSERT INTO t1 VALUES(3, 'three');

  CREATE TABLE t2(c, d);
  INSERT INTO t2 VALUES('one', 'I');
  INSERT INTO t2 VALUES('two', 'II');
  INSERT INTO t2 VALUES('three', 'III');

  CREATE TABLE t3(t3_a PRIMARY KEY, t3_d);
  CREATE TRIGGER t3t AFTER INSERT ON t3 WHEN new.t3_d IS NULL BEGIN
    UPDATE t3 SET t3_d = (
      SELECT d FROM 
        (SELECT * FROM t2 WHERE (new.t3_a%2)=(rowid%2) LIMIT 10),
        (SELECT * FROM t1 WHERE (new.t3_a%2)=(rowid%2) LIMIT 10)
      WHERE a = new.t3_a AND b = c
    ) WHERE t3_a = new.t3_a;
  END;
}

do_execsql_test 1.2 {
  INSERT INTO t3(t3_a) VALUES(1);
  INSERT INTO t3(t3_a) VALUES(2);
  INSERT INTO t3(t3_a) VALUES(3);
  SELECT * FROM t3;
} {1 I 2 II 3 III}

do_execsql_test 1.3 { DELETE FROM t3 }

ifcapable compound {
  do_execsql_test 1.4 {
    INSERT INTO t3(t3_a) SELECT 1 UNION SELECT 2 UNION SELECT 3;
    SELECT * FROM t3;
  } {1 I 2 II 3 III}
}



#-------------------------------------------------------------------------
# The following test case - 2.* - is from the original bug report as 
# posted to the mailing list.
#
do_execsql_test 2.1 {
  CREATE TABLE InventoryControl (
    InventoryControlId INTEGER PRIMARY KEY AUTOINCREMENT,
    SKU INTEGER NOT NULL,
    Variant INTEGER NOT NULL DEFAULT 0,
    ControlDate DATE NOT NULL,
    ControlState INTEGER NOT NULL DEFAULT -1,
    DeliveredQty VARCHAR(30)
  );
  
  CREATE TRIGGER TGR_InventoryControl_AfterInsert
  AFTER INSERT ON InventoryControl 
  FOR EACH ROW WHEN NEW.ControlState=-1 BEGIN 

  INSERT OR REPLACE INTO InventoryControl(
        InventoryControlId,SKU,Variant,ControlDate,ControlState,DeliveredQty
  ) SELECT
          T1.InventoryControlId AS InventoryControlId,
          T1.SKU AS SKU,
          T1.Variant AS Variant,
          T1.ControlDate AS ControlDate,
          1 AS ControlState,
          COALESCE(T2.DeliveredQty,0) AS DeliveredQty
      FROM (
          SELECT
              NEW.InventoryControlId AS InventoryControlId,
              II.SKU AS SKU,
              II.Variant AS Variant,
              COALESCE(LastClosedIC.ControlDate,NEW.ControlDate) AS ControlDate
          FROM
              InventoryItem II
          LEFT JOIN
              InventoryControl LastClosedIC
              ON  LastClosedIC.InventoryControlId IN ( SELECT 99999 )
          WHERE
              II.SKU=NEW.SKU AND
              II.Variant=NEW.Variant
      )   T1
      LEFT JOIN (
          SELECT
              TD.SKU AS SKU,
              TD.Variant AS Variant,
              10 AS DeliveredQty
          FROM
              TransactionDetail TD
          WHERE
              TD.SKU=NEW.SKU AND
              TD.Variant=NEW.Variant
      )   T2
      ON  T2.SKU=T1.SKU AND
          T2.Variant=T1.Variant;
  END;
  
  CREATE TABLE InventoryItem (
    SKU INTEGER NOT NULL,
    Variant INTEGER NOT NULL DEFAULT 0,
    DeptCode INTEGER NOT NULL,
    GroupCode INTEGER NOT NULL,
    ItemDescription VARCHAR(120) NOT NULL,
    PRIMARY KEY(SKU, Variant)
  );
  
  INSERT INTO InventoryItem VALUES(220,0,1,170,'Scoth Tampon Recurer');
  INSERT INTO InventoryItem VALUES(31,0,1,110,'Fromage');
  
  CREATE TABLE TransactionDetail (
    TransactionId INTEGER NOT NULL,
    SKU INTEGER NOT NULL,
    Variant INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY(TransactionId, SKU, Variant)
  );
  INSERT INTO TransactionDetail(TransactionId, SKU, Variant) VALUES(44, 31, 0);
  
  
  INSERT INTO InventoryControl(SKU, Variant, ControlDate) SELECT 
      II.SKU AS SKU, II.Variant AS Variant, '2011-08-30' AS ControlDate 
      FROM InventoryItem II;
}

do_execsql_test 2.2 {
  SELECT SKU, DeliveredQty FROM InventoryControl WHERE SKU=31
} {31 10}

do_execsql_test 2.3 {
  SELECT CASE WHEN DeliveredQty=10 THEN 'TEST PASSED!' ELSE 'TEST FAILED!' END 
  FROM InventoryControl WHERE SKU=31; 
} {{TEST PASSED!}}


finish_test