Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: reorganize and refactor client tests #654

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 43 additions & 22 deletions client/test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,45 +35,54 @@ 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) {
alreadyDone = true
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 = [
Expand Down
8 changes: 4 additions & 4 deletions client/test/times.js → client/test/query/time.js
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were not using arrow functions (which capture lexical scope) in all of our it assertions, we could reference this.hz_data and/or this.horizon in all our tests directly. This is unfortunate. Down the road it may be worth avoiding arrow functions in the tests so we can more easily manage their context and further avoid referencing things out of scope.

})

let range = count => Array.from(Array(count).keys())
Expand Down
8 changes: 5 additions & 3 deletions client/test/auth.js → client/test/query/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 0 additions & 16 deletions client/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions client/test/insert.js → client/test/write/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -147,4 +147,4 @@ const insertSuite = global.insertSuite = getData => () => {
assert.lengthOf(res, 0)
})
))
} // Testing `insert`
}
14 changes: 5 additions & 9 deletions client/test/remove.js → client/test/write/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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
Expand Down
21 changes: 8 additions & 13 deletions client/test/removeAll.js → client/test/write/removeAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions client/test/replace.js → client/test/write/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions client/test/store.js → client/test/write/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
}
8 changes: 4 additions & 4 deletions client/test/update.js → client/test/write/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions client/test/upsert.js → client/test/write/upsert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down