CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
swarmvtab3.test236 linesDownload Raw Back to test
1# 2017-07-152#3# The author disclaims copyright to this source code.  In place of4# a legal notice, here is a blessing:5#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.9#10#***********************************************************************11# This file implements regression tests for SQLite library.  The12# focus of this file is the "swarmvtab" extension13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17set testprefix swarmvtab318do_not_use_codec19 20ifcapable !vtab {21  finish_test22  return23}24 25load_static_extension db unionvtab26 27set nFile $sqlite_open_file_count28 29do_execsql_test 1.0 {30  CREATE TEMP TABLE swarm(id, tbl, minval, maxval);31}32 33# Set up 100 databases with filenames "remote_test.dbN", where N is between34# 0 and 99.35do_test 1.1 {36  for {set i 0} {$i < 100} {incr i} {37    set file remote_test.db$i38    forcedelete $file39    forcedelete test.db$i40    sqlite3 rrr $file41    rrr eval {42      CREATE TABLE t1(a INTEGER PRIMARY KEY, b);43      INSERT INTO t1 VALUES($i, $i);44    }45    rrr close46    db eval {47      INSERT INTO swarm VALUES($i, 't1', $i, $i);48    }49    set ::dbcache(test.db$i) 050  }51} {}52 53proc missing_db {filename} {54  set remote "remote_$filename"55  forcedelete $filename56  file copy $remote $filename57}58db func missing_db missing_db59 60proc openclose_db {filename bClose} {61  if {$bClose} {62    incr ::dbcache($filename) -163  } else {64    incr ::dbcache($filename) 165  }66  if {$::dbcache($filename)==0} {67    forcedelete $filename68  }69}70db func openclose_db openclose_db71 72proc check_dbcache {} {73  set n 074  for {set i 0} {$i<100} {incr i} {75    set exists [file exists test.db$i]76    if {$exists!=($::dbcache(test.db$i)!=0)} {77      error "inconsistent ::dbcache and disk ($i) - $exists"78    }79    incr n $exists80  }81  return $n82}83 84foreach {tn nMaxOpen cvt} {85  1 5 {86    CREATE VIRTUAL TABLE temp.s USING swarmvtab(87        'SELECT :prefix || id, tbl, minval, minval FROM swarm',88        :prefix='test.db',89        missing=missing_db,90        openclose=openclose_db,91        maxopen=592    )93  }94 95  2 3 {96    CREATE VIRTUAL TABLE temp.s USING swarmvtab(97        'SELECT :prefix || id, tbl, minval, minval FROM swarm',98        :prefix='test.db',99        missing =       'missing_db',100        openclose=[openclose_db],101        maxopen = 3102    )103  }104 105  3 1 {106    CREATE VIRTUAL TABLE temp.s USING swarmvtab(107        'SELECT :prefix||''.''||:suffix||id, tbl, minval, minval FROM swarm',108        :prefix=test, :suffix=db,109        missing =       'missing_db',110        openclose=[openclose_db],111        maxopen = 1112    )113  }114 115} {116  execsql { DROP TABLE IF EXISTS s }117 118  do_execsql_test 1.$tn.1 $cvt119 120  do_execsql_test 1.$tn.2 {121    SELECT b FROM s WHERE a<10;122  } {0 1 2 3 4 5 6 7 8 9}123 124  do_test 1.$tn.3 { check_dbcache } $nMaxOpen125 126  do_execsql_test 1.$tn.4 {127    SELECT b FROM s WHERE (b%10)=0;128  } {0 10 20 30 40 50 60 70 80 90}129 130  do_test 1.$tn.5 { check_dbcache } $nMaxOpen131}132 133execsql { DROP TABLE IF EXISTS s }134for {set i 0} {$i < 100} {incr i} {135  forcedelete remote_test.db$i136}137 138#----------------------------------------------------------------------------139#140do_execsql_test 2.0 {141  DROP TABLE IF EXISTS swarm;142  CREATE TEMP TABLE swarm(file, tbl, minval, maxval, ctx);143}144 145catch { array unset ::dbcache }146 147# Set up 100 databases with filenames "remote_test.dbN", where N is a148# random integer between 0 and 1,000,000149# 0 and 99.150do_test 2.1 {151  catch { array unset ctx_used } 152  for {set i 0} {$i < 100} {incr i} {153    while 1 {154      set ctx [expr abs(int(rand() *1000000))]155      if {[info exists ctx_used($ctx)]==0} break156    }157    set ctx_used($ctx) 1158 159    set file test_remote.db$ctx160    forcedelete $file161    forcedelete test.db$i162    sqlite3 rrr $file163    rrr eval {164      CREATE TABLE t1(a INTEGER PRIMARY KEY, b);165      INSERT INTO t1 VALUES($i, $i);166    }167    rrr close168    db eval {169      INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file)170    }171    set ::dbcache(test.db$i) 0172  }173} {}174 175proc missing_db {filename ctx} {176  file copy $ctx $filename177}178db func missing_db missing_db179 180proc openclose_db {filename ctx bClose} {181  if {$bClose} {182    incr ::dbcache($filename) -1183  } else {184    incr ::dbcache($filename) 1185  }186  if {$::dbcache($filename)==0} {187    forcedelete $filename188  }189}190db func openclose_db openclose_db191 192proc check_dbcache {} {193  set n 0194  foreach k [array names ::dbcache] {195    set exists [file exists $k]196    if {$exists!=($::dbcache($k)!=0)} {197      error "inconsistent ::dbcache and disk ($k) - $exists"198    }199    incr n $exists200  }201  return $n202}203 204foreach {tn nMaxOpen cvt} {205  2 5 {206    CREATE VIRTUAL TABLE temp.s USING swarmvtab(207        'SELECT file, tbl, minval, minval, ctx FROM swarm',208        missing=missing_db,209        openclose=openclose_db,210        maxopen=5211    )212  }213} {214  execsql { DROP TABLE IF EXISTS s }215 216  do_execsql_test 3.$tn.1 $cvt217 218  do_execsql_test 3.$tn.2 {219    SELECT b FROM s WHERE a<10;220  } {0 1 2 3 4 5 6 7 8 9}221 222  do_test 3.$tn.3 { check_dbcache } $nMaxOpen223 224  do_execsql_test 3.$tn.4 {225    SELECT b FROM s WHERE (b%10)=0;226  } {0 10 20 30 40 50 60 70 80 90}227 228  do_test 3.$tn.5 { check_dbcache } $nMaxOpen229}230 231db close232forcedelete {*}[glob test.db*]233forcedelete {*}[glob test_remote.db*]234 235finish_test236