@mangar2/persist
Version:
Persists an object in a file
46 lines (37 loc) • 1.56 kB
JavaScript
/**
* ---------------------------------------------------------------------------------------------------
* This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE Version 3. It is furnished
* "as is", without any support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*
* File: test.js
*
* Author: Volker Böhm
* Copyright: Volker Böhm
* ---------------------------------------------------------------------------------------------------
*/
const Persist = require('@mangar2/persist')
const UnitTest = require('@mangar2/unittest')
const persist = new Persist({ keepFiles: 3 })
const unitTest = new UnitTest(false)
async function cleanFiles () {
await Persist.deleteOldFiles('.', 'test', 0)
}
(async () => {
let readJSON = persist.readData('.', 'test')
unitTest.assertEqual(readJSON, undefined, 'read not existing file')
const test1JSON = { hello1: 'world1' }
const test2JSON = { hello2: 'world2' }
await cleanFiles()
await persist.saveObjectToFile('.', 'test', test1JSON)
readJSON = persist.readData('.', 'test')
unitTest.assertDeepEqual(test1JSON, readJSON, 'simple read')
await cleanFiles()
await persist.saveObjectToFile('.', 'test', test1JSON)
await persist.saveObjectToFile('.', 'test', test2JSON)
readJSON = persist.readData('.', 'test')
unitTest.assertDeepEqual(test2JSON, readJSON, 'newest file')
unitTest.showResult()
await cleanFiles()
})()