standardize flag handling
[sqlcipher.git] / test / pg_common.tcl
blobdd16659a671012dfb952d2fe4156eafc35b2c74f
1 # 2018 May 19
3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 package require sqlite3
14 package require Pgtcl
16 set db [pg_connect -conninfo "dbname=postgres user=postgres password=postgres"]
17 sqlite3 sqlite ""
19 proc execsql {sql} {
21 set sql [string map {{WITHOUT ROWID} {}} $sql]
23 set lSql [list]
24 set frag ""
25 while {[string length $sql]>0} {
26 set i [string first ";" $sql]
27 if {$i>=0} {
28 append frag [string range $sql 0 $i]
29 set sql [string range $sql $i+1 end]
30 if {[sqlite complete $frag]} {
31 lappend lSql $frag
32 set frag ""
34 } else {
35 set frag $sql
36 set sql ""
39 if {$frag != ""} {
40 lappend lSql $frag
42 #puts $lSql
44 set ret ""
45 set nChar 0
46 foreach stmt $lSql {
47 set res [pg_exec $::db $stmt]
48 set err [pg_result $res -error]
49 if {$err!=""} { error $err }
51 for {set i 0} {$i < [pg_result $res -numTuples]} {incr i} {
52 set t [pg_result $res -getTuple $i]
53 set nNew [string length $t]
54 if {$nChar>0 && ($nChar+$nNew+3)>75} {
55 append ret "\n "
56 set nChar 0
57 } else {
58 if {$nChar>0} {
59 append ret " "
60 incr nChar 3
63 incr nChar $nNew
64 append ret $t
66 pg_result $res -clear
69 set ret
72 proc execsql_test {tn sql} {
73 set res [execsql $sql]
74 set sql [string map {string_agg group_concat} $sql]
75 # set sql [string map [list {NULLS FIRST} {}] $sql]
76 # set sql [string map [list {NULLS LAST} {}] $sql]
77 puts $::fd "do_execsql_test $tn {"
78 puts $::fd " [string trim $sql]"
79 puts $::fd "} {$res}"
80 puts $::fd ""
83 proc errorsql_test {tn sql} {
84 set rc [catch {execsql $sql} msg]
85 if {$rc==0} {
86 error "errorsql_test SQL did not cause an error!"
88 set msg [lindex [split [string trim $msg] "\n"] 0]
89 puts $::fd "# PG says $msg"
90 set sql [string map {string_agg group_concat} $sql]
91 puts $::fd "do_test $tn { catch { execsql {"
92 puts $::fd " [string trim $sql]"
93 puts $::fd "} } } 1"
94 puts $::fd ""
97 # Same as [execsql_test], except coerce all results to floating point values
98 # with two decimal points.
100 proc execsql_float_test {tn sql} {
101 set F "%.4f"
102 set T 0.0001
103 set res [execsql $sql]
104 set res2 [list]
105 foreach r $res {
106 if {$r != ""} { set r [format $F $r] }
107 lappend res2 $r
110 set sql [string trim $sql]
111 puts $::fd [subst -nocommands {
112 do_test $tn {
113 set myres {}
114 foreach r [db eval {$sql}] {
115 lappend myres [format $F [set r]]
117 set res2 {$res2}
118 set i 0
119 foreach r [set myres] r2 [set res2] {
120 if {[set r]<([set r2]-$T) || [set r]>([set r2]+$T)} {
121 error "list element [set i] does not match: got=[set r] expected=[set r2]"
123 incr i
125 set {} {}
126 } {}
130 proc start_test {name date} {
131 set dir [file dirname $::argv0]
132 set output [file join $dir $name.test]
133 set ::fd [open $output w]
134 puts $::fd [string trimleft "
135 # $date
137 # The author disclaims copyright to this source code. In place of
138 # a legal notice, here is a blessing:
140 # May you do good and not evil.
141 # May you find forgiveness for yourself and forgive others.
142 # May you share freely, never taking more than you give.
144 #***********************************************************************
145 # This file implements regression tests for SQLite library.
148 ####################################################
149 # DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED!
150 ####################################################
152 puts $::fd {set testdir [file dirname $argv0]}
153 puts $::fd {source $testdir/tester.tcl}
154 puts $::fd "set testprefix $name"
155 puts $::fd ""
158 proc -- {args} {
159 puts $::fd "# $args"
162 proc ========== {args} {
163 puts $::fd "#[string repeat = 74]"
164 puts $::fd ""
167 proc finish_test {} {
168 puts $::fd finish_test
169 close $::fd
172 proc ifcapable {arg} {
173 puts $::fd "ifcapable $arg { finish_test ; return }"