Start adding tests, using testament.

FossilOrigin-Name: 6f368f0d303c65000c74f346b7bc39ffca964aff7767c60be2384739e5dc4d72
This commit is contained in:
mahlon 2025-03-18 02:21:06 +00:00
parent 89e879ca68
commit 1ed442a68a
16 changed files with 200 additions and 44 deletions

View file

@ -13,6 +13,7 @@
# See: https://developer.imdb.com/non-commercial-datasets/
import
std/dirs,
std/os,
std/sequtils,
std/strformat,
@ -53,52 +54,56 @@ for file in FILES:
var line = ""
while tsv_stream.readLine( line ):
c += 1
if c mod 1000 == 0: stderr.write( &"Parsing {file}... {c}\r" )
try:
c += 1
if c mod 1000 == 0: stderr.write( &"Parsing {file}... {c}\r" )
var row = line.split( '\t' )
case file
var row = line.split( '\t' )
case file
# nconst primaryName birthYear deathYear primaryProfession knownForTitles
of "name.basics":
row = row[0..3]
row[0] = $row[0].replace( "nm" ).parseInt()
# nconst primaryName birthYear deathYear primaryProfession knownForTitles
of "name.basics":
row = row[0..3]
row[0] = $row[0].replace( "nm" ).parseInt()
# tconst titleType primaryTitle originalTitle isAdult startYear endYear runtimeMinutes genres
of "title.basics":
if row[1] != "movie": continue
row.delete( 1 )
for i in 0..1: row.delete( 2 )
row.delete( 3 )
discard row.pop()
row[0] = $row[0].replace( "tt" ).parseInt()
# tconst titleType primaryTitle originalTitle isAdult startYear endYear runtimeMinutes genres
of "title.basics":
if row[1] != "movie": continue
row.delete( 1 )
for i in 0..1: row.delete( 2 )
row.delete( 3 )
discard row.pop()
row[0] = $row[0].replace( "tt" ).parseInt()
# tconst ordering nconst category job characters
of "title.principals":
if row[3] != "actor" and row[3] != "actress": continue
row.delete( 1 )
row = row[0..1]
row[0] = $row[0].replace( "tt" ).parseInt()
row[1] = $row[1].replace( "nm" ).parseInt()
row = @[ row[1], row[0] ]
# tconst ordering nconst category job characters
of "title.principals":
if row[3] != "actor" and row[3] != "actress": continue
row.delete( 1 )
row = row[0..1]
row[0] = $row[0].replace( "tt" ).parseInt()
row[1] = $row[1].replace( "nm" ).parseInt()
row = @[ row[1], row[0] ]
if file.contains( ".basics" ):
row.applyIt(
# empty value / null
if it == "\\N": ""
if file.contains( ".basics" ):
row.applyIt(
# empty value / null
if it == "\\N": ""
# RFC 4180 escapes
elif it.contains( "\"" ) or it.contains( ',' ):
var value = it
value = value.replace( "\"", "\"\"" )
value = value.replace( ",", "" )
"\"" & value & "\""
# RFC 4180 escapes
elif it.contains( "\"" ) or it.contains( ',' ):
var value = it
value = value.replace( "\"", "\"\"" )
value = value.replace( ",", "" )
"\"" & value & "\""
else: it
)
else: it
)
csv_file.write( row.join(","), "\n" )
csv_file.write( row.join(","), "\n" )
except ValueError:
continue
tsv_stream.close()
csv_file.close()
@ -109,10 +114,9 @@ for file in FILES:
# Ok, now import into a fresh kuzu database.
#
var db = newKuzuDatabase( DB )
var conn = db.connect()
if not DB.fileExists:
if not DB.dirExists:
var db = newKuzuDatabase( DB )
var conn = db.connect()
var duration = 0
for schema in @[

View file

@ -15,3 +15,10 @@ requires "nim ^= 2.0.0"
task makewrapper, "Generate the C wrapper using Futhark":
exec "nim c -d:futharkWrap --outdir=. src/kuzu.nim"
task test, "Run the test suite.":
exec "testament all"
exec "testament html"
task clean, "Remove all non-critical artifacts.":
exec "fossil clean --disable-undo --dotfiles --emptydirs -f -v"

View file

@ -23,12 +23,19 @@ include
"kuzu/connection.nim",
"kuzu/queries.nim"
proc kuzuVersionCompatible*(): bool =
## Returns true if the system installed Kuzu library
## is the expected version of this library wrapper.
result = KUZU_EXPECTED_LIBVERSION == KUZU_LIBVERSION
when isMainModule:
echo "Nim-Kuzu version: ", KUZU_VERSION,
". Expected library version: ", KUZU_EXPECTED_LIBVERSION, "."
echo "Installed Kuzu library version ", KUZU_LIBVERSION,
" (storage version ", KUZU_STORAGE_VERSION, ")"
if KUZU_EXPECTED_LIBVERSION == KUZU_LIBVERSION:
if kuzuVersionCompatible():
echo "Versions match!"
else:
echo "This library wraps a different version of Kuzu than what is installed."

View file

@ -3,8 +3,8 @@
const KUZU_VERSION* = "0.1.0"
const KUZU_EXPECTED_LIBVERSION* = "0.8.2"
let KUZU_LIBVERSION* = kuzu_get_version()
let KUZU_STORAGE_VERSION* = kuzu_get_storage_version()
let KUZU_LIBVERSION* = $kuzu_get_version()
let KUZU_STORAGE_VERSION* = kuzu_get_storage_version().int
let KUZU_DEFAULT_CONFIG* = kuzu_default_system_config()

View file

@ -0,0 +1,10 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
let db = newKuzuDatabase()
assert db.path == "(in-memory)"
assert typeOf( db.connect ) is KuzuConnection

View file

@ -0,0 +1,13 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
let db = newKuzuDatabase()
let conn = db.connect
# FIXME: This test should really perform some
# long running query in a thread, and cancel
# it from elsewhere.
conn.queryInterrupt()

View file

@ -0,0 +1,12 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
let db = newKuzuDatabase()
let conn = db.connect
# There is currently no getter for this, so
# we'll just have to assume a lack of
# exception/error means success.
conn.queryTimeout( 1000 )

View file

@ -0,0 +1,6 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
assert typeOf( KUZU_DEFAULT_CONFIG ) is kuzu_system_config

View file

@ -0,0 +1,7 @@
# vim: set et sta sw=4 ts=4 :
import re
import kuzu
assert KUZU_VERSION.contains( re"^\d+\.\d+\.\d+$" )

View file

@ -0,0 +1,7 @@
# vim: set et sta sw=4 ts=4 :
import re
import kuzu
assert KUZU_LIBVERSION.contains( re"^\d+\.\d+\.\d+$" )

View file

@ -0,0 +1,6 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
assert KUZU_STORAGE_VERSION >= 36

View file

@ -0,0 +1,7 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
var db = newKuzuDatabase()
assert db.path == "(in-memory)"

View file

@ -0,0 +1,17 @@
# vim: set et sta sw=4 ts=4 :
import
std/dirs,
std/paths
import kuzu
const DATABASE_PATH = Path( "tmp/testdb" )
DATABASE_PATH.removeDir()
var db = newKuzuDatabase( $DATABASE_PATH, kuzuConfig( auto_checkpoint=false ) )
assert db.path == "tmp/testdb"
assert db.config.auto_checkpoint == false
DATABASE_PATH.removeDir()

View file

@ -0,0 +1,19 @@
# vim: set et sta sw=4 ts=4 :
import
std/dirs,
std/paths
import kuzu
const DATABASE_PATH = Path( "tmp/testdb" )
DATABASE_PATH.removeDir()
var db = newKuzuDatabase( $DATABASE_PATH )
assert db.path == $DATABASE_PATH
assert db.config == kuzuConfig()
assert db.config.read_only == false
DATABASE_PATH.removeDir()

View file

@ -0,0 +1,20 @@
# vim: set et sta sw=4 ts=4 :
import kuzu
let db = newKuzuDatabase()
let conn = db.connect
var q = conn.query( "CREATE NODE TABLE Doop ( id SERIAL, thing STRING, PRIMARY KEY(id) )" )
assert typeOf( q ) is KuzuQueryResult
for thing in @[ "Camel", "Lampshade", "Delicious Cake" ]:
q = conn.query( "CREATE (d:Doop {thing: '" & thing & "'})" )
assert typeOf( q ) is KuzuQueryResult
q = conn.query( "MATCH (d:Doop) RETURN d.thing" )
assert q.num_columns == 1
assert q.num_tuples == 3
assert q.compile_time > 0
assert q.execution_time > 0

View file

@ -0,0 +1,14 @@
# vim: set et sta sw=4 ts=4 :
import
std/re
import kuzu
let db = newKuzuDatabase()
let conn = db.connect
try:
discard conn.query( "NOPE NOPE NOPE" )
except KuzuQueryException as err:
assert err.msg.contains( re"""Error running query:.*extraneous input 'NOPE'""" )