Code coverage++

This commit is contained in:
delvedor
2019-02-11 17:37:44 +01:00
parent ecb2ba47a8
commit d9a5f24e24
3 changed files with 163 additions and 0 deletions

View File

@ -490,3 +490,80 @@ test('API', t => {
t.end()
})
test('Node selector', t => {
t.test('round-robin', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection, nodeSelector: 'round-robin' })
pool.addConnection('http://localhost:9200/')
t.true(pool.getConnection() instanceof Connection)
})
t.test('random', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection, nodeSelector: 'random' })
pool.addConnection('http://localhost:9200/')
t.true(pool.getConnection() instanceof Connection)
})
t.test('custom function', t => {
t.plan(2)
const nodeSelector = connections => {
t.ok('called')
return connections[0]
}
const pool = new ConnectionPool({ Connection, nodeSelector })
pool.addConnection('http://localhost:9200/')
t.true(pool.getConnection() instanceof Connection)
})
t.end()
})
test('Node filter', t => {
t.test('default', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection })
pool.addConnection({ url: new URL('http://localhost:9200/') })
t.true(pool.getConnection() instanceof Connection)
})
t.test('Should filter master only nodes', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection })
pool.addConnection({
url: new URL('http://localhost:9200/'),
roles: {
master: true,
data: false,
ingest: false,
ml: false
}
})
t.strictEqual(pool.getConnection(), null)
})
t.test('custom', t => {
t.plan(2)
const nodeFilter = node => {
t.ok('called')
return true
}
const pool = new ConnectionPool({ Connection, nodeFilter })
pool.addConnection({ url: new URL('http://localhost:9200/') })
t.true(pool.getConnection() instanceof Connection)
})
t.test('custom (filter)', t => {
t.plan(2)
const nodeFilter = node => {
t.ok('called')
return false
}
const pool = new ConnectionPool({ Connection, nodeFilter })
pool.addConnection({ url: new URL('http://localhost:9200/') })
t.strictEqual(pool.getConnection(), null)
})
t.end()
})

View File

@ -591,3 +591,63 @@ test('Should disallow two-byte characters in URL path', t => {
)
})
})
test('setRole', t => {
t.test('Update the value of a role', t => {
t.plan(2)
const connection = new Connection({
url: new URL('http://localhost:9200')
})
t.deepEqual(connection.roles, {
master: true,
data: true,
ingest: true,
ml: false
})
connection.setRole('master', false)
t.deepEqual(connection.roles, {
master: false,
data: true,
ingest: true,
ml: false
})
})
t.test('Invalid role', t => {
t.plan(2)
const connection = new Connection({
url: new URL('http://localhost:9200')
})
try {
connection.setRole('car', true)
t.fail('Shoud throw')
} catch (err) {
t.true(err instanceof ConfigurationError)
t.is(err.message, 'Unsupported role: \'car\'')
}
})
t.test('Invalid value', t => {
t.plan(2)
const connection = new Connection({
url: new URL('http://localhost:9200')
})
try {
connection.setRole('master', 1)
t.fail('Shoud throw')
} catch (err) {
t.true(err instanceof ConfigurationError)
t.is(err.message, 'enabled should be a boolean')
}
})
t.end()
})

View File

@ -377,6 +377,32 @@ test('SerializationError', t => {
})
})
test('SerializationError (bulk)', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
})
const bulkBody = { hello: 'world' }
bulkBody.o = bulkBody
transport.request({
method: 'POST',
path: '/hello',
bulkBody
}, (err, { body }) => {
t.ok(err instanceof SerializationError)
})
})
test('DeserializationError', t => {
t.plan(1)
function handler (req, res) {