(use trace test (prefix sqlite3 sqlite3:))
(import dbfile)
(trace-call-sites #t)

(trace
 ;; dbfile:setup
 ;; dbfile:open-sqlite3-db
 ;; dbfile:init-subdb
 ;; dbfile:add-dbdat
 ;; db:initialize-main-db
 ;; dbfile:set-subdb
 ;; db:with-db
 ;; dbfile:get-subdb
 )

;; *************** dbfile.scm tests ****************


 (debug:print 0 *default-log-port* " tmp area: " (common:get-db-tmp-area))

(define tmpdir (common:get-db-tmp-area))
(test #f #t (dbr:dbstruct? (dbfile:setup #t *toppath* tmpdir)))
(test #f #t (dbr:dbstruct? (db:setup #t)))
(define dbstruct *dbstruct-dbs*)
(test #f #t (dbr:subdb? (dbfile:init-subdb dbstruct #f db:initialize-main-db))) ;; this opens the nfs main db

;; (test #f #t (dbr:dbdat? (dbfile:open-db *dbstruct-dbs* #f db:initialize-main-db))) ;; this opens the tmp db.
;; (define maindbdat (dbfile:open-db *dbstruct-dbs* #f db:initialize-main-db)) ;; this opens the tmp db.
;; (dbfile:add-dbdat dbstruct #f maindbdat)

;;(test #f #t (dbr:subdb? (dbfile:get-subdb dbstruct #f)))
;; (test #f #t (dbr:dbdat? (dbfile:get-dbdat dbstruct #f)))
;; (test #f #f (dbr:dbdat? (dbfile:get-dbdat dbstruct #f))) ;; stack empty so should fail.

;; (test #f #t (hash-table? (dbr:dbstruct-subdbs dbstruct)))
;; (test #f #t (stack? (dbr:subdb-dbstack (dbfile:get-subdb dbstruct #f))))
;; (test #f '("SYSTEM" "RELEASE") (db:get-keys *dbstruct-dbs*))


 (test #f #t (dbr:dbdat? (dbfile:open-db dbstruct 1 db:initialize-main-db)))
 (test #f #t (dbr:dbdat? (dbfile:open-db dbstruct 2 db:initialize-main-db)))
 (define rundbdat (dbfile:open-db dbstruct 1 db:initialize-main-db))
 (define rundbdat2 (dbfile:open-db dbstruct 1 db:initialize-main-db))
 (dbfile:add-dbdat dbstruct 1 rundbdat)
 (dbfile:add-dbdat dbstruct 2 rundbdat2)
;; (test #f #t (dbr:subdb? (dbfile:get-subdb dbstruct 1)))
;; (test #f #t (dbr:dbdat? (dbfile:get-dbdat dbstruct 1)))
;; (test #f #t (dbr:subdb? (dbfile:get-subdb dbstruct 2)))
;; (test #f #t (dbr:dbdat? (dbfile:get-dbdat dbstruct 2)))

;; (test #f #t (db:close-all dbstruct))


;; (test #f #t (> (dbfile:lazy-sqlite-db-modification-time ".db/main.db") 0))
;; (test #f #t (> (dbfile:lazy-sqlite-db-modification-time ".db/1.db") 0))
;; (test #f #t (> (dbfile:lazy-sqlite-db-modification-time ".db/2.db") 0))

;; (test #f #t (common:simple-file-lock "./db.lock"))
;; (test #f "./db.lock" (common:simple-file-release-lock "./db.lock"))



;; *************** db.scm tests ****************


;; (define thisdbdat (db:open-db dbstruct #f))
;; (test #f #t (dbr:dbdat? thisdbdat))

;; (test #f #t (dbr:dbdat? (db:get-db dbstruct #f)))
;; (test #f #t (dbr:dbdat? (db:get-db dbstruct 1)))
;; (test #f #t (dbr:dbdat? (db:get-db dbstruct 2)))

;; (dbfile:add-dbdat dbstruct #f maindbdat)
;; (define maindbdat (dbfile:get-dbdat dbstruct #f))
;; (dbfile:add-dbdat dbstruct #f maindbdat)

(define mtdbdat2 (dbr:subdb-mtdbdat (dbfile:get-subdb dbstruct #f)))

(define areapath  (dbr:dbstruct-areapath dbstruct))
(define mtdbpath  (dbfile:run-id->path areapath #f))
(define init-proc db:initialize-main-db)

(define mtdbdat   (dbfile:open-sqlite3-db mtdbpath init-proc))

(define maindb-handle (dbr:dbdat-dbh mtdbdat))
(define maindb-handle2 (dbr:dbdat-dbh mtdbdat2))
(debug:print 0 *default-log-port* "maindb handle: " maindb-handle)
(debug:print 0 *default-log-port* "maindb handle2: " maindb-handle2)

(sqlite3:execute maindb-handle "vacuum")
(sqlite3:execute maindb-handle2 "vacuum")

(define full-sel   (conc "SELECT * from runs"))

(sqlite3:for-each-row
	     (lambda (a . b)
               (debug:print 0 *default-log-port* "a: " a " b: " b)
             )
	     maindb-handle
	     full-sel)

(test #f #t (db:sync-touched dbstruct #f))
(test #f #t (db:sync-touched dbstruct 1))
(test #f #t (db:sync-touched dbstruct 2))

(test #f #t (db:safely-close-sqlite3-db (dbr:dbdat-dbh rundbdat) (dbr:dbdat-stmt-cache rundbdat)))
(test #f #t (db:safely-close-sqlite3-db (dbr:dbdat-dbh rundbdat2) (dbr:dbdat-stmt-cache rundbdat2)))
(test #f #t (db:safely-close-sqlite3-db (dbr:dbdat-dbh mtdbdat) (dbr:dbdat-stmt-cache mtdbdat)))

