Skip to content

Commit

Permalink
All outerbase connection 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-mabry committed May 29, 2024
1 parent b42e277 commit e33a62d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/connections/outerbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class OuterbaseConnection implements Connection {
query: Query
): Promise<{ data: any; error: Error | null; query: string }> {
if (!this.api_key) throw new Error('Outerbase API key is not set')
if (!query) throw new Error('Query was not provided')

const response = await fetch(`${API_URL}/api/v1/ezql/raw`, {
method: 'POST',
Expand Down Expand Up @@ -104,7 +103,6 @@ export class OuterbaseConnection implements Connection {
queryId: string
): Promise<{ data: any; error: Error | null }> {
if (!this.api_key) throw new Error('Outerbase API key is not set')
if (!queryId) throw new Error('Query ID is not set')

const response = await fetch(
`${API_URL}/api/v1/ezql/query/${queryId}`,
Expand Down
148 changes: 142 additions & 6 deletions tests/connections/outerbase.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,154 @@
import { describe, expect, test } from '@jest/globals'
import {
afterEach,
beforeEach,
describe,
expect,
jest,
test,
} from '@jest/globals'

import { OuterbaseConnection } from '../../src/connections/outerbase'
import { QueryType } from '../../src/query-params'

import fetchMock from 'fetch-mock'
describe('OuterbaseConnection', () => {
test('Can connect', async () => {
const connection = new OuterbaseConnection('API_KEY')
const actual = await connection.connect({ more: 'details' })
expect(actual).toBe(undefined)
})
test('Can disconnect', async () => {
const connection = new OuterbaseConnection('API_KEY')
const actual = await connection.disconnect()
expect(actual).toBe(undefined)
})
describe('Query', () => {
const mockApiKey = 'API_KEY'
let connection: OuterbaseConnection

beforeEach(() => {
connection = new OuterbaseConnection(mockApiKey)
fetchMock.reset()
})

afterEach(() => {
jest.clearAllMocks()
})

const TEST_QUERY = {
query: 'SELECT * FROM FAKE_TABLE;',
}
test('Should throw error when missing apiKey', async () => {
connection.api_key = undefined
await expect(connection.query(TEST_QUERY)).rejects.toThrow(
'Outerbase API key is not set'
)
})
test('Should return on successful response', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: {
result: [
{
results: 'hello world',
},
],
},
})
const actual = await connection.query(TEST_QUERY)
const expected = {
data: [],
error: null,
query: TEST_QUERY.query,
}
// Need to type the actual so I can pass it around
expect(expected).toMatchObject(actual as any)
})
test('Should return on return empty array on no results', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: {},
})
const actual = await connection.query(TEST_QUERY)
const expected = {
data: [],
error: null,
query: TEST_QUERY.query,
}

expect(actual.data).toEqual([])
expect(actual.error).toBeNull()
expect(actual.query).toEqual(TEST_QUERY.query)
})
test('Should return on return empty array on no results', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: null,
})
const actual = await connection.query(TEST_QUERY)

expect(actual.data).toEqual([])
expect(actual.error).toBeNull()
expect(actual.query).toEqual(TEST_QUERY.query)
})
test('Should return on successful response hey', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: {
result: [],
},
})
const actual = await connection.query(TEST_QUERY)
const expected = {
data: [],
error: null,
query: TEST_QUERY.query,
}
// Need to type the actual so I can pass it around
expect(expected).toMatchObject(actual as any)
})
})
describe('Run Saved Query', () => {
beforeEach(() => {
fetchMock.reset()
})
const TEST_QUERY = 'SELECT * FROM FAKE_TABLE;'

test('Should fail if the key isnt set', async () => {
const connection = new OuterbaseConnection('API_KEY')
connection.api_key = undefined
await expect(connection.runSavedQuery(TEST_QUERY)).rejects.toThrow(
'Outerbase API key is not set'
)
})
test('Should run a saved query', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: {
response: {
results: {
items: ['hello world'],
},
},
},
})
const response = await connection.runSavedQuery('1234')
expect(response.data).toEqual(['hello world'])
})
test('Should run a saved query and return no data if nothing responded', async () => {
const connection = new OuterbaseConnection('API_KEY')
fetchMock.postOnce(`*`, {
body: undefined,
})
const response = await connection.runSavedQuery('1234')
expect(response.data).toEqual([])
})
})
describe('Query Type', () => {
const connection = new OuterbaseConnection('FAKE_API_KEY')
const connection = new OuterbaseConnection('API_KEY')

test('Query type is set to named', () => {
expect(connection.queryType).toBe(QueryType.named)
})

test('Query type is set not positional', () => {
expect(connection.queryType).not.toBe(QueryType.positional)
})
})
})

0 comments on commit e33a62d

Please sign in to comment.