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 = await mongoPlugin.MongoMemoryServer.create(); console.log('created mongod instance'); console.log('mongod started'); smartdataOptions = { mongoDbUrl: mongod.getUri(), }; console.log(smartdataOptions); testDb = new smartdata.SmartdataDb(smartdataOptions); await testDb.init(); }); 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, }); await testDb.init(); }); // ======================================= // The actual tests // ======================================= // ------ // Collections // ------ @smartdata.Manager() class Car 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) => { smartdata.setDefaultManagerForDoc({ db: dbArg }, Car); return Car; }; tap.test('should produce a car', async () => { const CarClass = createCarClass(testDb); const carInstance = new CarClass('red', 'Mercedes'); await carInstance.save(); }); tap.test('should get a car', async () => { const car = Car.getInstance({ color: 'red', }); }); // ======================================= // close the database connection // ======================================= tap.test('close', async () => { if (mongod) { await mongod.stop(); } else { await testDb.mongoDb.dropDatabase(); } await testDb.close(); }); tap.start({ throwOnError: true });