Skip to content

Commit

Permalink
✅ add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbaye THIAM committed Apr 16, 2024
1 parent b5c3a68 commit 2835091
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 31 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ jobs:
- name: Setup services for testing purpose
run: export IMAGE_TAG=localhost:5000/test/graphql-mesh:latest && cd ./test/integration && docker compose up -d

- name: Inspect network
run: docker network inspect integration_default

- name: Set up Node.js
uses: actions/setup-node@v3
with:
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-mesh/utils/swaggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export const generateTypeDefsAndResolversFromSwagger = (
root = { ...root, followLink: hateoasLink.href }
}

// @TODO: Fix type checking for interger params
if (paramsToSend.length) {
paramsToSend.forEach((param, i) => {
args[param] = root[param] || root[paramsFromLink[i]] || ''
// To avoid params validation error in case of missing params or type mismatch we set default value to '0'
args[param] = root[param] || root[paramsFromLink[i]] || '0'
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/api/models/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Product = {
function generateProducts() {
const _products: Product[] = []
for (let i = 1; i <= 50; i++) {
const supplierId = Math.floor(Math.random() * 10) + 1
const supplierId = (i % 10) + 1
const product: Product = {
id: i,
name: `Product ${i}`,
Expand Down
25 changes: 25 additions & 0 deletions test/integration/tests/cases/directive-spl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { test, expect } from 'vitest'
import axios from 'axios'
import { url, headers } from '../config'

/* SPL filter */
const getTenFirstProductsQuery = /* GraphQL */ `
query getTenFirstProducts {
getProducts {
items @SPL(query: "id <= 10") {
name
id
price
supplierId
}
}
}
`

test('getTenFirstProducts with SPL filter query', async () => {
const response = await axios.post(url, { query: getTenFirstProductsQuery }, { headers })

const result = response.data
expect(response.status).toBe(200)
expect(result.data.getProducts.items.length).toEqual(10)
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { test, expect } from 'vitest'
import axios from 'axios'

const url = 'http://0.0.0.0:45538/graphql'
const headers = { 'Content-Type': 'application/json' }
import { url, headers } from '../config'

/* Get all products */
const getAllProductsQuery = /* GraphQL */ `
Expand All @@ -28,46 +26,55 @@ test('getAllProducts query', async () => {
expect(result.data.getProducts.items.length).toEqual(50)
})

/* SPL filter */
const getTenFirstProductsQuery = /* GraphQL */ `
query getTenFirstProducts {
getProducts {
items @SPL(query: "id <= 10") {
/* Hateoas link */
const getProductAndSupplierInfo = /* GraphQL */ `
query getProductAndSupplierInfo {
getProductById(id: 1) {
id
name
price
supplier {
name
id
price
supplierId
}
}
}
`

test('getTenFirstProducts with SPL filter query', async () => {
const response = await axios.post(url, { query: getTenFirstProductsQuery }, { headers })
test('Follow hateoas link to get Suppier info', async () => {
const response = await axios.post(url, { query: getProductAndSupplierInfo }, { headers })

const result = response.data
expect(response.status).toBe(200)
expect(result.data.getProducts.items.length).toEqual(10)
expect(result.data.getProductById.supplier.name).contains('Supplier 2')
})

/* Hateoas link */
const getProductAndSupplierInfo = /* GraphQL */ `
query getProductAndSupplierInfo {
/* Linklist property */

const getProductwithLinkList = /* GraphQL */ `
query getProductWithLinkList {
getProductById(id: 1) {
id
name
price
supplier {
name
id
supplierId
_linksList {
rel
href
}
}
}
`

test('Follow hateoas link to get Suppier info', async () => {
const response = await axios.post(url, { query: getProductAndSupplierInfo }, { headers })
const response = await axios.post(url, { query: getProductwithLinkList }, { headers })

const result = response.data
expect(result.data.getProductById.supplier.name).contains('Supplier')
expect(result.data.getProductById._linksList.length).toEqual(2)
expect(result.data.getProductById._linksList).toEqual([
{
rel: 'self',
href: '/products/1'
},
{
rel: 'supplier',
href: '/suppliers/2'
}
])
})
22 changes: 22 additions & 0 deletions test/integration/tests/cases/inject-additionnal-transforms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test, expect } from 'vitest'
import axios from 'axios'
import { url, headers } from '../config'

/* Get all products */
const getProductById = /* GraphQL */ `
query getProduct {
getProductById(id: 1) {
name @lower
price
supplierId
}
}
`

test('Injection lower transform', async () => {
const response = await axios.post(url, { query: getProductById }, { headers })

const result = response.data
expect(result).toHaveProperty('data')
expect(result.data.getProductById.name).toEqual('product 1')
})
20 changes: 20 additions & 0 deletions test/integration/tests/cases/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test, expect } from 'vitest'
import axios from 'axios'
import { url, headers } from '../config'

/* Get all products */
const getProductById = /* GraphQL */ `
query getProduct {
getProductById(id: 1) {
name
price
supplierId
}
}
`

test('Server timing plugin', async () => {
const response = await axios.post(url, { query: getProductById }, { headers })
const serverTiming = response.headers['server-timing']
expect(serverTiming).contains('getProductById;desc="getProductById (Products)";dur')
})
3 changes: 3 additions & 0 deletions test/integration/tests/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const url = 'http://0.0.0.0:45538/graphql'
// export const url = 'http://0.0.0.0:4000/graphql'
export const headers = { 'Content-Type': 'application/json' }

0 comments on commit 2835091

Please sign in to comment.