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

chore: Convert db unit tests to node:test #2514

Merged
merged 7 commits into from
Aug 27, 2024
Merged
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
163 changes: 70 additions & 93 deletions test/unit/db/query-parsers/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

'use strict'

const { test } = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const parseSql = require('../../../../lib/db/query-parsers/sql')
const CATs = require('../../../lib/cross_agent_tests/sql_parsing')

Expand All @@ -19,38 +20,33 @@ function clean(sql) {
return '"' + sql.replace(/\n/gm, '\\n').replace(/\r/gm, '\\r').replace(/\t/gm, '\\t') + '"'
}

test('database query parser', function (t) {
t.autoend()
t.test('should accept query as a string', function (t) {
test('database query parser', async (t) => {
await t.test('should accept query as a string', function () {
const ps = parseSql('select * from someTable')
t.equal(ps.query, 'select * from someTable')
t.end()
assert.equal(ps.query, 'select * from someTable')
})

t.test('should accept query as a sql property of an object', function (t) {
await t.test('should accept query as a sql property of an object', function () {
const ps = parseSql({
sql: 'select * from someTable'
})
t.equal(ps.query, 'select * from someTable')
t.end()
assert.equal(ps.query, 'select * from someTable')
})

t.test('SELECT SQL', function (t) {
t.autoend()
t.test('should parse a simple query', function (t) {
await t.test('SELECT SQL', async (t) => {
await t.test('should parse a simple query', function () {
const ps = parseSql('Select * from dude')
t.ok(ps)
assert.ok(ps)

t.ok(ps.operation)
t.equal(ps.operation, 'select')
assert.ok(ps.operation)
assert.equal(ps.operation, 'select')

t.ok(ps.collection)
t.equal(ps.collection, 'dude')
t.equal(ps.query, 'Select * from dude')
t.end()
assert.ok(ps.collection)
assert.equal(ps.collection, 'dude')
assert.equal(ps.query, 'Select * from dude')
})

t.test('should parse more interesting queries too', function (t) {
await t.test('should parse more interesting queries too', function () {
const sql = [
'SELECT P.postcode, ',
'P.suburb, ',
Expand All @@ -66,121 +62,102 @@ test('database query parser', function (t) {
'LIMIT 1'
].join('\n')
const ps = parseSql(sql)
t.ok(ps)
t.equal(ps.operation, 'select')
t.equal(ps.collection, 'postcodes')
t.equal(ps.query, sql)
t.end()
assert.ok(ps)
assert.equal(ps.operation, 'select')
assert.equal(ps.collection, 'postcodes')
assert.equal(ps.query, sql)
})
})

t.test('DELETE SQL', function (t) {
t.autoend()
t.test('should parse a simple command', function (t) {
await t.test('DELETE SQL', async (t) => {
await t.test('should parse a simple command', function () {
const ps = parseSql('DELETE\nfrom dude')
t.ok(ps)
assert.ok(ps)

t.ok(ps.operation)
t.equal(ps.operation, 'delete')
assert.ok(ps.operation)
assert.equal(ps.operation, 'delete')

t.ok(ps.collection)
t.equal(ps.collection, 'dude')
t.equal(ps.query, 'DELETE\nfrom dude')
t.end()
assert.ok(ps.collection)
assert.equal(ps.collection, 'dude')
assert.equal(ps.query, 'DELETE\nfrom dude')
})

t.test('should parse a command with conditions', function (t) {
await t.test('should parse a command with conditions', function () {
const ps = parseSql("DELETE\nfrom dude where name = 'man'")
t.ok(ps)
assert.ok(ps)

t.ok(ps.operation)
t.equal(ps.operation, 'delete')
assert.ok(ps.operation)
assert.equal(ps.operation, 'delete')

t.ok(ps.collection)
t.equal(ps.collection, 'dude')
t.equal(ps.query, "DELETE\nfrom dude where name = 'man'")
t.end()
assert.ok(ps.collection)
assert.equal(ps.collection, 'dude')
assert.equal(ps.query, "DELETE\nfrom dude where name = 'man'")
})
})

t.test('UPDATE SQL', function (t) {
t.autoend()
t.test('should parse a command with gratuitous white space and conditions', function (t) {
await t.test('UPDATE SQL', function (t) {
t.test('should parse a command with gratuitous white space and conditions', function () {
const ps = parseSql(' update test set value = 1 where id = 12')
t.ok(ps)
assert.ok(ps)

t.ok(ps.operation)
t.equal(ps.operation, 'update')
assert.ok(ps.operation)
assert.equal(ps.operation, 'update')

t.ok(ps.collection)
t.equal(ps.collection, 'test')
t.equal(ps.query, 'update test set value = 1 where id = 12')
t.end()
assert.ok(ps.collection)
assert.equal(ps.collection, 'test')
assert.equal(ps.query, 'update test set value = 1 where id = 12')
})
})

t.test('INSERT SQL', function (t) {
t.autoend()
t.test('should parse a command with a subquery', function (t) {
await t.test('INSERT SQL', function (t) {
t.test('should parse a command with a subquery', function () {
const ps = parseSql(' insert into\ntest\nselect * from dude')
t.ok(ps)
assert.ok(ps)

t.ok(ps.operation)
t.equal(ps.operation, 'insert')
assert.ok(ps.operation)
assert.equal(ps.operation, 'insert')

t.ok(ps.collection)
t.equal(ps.collection, 'test')
t.equal(ps.query, 'insert into\ntest\nselect * from dude')
t.end()
assert.ok(ps.collection)
assert.equal(ps.collection, 'test')
assert.equal(ps.query, 'insert into\ntest\nselect * from dude')
})
})

t.test('invalid SQL', function (t) {
t.autoend()
t.test("should return 'other' when handed garbage", function (t) {
await t.test('invalid SQL', async (t) => {
await t.test("should return 'other' when handed garbage", function () {
const ps = parseSql(' bulge into\ndudes\nselect * from dude')
t.ok(ps)
t.equal(ps.operation, 'other')
t.notOk(ps.collection)
t.equal(ps.query, 'bulge into\ndudes\nselect * from dude')
t.end()
assert.ok(ps)
assert.equal(ps.operation, 'other')
assert.ok(!ps.collection)
assert.equal(ps.query, 'bulge into\ndudes\nselect * from dude')
})

t.test("should return 'other' when handed an object", function (t) {
await t.test("should return 'other' when handed an object", function () {
const ps = parseSql({
key: 'value'
})
t.ok(ps)
t.equal(ps.operation, 'other')
t.notOk(ps.collection)
t.equal(ps.query, '')
t.end()
assert.ok(ps)
assert.equal(ps.operation, 'other')
assert.ok(!ps.collection)
assert.equal(ps.query, '')
})
})

t.test('CAT', function (t) {
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
t.autoend()
CATs.forEach(function (cat) {
t.test(clean(cat.input), function (t) {
t.autoend()
await t.test('CAT', async function (t) {
amychisholm03 marked this conversation as resolved.
Show resolved Hide resolved
for (const cat of CATs) {
await t.test(clean(cat.input), async (t) => {
const ps = parseSql(cat.input)

t.test('should parse the operation as ' + cat.operation, function (t) {
t.equal(ps.operation, cat.operation)
t.end()
})
assert.equal(ps.operation, cat.operation, `should parse the operation as ${cat.operation}`)

if (cat.table === '(subquery)') {
t.test('should parse subquery collections as ' + cat.table)
t.todo('should parse subquery collections as ' + cat.table)
} else if (/\w+\.\w+/.test(ps.collection)) {
t.test('should strip database names from collection names as ' + cat.table)
t.todo('should strip database names from collection names as ' + cat.table)
} else {
t.test('should parse the collection as ' + cat.table, function (t) {
t.equal(ps.collection, cat.table)
t.end()
})
assert.equal(ps.collection, cat.table, `should parse the collection as ${cat.table}`)
}
})
})
}
})
})
65 changes: 23 additions & 42 deletions test/unit/db/query-sample.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const sinon = require('sinon')
const QuerySample = require('../../../lib/db/query-sample')
const codec = require('../../../lib/util/codec')

tap.test('Query Sample', (t) => {
t.autoend()

t.test('should set trace to query with longest duration', (t) => {
test('Query Sample', async (t) => {
await t.test('should set trace to query with longest duration', () => {
const trace = {
duration: 3
}
Expand All @@ -25,12 +24,10 @@ tap.test('Query Sample', (t) => {
const querySample = new QuerySample(tracer, trace)
querySample.aggregate(slowQuery)

t.equal(querySample.trace.duration, 30)

t.end()
assert.equal(querySample.trace.duration, 30)
})

t.test('should not set trace to query with shorter duration', (t) => {
await t.test('should not set trace to query with shorter duration', () => {
const trace = {
duration: 30
}
Expand All @@ -42,12 +39,10 @@ tap.test('Query Sample', (t) => {
const querySample = new QuerySample(tracer, trace)
querySample.aggregate(slowQuery)

t.equal(querySample.trace.duration, 30)

t.end()
assert.equal(querySample.trace.duration, 30)
})

t.test('should merge sample with longer duration', (t) => {
await t.test('should merge sample with longer duration', () => {
const slowSample = {
trace: {
duration: 30
Expand All @@ -61,12 +56,10 @@ tap.test('Query Sample', (t) => {
const querySample = new QuerySample(tracer, trace)
querySample.merge(slowSample)

t.equal(querySample.trace.duration, 30)

t.end()
assert.equal(querySample.trace.duration, 30)
})

t.test('should not merge sample with shorter duration', (t) => {
await t.test('should not merge sample with shorter duration', () => {
const slowSample = {
trace: {
duration: 3
Expand All @@ -80,12 +73,10 @@ tap.test('Query Sample', (t) => {
const querySample = new QuerySample(tracer, trace)
querySample.merge(slowSample)

t.equal(querySample.trace.duration, 30)

t.end()
assert.equal(querySample.trace.duration, 30)
})

t.test('should encode json when simple_compression is disabled', (t) => {
await t.test('should encode json when simple_compression is disabled', () => {
const fakeTracer = {
config: {
simple_compression: false
Expand All @@ -108,15 +99,13 @@ tap.test('Query Sample', (t) => {

querySample.prepareJSON(() => {})

t.ok(codecCalled)
assert.ok(codecCalled)

QuerySample.prototype.getParams.restore()
codec.encode.restore()

t.end()
})

t.test('should call _getJSON when simple_compression is enabled', (t) => {
await t.test('should call _getJSON when simple_compression is enabled', () => {
const fakeTracer = {
config: {
simple_compression: true,
Expand Down Expand Up @@ -151,15 +140,13 @@ tap.test('Query Sample', (t) => {

clock.runAll()

t.ok(getFullNameCalled)
assert.ok(getFullNameCalled)

clock.restore()
QuerySample.prototype.getParams.restore()

t.end()
})

t.test('should return segment attributes as params if present', (t) => {
await t.test('should return segment attributes as params if present', () => {
const expectedParams = {
host: 'host',
port_path_or_id: 1,
Expand Down Expand Up @@ -187,14 +174,12 @@ tap.test('Query Sample', (t) => {

const result = querySample.getParams()

t.equal(result.host, expectedParams.host)
t.equal(result.port_path_or_id, expectedParams.port_path_or_id)
t.equal(result.database_name, expectedParams.database_name)

t.end()
assert.equal(result.host, expectedParams.host)
assert.equal(result.port_path_or_id, expectedParams.port_path_or_id)
assert.equal(result.database_name, expectedParams.database_name)
})

t.test('should add DT intrinsics when DT enabled', (t) => {
await t.test('should add DT intrinsics when DT enabled', () => {
let addDtIntrinsicsCalled = false
const fakeTracer = {
config: {
Expand All @@ -219,12 +204,10 @@ tap.test('Query Sample', (t) => {

querySample.getParams()

t.equal(addDtIntrinsicsCalled, true)

t.end()
assert.equal(addDtIntrinsicsCalled, true)
})

t.test('should not add DT intrinsics when DT disabled', (t) => {
await t.test('should not add DT intrinsics when DT disabled', () => {
let addDtIntrinsicsCalled = false
const fakeTracer = {
config: {
Expand All @@ -249,8 +232,6 @@ tap.test('Query Sample', (t) => {

querySample.getParams()

t.equal(addDtIntrinsicsCalled, false)

t.end()
assert.equal(addDtIntrinsicsCalled, false)
})
})
Loading
Loading