2024-01-22 22:08:55 +00:00
/ * \
2024-03-11 09:10:01 +00:00
title : $ : / p l u g i n s / t i d d l y w i k i / m u l t i w i k i s e r v e r / s t o r e / t e s t s - s q l - t i d d l e r - d a t a b a s e . j s
2024-01-22 22:08:55 +00:00
type : application / javascript
tags : [ [ $ : / t a g s / t e s t - s p e c ] ]
Tests the SQL tiddler database layer
\ * /
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
if ( $tw . node ) {
2024-03-11 09:01:43 +00:00
describe ( "SQL tiddler database with node-sqlite3-wasm" , function ( ) {
2024-02-25 17:58:53 +00:00
runSqlDatabaseTests ( "wasm" ) ;
} ) ;
2024-03-11 09:01:43 +00:00
describe ( "SQL tiddler database with better-sqlite3" , function ( ) {
2024-02-25 17:58:53 +00:00
runSqlDatabaseTests ( "better" ) ;
} ) ;
function runSqlDatabaseTests ( engine ) {
2024-01-22 22:08:55 +00:00
// Create and initialise the tiddler store
2024-03-11 09:10:01 +00:00
var SqlTiddlerDatabase = require ( "$:/plugins/tiddlywiki/multiwikiserver/store/sql-tiddler-database.js" ) . SqlTiddlerDatabase ;
2024-01-22 22:08:55 +00:00
const sqlTiddlerDatabase = new SqlTiddlerDatabase ( {
2024-02-25 17:58:53 +00:00
engine : engine
2024-01-22 22:08:55 +00:00
} ) ;
sqlTiddlerDatabase . createTables ( ) ;
// Tear down
afterAll ( function ( ) {
// Close the database
sqlTiddlerDatabase . close ( ) ;
} ) ;
// Run tests
2024-02-25 17:58:53 +00:00
it ( "should save and retrieve tiddlers using engine: " + engine , function ( ) {
2024-01-28 17:11:53 +00:00
// Create bags and recipes
sqlTiddlerDatabase . createBag ( "bag-alpha" , "Bag alpha" ) ;
sqlTiddlerDatabase . createBag ( "bag-beta" , "Bag beta" ) ;
sqlTiddlerDatabase . createBag ( "bag-gamma" , "Bag gamma" ) ;
expect ( sqlTiddlerDatabase . listBags ( ) ) . toEqual ( [
2024-02-23 09:30:12 +00:00
{ bag _name : 'bag-alpha' , accesscontrol : '' , description : "Bag alpha" } ,
{ bag _name : 'bag-beta' , accesscontrol : '' , description : "Bag beta" } ,
{ bag _name : 'bag-gamma' , accesscontrol : '' , description : "Bag gamma" }
2024-01-28 17:11:53 +00:00
] ) ;
sqlTiddlerDatabase . createRecipe ( "recipe-rho" , [ "bag-alpha" , "bag-beta" ] , "Recipe rho" ) ;
sqlTiddlerDatabase . createRecipe ( "recipe-sigma" , [ "bag-alpha" , "bag-gamma" ] , "Recipe sigma" ) ;
sqlTiddlerDatabase . createRecipe ( "recipe-tau" , [ "bag-alpha" ] , "Recipe tau" ) ;
sqlTiddlerDatabase . createRecipe ( "recipe-upsilon" , [ "bag-alpha" , "bag-gamma" , "bag-beta" ] , "Recipe upsilon" ) ;
expect ( sqlTiddlerDatabase . listRecipes ( ) ) . toEqual ( [
{ recipe _name : 'recipe-rho' , bag _names : [ "bag-alpha" , "bag-beta" ] , description : "Recipe rho" } ,
{ recipe _name : 'recipe-sigma' , bag _names : [ "bag-alpha" , "bag-gamma" ] , description : "Recipe sigma" } ,
{ recipe _name : 'recipe-tau' , bag _names : [ "bag-alpha" ] , description : "Recipe tau" } ,
{ recipe _name : 'recipe-upsilon' , bag _names : [ "bag-alpha" , "bag-gamma" , "bag-beta" ] , description : "Recipe upsilon" }
] ) ;
expect ( sqlTiddlerDatabase . getRecipeBags ( "recipe-rho" ) ) . toEqual ( [ "bag-alpha" , "bag-beta" ] ) ;
expect ( sqlTiddlerDatabase . getRecipeBags ( "recipe-sigma" ) ) . toEqual ( [ "bag-alpha" , "bag-gamma" ] ) ;
expect ( sqlTiddlerDatabase . getRecipeBags ( "recipe-tau" ) ) . toEqual ( [ "bag-alpha" ] ) ;
expect ( sqlTiddlerDatabase . getRecipeBags ( "recipe-upsilon" ) ) . toEqual ( [ "bag-alpha" , "bag-gamma" , "bag-beta" ] ) ;
2024-01-22 22:08:55 +00:00
// Save tiddlers
sqlTiddlerDatabase . saveBagTiddler ( { title : "Another Tiddler" , text : "I'm in alpha" , tags : "one two three" } , "bag-alpha" ) ;
sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in alpha as well" , tags : "one two three" } , "bag-alpha" ) ;
sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in beta" , tags : "four five six" } , "bag-beta" ) ;
sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in gamma" , tags : "seven eight nine" } , "bag-gamma" ) ;
// Verify what we've got
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-rho" ) ) . toEqual ( [
{ title : 'Another Tiddler' , bag _name : 'bag-alpha' } ,
{ title : 'Hello There' , bag _name : 'bag-beta' }
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [
{ title : 'Another Tiddler' , bag _name : 'bag-alpha' } ,
{ title : 'Hello There' , bag _name : 'bag-gamma' }
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Hello There" , "recipe-rho" ) . tiddler ) . toEqual ( { title : "Hello There" , text : "I'm in beta" , tags : "four five six" } ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Missing Tiddler" , "recipe-rho" ) ) . toEqual ( null ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Another Tiddler" , "recipe-rho" ) . tiddler ) . toEqual ( { title : "Another Tiddler" , text : "I'm in alpha" , tags : "one two three" } ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Hello There" , "recipe-sigma" ) . tiddler ) . toEqual ( { title : "Hello There" , text : "I'm in gamma" , tags : "seven eight nine" } ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Another Tiddler" , "recipe-sigma" ) . tiddler ) . toEqual ( { title : "Another Tiddler" , text : "I'm in alpha" , tags : "one two three" } ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Hello There" , "recipe-upsilon" ) . tiddler ) . toEqual ( { title : "Hello There" , text : "I'm in beta" , tags : "four five six" } ) ;
// Delete a tiddlers to ensure the underlying tiddler in the recipe shows through
sqlTiddlerDatabase . deleteTiddler ( "Hello There" , "bag-beta" ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-rho" ) ) . toEqual ( [
{ title : 'Another Tiddler' , bag _name : 'bag-alpha' } ,
{ title : 'Hello There' , bag _name : 'bag-alpha' }
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [
{ title : 'Another Tiddler' , bag _name : 'bag-alpha' } ,
{ title : 'Hello There' , bag _name : 'bag-gamma' }
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Hello There" , "recipe-beta" ) ) . toEqual ( null ) ;
sqlTiddlerDatabase . deleteTiddler ( "Another Tiddler" , "bag-alpha" ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-rho" ) ) . toEqual ( [ { title : 'Hello There' , bag _name : 'bag-alpha' } ] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [ { title : 'Hello There' , bag _name : 'bag-gamma' } ] ) ;
// Save a recipe tiddler
expect ( sqlTiddlerDatabase . saveRecipeTiddler ( { title : "More" , text : "None" } , "recipe-rho" ) ) . toEqual ( { tiddler _id : 5 , bag _name : 'bag-beta' } ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "More" , "recipe-rho" ) . tiddler ) . toEqual ( { title : "More" , text : "None" } ) ;
} ) ;
2024-02-25 17:58:53 +00:00
}
2024-01-22 22:08:55 +00:00
}
} ) ( ) ;