2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the COPY statement.
2N/A#
2N/A# $Id: copy.test,v 1.17 2004/02/17 18:26:57 dougcurrie Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Create a file of data from which to copy.
2N/A#
2N/Aset f [open data1.txt w]
2N/Aputs $f "11\t22\t33"
2N/Aputs $f "22\t33\t11"
2N/Aclose $f
2N/Aset f [open data2.txt w]
2N/Aputs $f "11\t22\t33"
2N/Aputs $f "\\."
2N/Aputs $f "22\t33\t11"
2N/Aclose $f
2N/Aset f [open data3.txt w]
2N/Aputs $f "11\t22\t33\t44"
2N/Aputs $f "22\t33\t11"
2N/Aclose $f
2N/Aset f [open data4.txt w]
2N/Aputs $f "11 | 22 | 33"
2N/Aputs $f "22 | 33 | 11"
2N/Aclose $f
2N/Aset f [open data5.txt w]
2N/Aputs $f "11|22|33"
2N/Aputs $f "22|33|11"
2N/Aclose $f
2N/Aset f [open dataX.txt w]
2N/Afconfigure $f -translation binary
2N/Aputs -nonewline $f "11|22|33\r"
2N/Aputs -nonewline $f "22|33|44\r\n"
2N/Aputs -nonewline $f "33|44|55\n"
2N/Aputs -nonewline $f "44|55|66\r"
2N/Aputs -nonewline $f "55|66|77\r\n"
2N/Aputs -nonewline $f "66|77|88\n"
2N/Aclose $f
2N/A
2N/A# Try to COPY into a non-existant table.
2N/A#
2N/Ado_test copy-1.1 {
2N/A set v [catch {execsql {COPY test1 FROM 'data1.txt'}} msg]
2N/A lappend v $msg
2N/A} {1 {no such table: test1}}
2N/A
2N/A# Try to insert into sqlite_master
2N/A#
2N/Ado_test copy-1.2 {
2N/A set v [catch {execsql {COPY sqlite_master FROM 'data2.txt'}} msg]
2N/A lappend v $msg
2N/A} {1 {table sqlite_master may not be modified}}
2N/A
2N/A# Do some actual inserts
2N/A#
2N/Ado_test copy-1.3 {
2N/A execsql {CREATE TABLE test1(one int, two int, three int)}
2N/A execsql {COPY test1 FROM 'data1.txt'}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {11 22 33 22 33 11}
2N/A
2N/A# Make sure input terminates at \.
2N/A#
2N/Ado_test copy-1.4 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {COPY test1 FROM 'data2.txt'}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {11 22 33}
2N/A
2N/A# Test out the USING DELIMITERS clause
2N/A#
2N/Ado_test copy-1.5 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS ' | '}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {11 22 33 22 33 11}
2N/Ado_test copy-1.6 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {COPY test1 FROM 'data5.txt' USING DELIMITERS '|'}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {11 22 33 22 33 11}
2N/Ado_test copy-1.7 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {COPY test1 FROM 'data4.txt' USING DELIMITERS '|'}
2N/A execsql {SELECT * FROM test1 ORDER BY one}
2N/A} {{11 } { 22 } { 33} {22 } { 33 } { 11}}
2N/A
2N/A# Try copying into a table that has one or more indices.
2N/A#
2N/Ado_test copy-1.8 {
2N/A execsql {DELETE FROM test1}
2N/A execsql {CREATE INDEX index1 ON test1(one)}
2N/A execsql {CREATE INDEX index2 ON test1(two)}
2N/A execsql {CREATE INDEX index3 ON test1(three)}
2N/A execsql {COPY test1 from 'data1.txt'}
2N/A execsql {SELECT * FROM test1 WHERE one=11}
2N/A} {11 22 33}
2N/Ado_test copy-1.8b {
2N/A execsql {SELECT * FROM test1 WHERE one=22}
2N/A} {22 33 11}
2N/Ado_test copy-1.8c {
2N/A execsql {SELECT * FROM test1 WHERE two=22}
2N/A} {11 22 33}
2N/Ado_test copy-1.8d {
2N/A execsql {SELECT * FROM test1 WHERE three=11}
2N/A} {22 33 11}
2N/A
2N/A
2N/A# Try inserting really long data
2N/A#
2N/Aset x {}
2N/Afor {set i 0} {$i<100} {incr i} {
2N/A append x "($i)-abcdefghijklmnopqrstyvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-"
2N/A}
2N/Ado_test copy-2.1 {
2N/A execsql {CREATE TABLE test2(a int, x text)}
2N/A set f [open data21.txt w]
2N/A puts $f "123\t$x"
2N/A close $f
2N/A execsql {COPY test2 FROM 'data21.txt'}
2N/A execsql {SELECT x from test2}
2N/A} $x
2N/Afile delete -force data21.txt
2N/A
2N/A# Test the escape character mechanism
2N/A#
2N/Ado_test copy-3.1 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "hello\\\tworld\t1"
2N/A puts $fd "hello\tworld\\\t2"
2N/A close $fd
2N/A execsql {
2N/A CREATE TABLE t1(a text, b text);
2N/A COPY t1 FROM 'data6.txt';
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} {hello {world 2} {hello world} 1}
2N/Ado_test copy-3.2 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "1\thello\\\nworld"
2N/A puts $fd "2\thello world"
2N/A close $fd
2N/A execsql {
2N/A DELETE FROM t1;
2N/A COPY t1 FROM 'data6.txt';
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} {1 {hello
2N/Aworld} 2 {hello world}}
2N/Ado_test copy-3.3 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "1:hello\\b\\f\\n\\r\\t\\vworld"
2N/A puts $fd "2:hello world"
2N/A close $fd
2N/A execsql {
2N/A DELETE FROM t1;
2N/A COPY t1 FROM 'data6.txt' USING DELIMITERS ':';
2N/A SELECT * FROM t1 ORDER BY a;
2N/A }
2N/A} [list 1 "hello\b\f\n\r\t\vworld" 2 "hello world"]
2N/A
2N/A# Test the embedded NULL logic.
2N/A#
2N/Ado_test copy-4.1 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "1\t\\N"
2N/A puts $fd "\\N\thello world"
2N/A close $fd
2N/A execsql {
2N/A DELETE FROM t1;
2N/A COPY t1 FROM 'data6.txt';
2N/A SELECT * FROM t1 WHERE a IS NULL;
2N/A }
2N/A} {{} {hello world}}
2N/Ado_test copy-4.2 {
2N/A execsql {
2N/A SELECT * FROM t1 WHERE b IS NULL;
2N/A }
2N/A} {1 {}}
2N/A
2N/A# Test the conflict resolution logic for COPY
2N/A#
2N/Ado_test copy-5.1 {
2N/A execsql {
2N/A DROP TABLE t1;
2N/A CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE, c);
2N/A COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
2N/A SELECT * FROM t1;
2N/A }
2N/A} {11 22 33 22 33 11}
2N/Ado_test copy-5.2 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "33|22|44"
2N/A close $fd
2N/A catchsql {
2N/A COPY t1 FROM 'data6.txt' USING DELIMITERS '|';
2N/A SELECT * FROM t1;
2N/A }
2N/A} {1 {column b is not unique}}
2N/Ado_test copy-5.3 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "33|22|44"
2N/A close $fd
2N/A catchsql {
2N/A COPY OR IGNORE t1 FROM 'data6.txt' USING DELIMITERS '|';
2N/A SELECT * FROM t1;
2N/A }
2N/A} {0 {11 22 33 22 33 11}}
2N/Ado_test copy-5.4 {
2N/A set fd [open data6.txt w]
2N/A puts $fd "33|22|44"
2N/A close $fd
2N/A catchsql {
2N/A COPY OR REPLACE t1 FROM 'data6.txt' USING DELIMITERS '|';
2N/A SELECT * FROM t1;
2N/A }
2N/A} {0 {22 33 11 33 22 44}}
2N/A
2N/Ado_test copy-5.5 {
2N/A execsql {
2N/A DELETE FROM t1;
2N/A PRAGMA count_changes=on;
2N/A COPY t1 FROM 'data5.txt' USING DELIMITERS '|';
2N/A }
2N/A} {2}
2N/Ado_test copy-5.6 {
2N/A execsql {
2N/A COPY OR REPLACE t1 FROM 'data5.txt' USING DELIMITERS '|';
2N/A }
2N/A} {2}
2N/Ado_test copy-5.7 {
2N/A execsql {
2N/A COPY OR IGNORE t1 FROM 'data5.txt' USING DELIMITERS '|';
2N/A }
2N/A} {0}
2N/A
2N/Ado_test copy-6.1 {
2N/A execsql {
2N/A PRAGMA count_changes=off;
2N/A CREATE TABLE t2(a,b,c);
2N/A COPY t2 FROM 'dataX.txt' USING DELIMITERS '|';
2N/A SELECT * FROM t2;
2N/A }
2N/A} {11 22 33 22 33 44 33 44 55 44 55 66 55 66 77 66 77 88}
2N/A
2N/Aintegrity_check copy-7.1
2N/A
2N/A# Cleanup
2N/A#
2N/A#file delete -force data1.txt data2.txt data3.txt data4.txt data5.txt \
2N/A data6.txt dataX.txt
2N/A
2N/Afinish_test