diff --git a/src/connections/outerbase.ts b/src/connections/outerbase.ts index 22d6fb0..9c49f75 100644 --- a/src/connections/outerbase.ts +++ b/src/connections/outerbase.ts @@ -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', @@ -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}`, diff --git a/tests/connections/outerbase.test.ts b/tests/connections/outerbase.test.ts index 0f520eb..e6e991c 100644 --- a/tests/connections/outerbase.test.ts +++ b/tests/connections/outerbase.test.ts @@ -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) - }) }) })