fixes queries and datasource tests

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 17:45:18 +02:00
parent d86467ecd7
commit 003c1c75d4
7 changed files with 143 additions and 28 deletions

View File

@ -69,16 +69,14 @@ export const getFrontendStore = () => {
// Initialise backend stores
const [
_integrations,
_queries,
_tables,
] = await Promise.all([
api.get("/api/integrations").then(r => r.json()),
api.get(`/api/queries`).then(r => r.json()),
api.get(`/api/tables`).then(r => r.json()),
])
datasources.init()
integrations.set(_integrations)
queries.set({ list: _queries, selected: null })
queries.init()
database.set(application.instance)
tables.set({
list: _tables,

View File

@ -15,7 +15,7 @@
import IntegrationQueryEditor from "components/integration/index.svelte"
import ExternalDataSourceTable from "components/backend/DataTable/ExternalDataSourceTable.svelte"
import ParameterBuilder from "components/integration/QueryParameterBuilder.svelte"
import { datasources, integrations } from 'stores/backend/'
import { datasources, integrations, queries } from 'stores/backend/'
const PREVIEW_HEADINGS = [
{

View File

@ -10,6 +10,11 @@ export function createQueriesStore() {
subscribe,
set,
update,
init: async () => {
const response = await api.get(`/api/queries`)
const json = await response.json()
set({ list: json, selected: null })
},
fetch: async () => {
const response = await api.get(`/api/queries`)
const json = await response.json()
@ -54,7 +59,7 @@ export function createQueriesStore() {
datasources.update(state => ({ ...state, selected: query.datasourceId }))
},
delete: async query => {
await api.delete(`/api/queries/${query._id}/${query._rev}`)
const response = await api.delete(`/api/queries/${query._id}/${query._rev}`)
update(state => {
state.list = state.list.filter(existing => existing._id !== query._id)
if (state.selected === query._id) {
@ -63,6 +68,7 @@ export function createQueriesStore() {
return state
})
console.log(response)
},
}
}

View File

@ -11,8 +11,9 @@ import { queries } from '../queries'
describe("Datasources Store", () => {
let store = createDatasourcesStore()
beforeEach(() => {
store.init()
beforeEach(async () => {
api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]})
await store.init()
})
it("Initialises correctly", async () => {
@ -35,9 +36,9 @@ describe("Datasources Store", () => {
expect(get(store).select).toEqual(SOME_DATASOURCE._id)
})
it("resets the queries store when it a new datasource is selected", async () => {
it("resets the queries store when new datasource is selected", async () => {
store.select(SOME_DATASOURCE._id)
await store.select(SOME_DATASOURCE._id)
const queriesValue = get(queries)
expect(queriesValue.selected).toEqual(null)
})

View File

@ -0,0 +1,80 @@
export const SOME_QUERY = {
"datasourceId": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"parameters": [],
"fields": {
"headers": {},
"queryString": "",
"path": "Speakers"
},
"queryVerb": "read",
"schema": {},
"name": "Speakers",
"_id": "query_datasource_04b003a7b4a8428eadd3bb2f7eae0255_bcb8ffc6fcbc484e8d63121fc0bf986f",
"_rev": "2-941f8699eb0adf995f8bd59c99203b26",
"readable": true
}
export const SAVE_QUERY_RESPONSE = {
"datasourceId": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"parameters": [],
"fields": {
"headers": {},
"queryString": "",
"path": "Speakers"
},
"queryVerb": "read",
"schema": {
"id": {
"name": "id",
"type": "string"
},
"firstName": {
"name": "firstName",
"type": "string"
},
"lastName": {
"name": "lastName",
"type": "string"
},
"fullName": {
"name": "fullName",
"type": "string"
},
"bio": {
"name": "bio",
"type": "string"
},
"tagLine": {
"name": "tagLine",
"type": "string"
},
"profilePicture": {
"name": "profilePicture",
"type": "string"
},
"sessions": {
"name": "sessions",
"type": "string"
},
"isTopSpeaker": {
"name": "isTopSpeaker",
"type": "string"
},
"links": {
"name": "links",
"type": "string"
},
"questionAnswers": {
"name": "questionAnswers",
"type": "string"
},
"categories": {
"name": "categories",
"type": "string"
}
},
"name": "Speakers",
"_id": "query_datasource_04b003a7b4a8428eadd3bb2f7eae0255_bcb8ffc6fcbc484e8d63121fc0bf986f",
"_rev": "3-5a64adef494b1e9c793dc91b51ce73c6",
"readable": true
}

View File

@ -21,6 +21,5 @@ describe("Permissions Store", () => {
expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`)
expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE)
})
})

View File

@ -1,26 +1,57 @@
// import api from 'builderStore/api'
import { get } from 'svelte/store'
import api from 'builderStore/api'
// jest.mock('builderStore/api');
jest.mock('builderStore/api');
// const PERMISSIONS_FOR_RESOURCE = {
// "write": "BASIC",
// "read": "BASIC"
// }
import { SOME_QUERY, SAVE_QUERY_RESPONSE } from './fixtures/queries'
// import { createQueriesStore } from "../queries"
import { createQueriesStore } from "../queries"
import { datasources } from '../datasources'
// describe("Queries Store", () => {
// const store = createQueriesStore()
describe("Queries Store", () => {
let store = createQueriesStore()
// it("fetches permissions for specific resource", async () => {
// api.get.mockReturnValueOnce({ json: () => PERMISSIONS_FOR_RESOURCE})
beforeEach(async () => {
api.get.mockReturnValue({ json: () => [SOME_QUERY]})
await store.init()
})
// const resourceId = "ta_013657543b4043b89dbb17e9d3a4723a"
it("Initialises correctly", async () => {
api.get.mockReturnValue({ json: () => [SOME_QUERY]})
// const permissions = await store.forResource(resourceId)
await store.init()
expect(get(store)).toEqual({ list: [SOME_QUERY], selected: null})
})
// expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`)
// expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE)
it("fetches all the queries", async () => {
api.get.mockReturnValue({ json: () => [SOME_QUERY]})
// })
// })
await store.fetch()
expect(get(store)).toEqual({ list: [SOME_QUERY], selected: null})
})
it("selects a query and updates selected datasource", async () => {
await store.select(SOME_QUERY)
expect(get(store).selected).toEqual(SOME_QUERY._id)
expect(get(datasources).selected).toEqual(SOME_QUERY.datasourceId)
})
it("saves the datasource, updates the store and returns status message", async () => {
api.post.mockReturnValue({ json: () => SAVE_QUERY_RESPONSE})
await store.select(SOME_QUERY.datasourceId, SOME_QUERY)
expect(get(store).list).toEqual(expect.arrayContaining([SOME_QUERY]))
})
it("deletes a datasource, updates the store and returns status message", async () => {
api.get.mockReturnValue({ json: () => SOME_QUERY})
await store.fetch()
api.delete.mockReturnValue({status: 200, message: 'Datasource deleted.'})
await store.delete(SOME_QUERY)
expect(get(store)).toEqual({ list: [], selected: null})
})
})