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
\ * /
2024-09-30 14:28:37 +00:00
if ( $tw . node ) {
2024-01-22 22:08:55 +00:00
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
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
2024-03-17 15:06:36 +00:00
expect ( sqlTiddlerDatabase . createBag ( "bag-alpha" , "Bag alpha" ) ) . toEqual ( 1 ) ;
expect ( sqlTiddlerDatabase . createBag ( "bag-beta" , "Bag beta" ) ) . toEqual ( 2 ) ;
expect ( sqlTiddlerDatabase . createBag ( "bag-gamma" , "Bag gamma" ) ) . toEqual ( 3 ) ;
2024-01-28 17:11:53 +00:00
expect ( sqlTiddlerDatabase . listBags ( ) ) . toEqual ( [
2024-03-17 15:06:36 +00:00
{ bag _name : 'bag-alpha' , bag _id : 1 , accesscontrol : '' , description : "Bag alpha" } ,
{ bag _name : 'bag-beta' , bag _id : 2 , accesscontrol : '' , description : "Bag beta" } ,
{ bag _name : 'bag-gamma' , bag _id : 3 , accesscontrol : '' , description : "Bag gamma" }
2024-01-28 17:11:53 +00:00
] ) ;
2024-03-17 15:06:36 +00:00
expect ( sqlTiddlerDatabase . createRecipe ( "recipe-rho" , [ "bag-alpha" , "bag-beta" ] , "Recipe rho" ) ) . toEqual ( 1 ) ;
expect ( sqlTiddlerDatabase . createRecipe ( "recipe-sigma" , [ "bag-alpha" , "bag-gamma" ] , "Recipe sigma" ) ) . toEqual ( 2 ) ;
expect ( sqlTiddlerDatabase . createRecipe ( "recipe-tau" , [ "bag-alpha" ] , "Recipe tau" ) ) . toEqual ( 3 ) ;
expect ( sqlTiddlerDatabase . createRecipe ( "recipe-upsilon" , [ "bag-alpha" , "bag-gamma" , "bag-beta" ] , "Recipe upsilon" ) ) . toEqual ( 4 ) ;
2024-01-28 17:11:53 +00:00
expect ( sqlTiddlerDatabase . listRecipes ( ) ) . toEqual ( [
2024-03-17 15:06:36 +00:00
{ recipe _name : 'recipe-rho' , recipe _id : 1 , bag _names : [ "bag-alpha" , "bag-beta" ] , description : "Recipe rho" } ,
{ recipe _name : 'recipe-sigma' , recipe _id : 2 , bag _names : [ "bag-alpha" , "bag-gamma" ] , description : "Recipe sigma" } ,
{ recipe _name : 'recipe-tau' , recipe _id : 3 , bag _names : [ "bag-alpha" ] , description : "Recipe tau" } ,
{ recipe _name : 'recipe-upsilon' , recipe _id : 4 , bag _names : [ "bag-alpha" , "bag-gamma" , "bag-beta" ] , description : "Recipe upsilon" }
2024-01-28 17:11:53 +00:00
] ) ;
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
2024-03-17 13:07:24 +00:00
expect ( sqlTiddlerDatabase . saveBagTiddler ( { title : "Another Tiddler" , text : "I'm in alpha" , tags : "one two three" } , "bag-alpha" ) ) . toEqual ( {
tiddler _id : 1
} ) ;
expect ( sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in alpha as well" , tags : "one two three" } , "bag-alpha" ) ) . toEqual ( {
tiddler _id : 2
} ) ;
expect ( sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in beta" , tags : "four five six" } , "bag-beta" ) ) . toEqual ( {
tiddler _id : 3
} ) ;
expect ( sqlTiddlerDatabase . saveBagTiddler ( { title : "Hello There" , text : "I'm in gamma" , tags : "seven eight nine" } , "bag-gamma" ) ) . toEqual ( {
tiddler _id : 4
} ) ;
2024-01-22 22:08:55 +00:00
// Verify what we've got
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-rho" ) ) . toEqual ( [
2024-03-23 09:27:54 +00:00
{ title : 'Another Tiddler' , tiddler _id : 1 , bag _name : 'bag-alpha' , is _deleted : 0 } ,
{ title : 'Hello There' , tiddler _id : 3 , bag _name : 'bag-beta' , is _deleted : 0 }
2024-01-22 22:08:55 +00:00
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [
2024-03-23 09:27:54 +00:00
{ title : 'Another Tiddler' , tiddler _id : 1 , bag _name : 'bag-alpha' , is _deleted : 0 } ,
{ title : 'Hello There' , tiddler _id : 4 , bag _name : 'bag-gamma' , is _deleted : 0 }
2024-01-22 22:08:55 +00:00
] ) ;
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 ( [
2024-03-23 09:27:54 +00:00
{ title : 'Another Tiddler' , tiddler _id : 1 , bag _name : 'bag-alpha' , is _deleted : 0 } ,
{ title : 'Hello There' , tiddler _id : 2 , bag _name : 'bag-alpha' , is _deleted : 0 }
2024-01-22 22:08:55 +00:00
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [
2024-03-23 09:27:54 +00:00
{ title : 'Another Tiddler' , tiddler _id : 1 , bag _name : 'bag-alpha' , is _deleted : 0 } ,
{ title : 'Hello There' , tiddler _id : 4 , bag _name : 'bag-gamma' , is _deleted : 0 }
2024-01-22 22:08:55 +00:00
] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "Hello There" , "recipe-beta" ) ) . toEqual ( null ) ;
sqlTiddlerDatabase . deleteTiddler ( "Another Tiddler" , "bag-alpha" ) ;
2024-03-23 09:27:54 +00:00
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-rho" ) ) . toEqual ( [ { title : 'Hello There' , tiddler _id : 2 , bag _name : 'bag-alpha' , is _deleted : 0 } ] ) ;
expect ( sqlTiddlerDatabase . getRecipeTiddlers ( "recipe-sigma" ) ) . toEqual ( [ { title : 'Hello There' , tiddler _id : 4 , bag _name : 'bag-gamma' , is _deleted : 0 } ] ) ;
2024-01-22 22:08:55 +00:00
// Save a recipe tiddler
2024-03-17 14:18:47 +00:00
expect ( sqlTiddlerDatabase . saveRecipeTiddler ( { title : "More" , text : "None" } , "recipe-rho" ) ) . toEqual ( { tiddler _id : 7 , bag _name : 'bag-beta' } ) ;
2024-01-22 22:08:55 +00:00
expect ( sqlTiddlerDatabase . getRecipeTiddler ( "More" , "recipe-rho" ) . tiddler ) . toEqual ( { title : "More" , text : "None" } ) ;
} ) ;
2024-10-30 17:59:44 +00:00
it ( "should manage users correctly" , function ( ) {
console . log ( "should manage users correctly" )
// Create users
const userId1 = sqlTiddlerDatabase . createUser ( "john_doe" , "john@example.com" , "pass123" ) ;
const userId2 = sqlTiddlerDatabase . createUser ( "jane_doe" , "jane@example.com" , "pass123" ) ;
// Retrieve users
const user1 = sqlTiddlerDatabase . getUser ( userId1 ) ;
expect ( user1 . user _id ) . toBe ( userId1 ) ;
expect ( user1 . username ) . toBe ( "john_doe" ) ;
expect ( user1 . email ) . toBe ( "john@example.com" ) ;
expect ( user1 . created _at ) . toMatch ( /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/ ) ; // Match timestamp format
expect ( user1 . last _login ) . toBeNull ( ) ;
// Update user
sqlTiddlerDatabase . updateUser ( userId1 , "john_updated" , "john_updated@example.com" ) ;
expect ( sqlTiddlerDatabase . getUser ( userId1 ) . username ) . toBe ( "john_updated" ) ;
expect ( sqlTiddlerDatabase . getUser ( userId1 ) . email ) . toBe ( "john_updated@example.com" ) ;
// List users
const users = sqlTiddlerDatabase . listUsers ( ) ;
expect ( users . length ) . toBe ( 2 ) ;
expect ( users [ 0 ] . username ) . toBe ( "jane_doe" ) ;
expect ( users [ 1 ] . username ) . toBe ( "john_updated" ) ;
// Delete user
sqlTiddlerDatabase . deleteUser ( userId2 ) ;
// expect(sqlTiddlerDatabase.getUser(userId2)).toBe(null || undefined);
} ) ;
it ( "should manage groups correctly" , function ( ) {
console . log ( "should manage groups correctly" )
// Create groups
const groupId1 = sqlTiddlerDatabase . createGroup ( "Editors" , "Can edit content" ) ;
const groupId2 = sqlTiddlerDatabase . createGroup ( "Viewers" , "Can view content" ) ;
// Retrieve groups
expect ( sqlTiddlerDatabase . getGroup ( groupId1 ) ) . toEqual ( {
group _id : groupId1 ,
group _name : "Editors" ,
description : "Can edit content"
} ) ;
// Update group
sqlTiddlerDatabase . updateGroup ( groupId1 , "Super Editors" , "Can edit all content" ) ;
expect ( sqlTiddlerDatabase . getGroup ( groupId1 ) . group _name ) . toBe ( "Super Editors" ) ;
expect ( sqlTiddlerDatabase . getGroup ( groupId1 ) . description ) . toBe ( "Can edit all content" ) ;
// List groups
const groups = sqlTiddlerDatabase . listGroups ( ) ;
expect ( groups . length ) . toBe ( 2 ) ;
expect ( groups [ 0 ] . group _name ) . toBe ( "Super Editors" ) ;
expect ( groups [ 1 ] . group _name ) . toBe ( "Viewers" ) ;
// Delete group
sqlTiddlerDatabase . deleteGroup ( groupId2 ) ;
// expect(sqlTiddlerDatabase.getGroup(groupId2)).toBe(null || undefined);
} ) ;
it ( "should manage roles correctly" , function ( ) {
console . log ( "should manage roles correctly" )
// Create roles
const roleId1 = sqlTiddlerDatabase . createRole ( "Admin" + Date . now ( ) , "Full access" ) ;
const roleId2 = sqlTiddlerDatabase . createRole ( "Editor" + Date . now ( ) , "Can edit content" ) ;
// Retrieve roles
expect ( sqlTiddlerDatabase . getRole ( roleId1 ) ) . toEqual ( {
role _id : roleId1 ,
role _name : jasmine . stringMatching ( /^Admin\d+$/ ) ,
description : "Full access"
} ) ;
// Update role
sqlTiddlerDatabase . updateRole ( roleId1 , "Super Admin" + Date . now ( ) , "God-like powers" ) ;
expect ( sqlTiddlerDatabase . getRole ( roleId1 ) . role _name ) . toMatch ( /^Super Admin\d+$/ ) ;
expect ( sqlTiddlerDatabase . getRole ( roleId1 ) . description ) . toBe ( "God-like powers" ) ;
// List roles
const roles = sqlTiddlerDatabase . listRoles ( ) ;
expect ( roles . length ) . toBeGreaterThan ( 0 ) ;
// expect(roles[0].role_name).toMatch(/^Editor\d+$/);
// expect(roles[1].role_name).toMatch(/^Super Admin\d+$/);
// Delete role
sqlTiddlerDatabase . deleteRole ( roleId2 ) ;
// expect(sqlTiddlerDatabase.getRole(roleId2)).toBeUndefined();
} ) ;
it ( "should manage permissions correctly" , function ( ) {
console . log ( "should manage permissions correctly" )
// Create permissions
const permissionId1 = sqlTiddlerDatabase . createPermission ( "read_tiddlers" + Date . now ( ) , "Can read tiddlers" ) ;
const permissionId2 = sqlTiddlerDatabase . createPermission ( "write_tiddlers" + Date . now ( ) , "Can write tiddlers" ) ;
// Retrieve permissions
expect ( sqlTiddlerDatabase . getPermission ( permissionId1 ) ) . toEqual ( {
permission _id : permissionId1 ,
permission _name : jasmine . stringMatching ( /^read_tiddlers\d+$/ ) ,
description : "Can read tiddlers"
} ) ;
// Update permission
sqlTiddlerDatabase . updatePermission ( permissionId1 , "read_all_tiddlers" + Date . now ( ) , "Can read all tiddlers" ) ;
expect ( sqlTiddlerDatabase . getPermission ( permissionId1 ) . permission _name ) . toMatch ( /^read_all_tiddlers\d+$/ ) ;
expect ( sqlTiddlerDatabase . getPermission ( permissionId1 ) . description ) . toBe ( "Can read all tiddlers" ) ;
// List permissions
const permissions = sqlTiddlerDatabase . listPermissions ( ) ;
expect ( permissions . length ) . toBeGreaterThan ( 0 ) ;
expect ( permissions [ 0 ] . permission _name ) . toMatch ( /^read_all_tiddlers\d+$/ ) ;
expect ( permissions [ 1 ] . permission _name ) . toMatch ( /^write_tiddlers\d+$/ ) ;
// Delete permission
sqlTiddlerDatabase . deletePermission ( permissionId2 ) ;
// expect(sqlTiddlerDatabase.getPermission(permissionId2)).toBeUndefined();
} ) ;
2024-02-25 17:58:53 +00:00
}
2024-01-22 22:08:55 +00:00
} ) ( ) ;
2024-09-30 14:28:37 +00:00
}