From 3fc76fca1e9de24f773051e36134b38569ba0d55 Mon Sep 17 00:00:00 2001 From: marshall007 Date: Wed, 20 Jul 2016 01:12:22 -0500 Subject: [PATCH] WIP: reorganize and refactor tests to avoid globals --- client/test/api.js | 65 ++++++++++++++++--------- client/test/{times.js => query/time.js} | 8 +-- client/test/{auth.js => query/user.js} | 8 +-- client/test/test.js | 16 ------ client/test/{ => write}/insert.js | 10 ++-- client/test/{ => write}/remove.js | 14 ++---- client/test/{ => write}/removeAll.js | 21 +++----- client/test/{ => write}/replace.js | 8 +-- client/test/{ => write}/store.js | 10 ++-- client/test/{ => write}/update.js | 8 +-- client/test/{ => write}/upsert.js | 8 +-- 11 files changed, 87 insertions(+), 89 deletions(-) rename client/test/{times.js => query/time.js} (93%) rename client/test/{auth.js => query/user.js} (94%) rename client/test/{ => write}/insert.js (96%) rename client/test/{ => write}/remove.js (91%) rename client/test/{ => write}/removeAll.js (91%) rename client/test/{ => write}/replace.js (96%) rename client/test/{ => write}/store.js (96%) rename client/test/{ => write}/update.js (96%) rename client/test/{ => write}/upsert.js (96%) diff --git a/client/test/api.js b/client/test/api.js index 49c62bd00..5a04a4a7f 100644 --- a/client/test/api.js +++ b/client/test/api.js @@ -5,6 +5,18 @@ import { toArray } from 'rxjs/operator/toArray' import { assertCompletes, removeAllData, compareSetsWithoutVersion } from './utils' +import store from './write/store' +import insert from './write/insert' +import upsert from './write/upsert' +import update from './write/update' +import replace from './write/replace' + +import remove from './write/remove' +import removeAll from './write/removeAll' + +import time from './query/time' +import user from './query/user' + // This test suite covers various edge cases in the Horizon client library API. // It does not cover correctness of the full system in various circumstances. // The purpose of the API test suite is to act as a runnable, checkable spec for @@ -23,18 +35,18 @@ describe('Core API tests', () => { const getData = () => data // Set up the horizon connection before running these tests. - before(done => { + before(function(done) { Horizon.clearAuthTokens() - horizon = Horizon({ secure: false, lazyWrites: true }) - horizon.connect(err => done(err)) - horizon.onReady(() => { - data = horizon('test_data') + this.horizon = Horizon({ secure: false, lazyWrites: true }) + this.horizon.connect(err => done(err)) + this.horizon.onReady(() => { + this.hz_data = this.horizon('test_data') done() }) }) // Kill the horizon connection after running these tests. - after(done => { + after(function(done) { let alreadyDone = false function wrappedDone(...args) { if (!alreadyDone) { @@ -42,26 +54,35 @@ describe('Core API tests', () => { return done(...args) } } - horizon.disconnect() - horizon.onDisconnected(() => wrappedDone()) + this.horizon.disconnect() + this.horizon.onDisconnected(wrappedDone) }) // Test the mutation commands - describe('Storage API', () => { + describe('Write API', () => { // Drop all data after each test - afterEach(done => removeAllData(data, done)) - - describe('Testing `store`', storeSuite(getData)) - describe('Testing `insert`', insertSuite(getData)) - describe('Testing `upsert`', upsertSuite(getData)) - describe('Testing `update`', updateSuite(getData)) - describe('Testing `replace`', replaceSuite(getData)) - }) // Storage API - describe('Testing `remove`', removeSuite(getData)) - describe('Testing `removeAll`', removeAllSuite(getData)) - - describe('Testing `times`', timesSuite(getData)) - describe('Testing authentication', authSuite(getHorizon)) + afterEach(function(done) { + removeAllData(this.hz_data, done) + }) + + describe('Testing `store`', store) + describe('Testing `insert`', insert) + describe('Testing `upsert`', upsert) + describe('Testing `update`', update) + describe('Testing `replace`', replace) + }) + + + describe('Remove API', () => { + describe('Testing `remove`', remove) + describe('Testing `removeAll`', removeAll) + }) + + describe('Query API', () => { + describe('Testing `date and time`', time) + describe('Testing `user`', user) + }) + // Test the lookup API describe('Lookup API', () => { const testData = [ diff --git a/client/test/times.js b/client/test/query/time.js similarity index 93% rename from client/test/times.js rename to client/test/query/time.js index 2c033ff8f..6619d10e3 100644 --- a/client/test/times.js +++ b/client/test/query/time.js @@ -1,13 +1,13 @@ import { _do as tap } from 'rxjs/operator/do' import { toArray } from 'rxjs/operator/toArray' -import { assertCompletes, compareWithoutVersion } from './utils' +import { assertCompletes, compareWithoutVersion } from '../utils' -const timesSuite = global.timesSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) let range = count => Array.from(Array(count).keys()) diff --git a/client/test/auth.js b/client/test/query/user.js similarity index 94% rename from client/test/auth.js rename to client/test/query/user.js index 735cc9292..a9c6f02c6 100644 --- a/client/test/auth.js +++ b/client/test/query/user.js @@ -2,11 +2,13 @@ import { _do as tap } from 'rxjs/operator/do' import { mergeMap } from 'rxjs/operator/mergeMap' import { mergeMapTo } from 'rxjs/operator/mergeMapTo' -const authSuite = global.authSuite = (getHorizon) => () => { +export default function() { let horizon - before(() => { - horizon = getHorizon() + + before(function() { + horizon = this.horizon }) + it('gets an empty object when unauthenticated', done => { horizon.currentUser().fetch().subscribe({ next(user) { diff --git a/client/test/test.js b/client/test/test.js index d19b319ac..8e064dc9e 100644 --- a/client/test/test.js +++ b/client/test/test.js @@ -66,22 +66,6 @@ require('./utils') // Testing the Horizon object require('./horizonObject.js') -// Testing insertion/storage commands -require('./store.js') -require('./insert.js') -require('./upsert.js') -require('./update.js') -require('./replace.js') - -// Test the removal commands -require('./remove.js') -require('./removeAll.js') - -// Times tests -require('./times.js') -// Authentication tests -require('./auth.js') - // Read API require('./collection.js') require('./find.js') diff --git a/client/test/insert.js b/client/test/write/insert.js similarity index 96% rename from client/test/insert.js rename to client/test/write/insert.js index 396e64bf2..8442dfcb8 100644 --- a/client/test/insert.js +++ b/client/test/write/insert.js @@ -7,13 +7,13 @@ import { assertCompletes, assertThrows, assertErrors, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const insertSuite = global.insertSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) // The `insert` command stores documents in the database, and errors if @@ -147,4 +147,4 @@ const insertSuite = global.insertSuite = getData => () => { assert.lengthOf(res, 0) }) )) -} // Testing `insert` +} diff --git a/client/test/remove.js b/client/test/write/remove.js similarity index 91% rename from client/test/remove.js rename to client/test/write/remove.js index e212bd462..c73c96844 100644 --- a/client/test/remove.js +++ b/client/test/write/remove.js @@ -10,10 +10,11 @@ import { assertCompletes, assertErrors, removeAllData, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const removeSuite = global.removeSuite = getData => () => { +export default function() { let data + const testData = [ { id: 1, a: 1 }, { id: 2, a: 2 }, @@ -22,13 +23,8 @@ const removeSuite = global.removeSuite = getData => () => { { id: 'do_not_remove_2' }, ] - before(() => { - data = getData() - }) - - // Drop all the existing data - before(done => { - removeAllData(data, done) + before(function() { + data = this.hz_data }) // Insert the test data and make sure it's in diff --git a/client/test/removeAll.js b/client/test/write/removeAll.js similarity index 91% rename from client/test/removeAll.js rename to client/test/write/removeAll.js index f0dadce58..808c28eaf 100644 --- a/client/test/removeAll.js +++ b/client/test/write/removeAll.js @@ -10,34 +10,29 @@ import { assertCompletes, assertErrors, removeAllData, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const removeAllSuite = global.removeAllSuite = getData => () => { +export default function() { let data + const testData = [ { id: 1, a: 1 }, { id: 2, a: 2 }, { id: 3, a: 3 }, - { id: 4, a: 4 }, { id: 'do_not_remove_1' }, { id: 'do_not_remove_2' }, ] - before(() => { - data = getData() - }) - - // Drop all the existing data - before(done => { - removeAllData(data, done) + before(function() { + data = this.hz_data }) // Insert the test data and make sure it's in before(assertCompletes(() => data.store(testData)::ignoreElements() - ::concat(data.fetch()) - // Make sure it's there - ::tap(res => compareSetsWithoutVersion(res, testData)) + ::concat(data.fetch()) + // Make sure it's there + ::tap(res => compareSetsWithoutVersion(res, testData)) )) // All right, let's remove a document. The promise resolves with no diff --git a/client/test/replace.js b/client/test/write/replace.js similarity index 96% rename from client/test/replace.js rename to client/test/write/replace.js index 5a1067d8f..ff06d2178 100644 --- a/client/test/replace.js +++ b/client/test/write/replace.js @@ -6,13 +6,13 @@ import { assertCompletes, assertThrows, assertErrors, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const replaceSuite = global.replaceSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) // Let's store a document first, then replace it. diff --git a/client/test/store.js b/client/test/write/store.js similarity index 96% rename from client/test/store.js rename to client/test/write/store.js index 8d183b4c0..f2d1d4a52 100644 --- a/client/test/store.js +++ b/client/test/write/store.js @@ -7,13 +7,13 @@ import { assertCompletes, assertThrows, assertErrors, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const storeSuite = global.storeSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) // The `store` command stores documents in the database, and overwrites @@ -139,4 +139,4 @@ const storeSuite = global.storeSuite = getData => () => { ::mergeMap(id => data.find(id[0]).fetch()) ::tap(result => assert.deepEqual(originalDate, result.date)) })) -} // Testing `store` +} diff --git a/client/test/update.js b/client/test/write/update.js similarity index 96% rename from client/test/update.js rename to client/test/write/update.js index cb328a7fc..fd7a537eb 100644 --- a/client/test/update.js +++ b/client/test/write/update.js @@ -7,13 +7,13 @@ import { assertCompletes, assertThrows, assertErrors, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const updateSuite = global.updateSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) // Let's store a document first, then update it. diff --git a/client/test/upsert.js b/client/test/write/upsert.js similarity index 96% rename from client/test/upsert.js rename to client/test/write/upsert.js index 3bf9b9dd3..cbd350c44 100644 --- a/client/test/upsert.js +++ b/client/test/write/upsert.js @@ -7,13 +7,13 @@ import { assertCompletes, assertThrows, assertErrors, compareWithoutVersion, - compareSetsWithoutVersion } from './utils' + compareSetsWithoutVersion } from '../utils' -const upsertSuite = global.upsertSuite = getData => () => { +export default function() { let data - before(() => { - data = getData() + before(function() { + data = this.hz_data }) // The `upsert` command stores documents in the database, and updates