database.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict'
  2. import test from 'ava'
  3. import { json, getHttpServer, connectDb } from '../utils/_test_functions'
  4. import DataController from '../../server/database/controllers/Data'
  5. // Database and WebSocket testing
  6. // Before all tests, connect to the database
  7. test.beforeEach(async t => (t.context.server = await getHttpServer()))
  8. test('Check database is working', async t => {
  9. // Connect to database
  10. await connectDb()
  11. // Add the document
  12. const testData = { AUTOMATED_TEST_DB: true, TEST_DATABASE_OBJ: { msg: 'add' } }
  13. const doc = await DataController.add(testData)
  14. t.deepEqual(doc.data, testData, json(doc))
  15. // Find the document
  16. const findDoc = await DataController.find(doc.id)
  17. t.deepEqual(findDoc.data, testData, json(findDoc))
  18. // Update the document
  19. testData.TEST_DATABASE_OBJ.msg = 'updated'
  20. const updateTo = { AUTOMATED_TEST_DB: true, newObject: 'test', newProperties: { test: true } }
  21. const docUpdated = await DataController.update(doc.id, updateTo)
  22. t.deepEqual(docUpdated.data, updateTo, json(docUpdated))
  23. // Delete the added document
  24. await t.notThrowsAsync(DataController.del(doc.id))
  25. })