diff --git a/test/test.typescript.ts b/test/test.typescript.ts new file mode 100644 index 0000000..814dc3b --- /dev/null +++ b/test/test.typescript.ts @@ -0,0 +1,107 @@ +import { tap, expect } from '@pushrocks/tapbundle'; +import { Qenv } from '@pushrocks/qenv'; + +const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit/'); + +console.log(process.memoryUsage()); + +// the tested module +import * as smartdata from '../ts/index'; + +import * as mongoPlugin from 'mongodb-memory-server'; +import { smartunique } from '../ts/smartdata.plugins'; + +// ======================================= +// Connecting to the database server +// ======================================= + +let testDb: smartdata.SmartdataDb; +let smartdataOptions: smartdata.IMongoDescriptor; +let mongod: mongoPlugin.MongoMemoryServer; + +const totalCars = 2000; + +tap.skip.test('should create a testinstance as database', async () => { + mongod = new mongoPlugin.MongoMemoryServer({}); + console.log('created mongod instance'); + await mongod._startUpInstance().catch((err) => { + console.log(err); + }); + console.log('mongod started'); + smartdataOptions = { + mongoDbName: await mongod.getDbName(), + mongoDbPass: '', + mongoDbUrl: await mongod.getUri(), + }; + console.log(smartdataOptions); + testDb = new smartdata.SmartdataDb(smartdataOptions); +}); + +tap.test('should connect to atlas', async (tools) => { + const databaseName = `test-smartdata-${smartunique.shortId()}`; + testDb = new smartdata.SmartdataDb({ + mongoDbUrl: testQenv.getEnvVarOnDemand('MONGO_URL'), + mongoDbName: databaseName, + }); +}); + +tap.test('should establish a connection to mongod', async () => { + await testDb.init(); +}); + +// ======================================= +// The actual tests +// ======================================= + +// ------ +// Collections +// ------ +class CarTemplate extends smartdata.SmartDataDbDoc { + @smartdata.unI() + public index: string = smartunique.shortId(); + + @smartdata.svDb() + public color: string; + + @smartdata.svDb() + public brand: string; + + @smartdata.svDb() + deepData = { + sodeep: 'yes', + }; + + constructor(colorArg: string, brandArg: string) { + super(); + this.color = colorArg; + this.brand = brandArg; + } +} + +const createCarClass = (dbArg: smartdata.SmartdataDb) => { + console.log(this); + @smartdata.Collection(() => { + return dbArg; + }) + class Car extends CarTemplate {}; + return Car; +}; + +tap.test('should prodice a car', async () => { + const CarClass = createCarClass(testDb); + const carInstance = new CarClass('red', 'Mercedes'); + await carInstance.save(); +}); + +// ======================================= +// close the database connection +// ======================================= +tap.test('should close the database connection', async (tools) => { + await testDb.mongoDb.dropDatabase(); + await testDb.close(); + try { + await mongod.stop(); + } catch (e) {} +}); + +tap.start({ throwOnError: true });