-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b42e277
commit e33a62d
Showing
2 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |