Skip to content

Commit

Permalink
add some testing for main data endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed Jul 10, 2024
1 parent a933207 commit ce34ba5
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 39 deletions.
24 changes: 19 additions & 5 deletions src/endpoints/poems/poemFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PoemFetch extends OpenAPIRoute {
.min(1)
.optional()
.describe(
"If a Poem is part of a set, it's index inside it",
"If a Poem is part of a set, it's index inside it"
),
}),
},
Expand Down Expand Up @@ -53,7 +53,13 @@ export class PoemFetch extends OpenAPIRoute {
let { id, title, subindex } = data.query
subindex ??= 1
title ??= ''

if (id) {
title = ''
subindex = 0
}
id ??= ''

try {
const result = await db.query.poems.findFirst({
orderBy: (poems) => poems.subindex,
Expand All @@ -62,16 +68,24 @@ export class PoemFetch extends OpenAPIRoute {
eq(poems.xata_id, id),
and(
eq(poems.title, title.toUpperCase()),
eq(poems.subindex, subindex),
),
eq(poems.subindex, subindex)
)
),
})
if (!result) {
return new Response('No poem Found', { status: 404 })
return {
ok: false,
status: 404,
cause: 'No poem Found',
}
}
return result
} catch (error) {
return new Response(JSON.stringify(data), { status: error.status })
return {
ok: false,
...data,
error,
}
}
}
}
28 changes: 14 additions & 14 deletions test/authors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import { SELF } from 'cloudflare:test'
import { describe, expect, it } from 'vitest'

describe('Authors Endpoints', () => {
describe('Authors', () => {
it('list all authors', async () => {
describe('List', async () => {
it('Basic', async () => {
const response = await SELF.fetch('http://example.com/autores')
expect(await response.json()).containSubset([
{
name: 'José Lezama Lima',
},
])
})
// it('only list author that match a certain country', () => {
// })
// it('only list author born after x date', () => {
// })
// it('only list author born before x date', () => {
// })
// it('only list author that died after x date', () => {
// })
// it('only list author that died before x date', () => {
// })
// it('only list author lived between x-y date', () => {
// })
})
// it('only list author that match a certain country', () => {
// })
// it('only list author born after x date', () => {
// })
// it('only list author born before x date', () => {
// })
// it('only list author that died after x date', () => {
// })
// it('only list author that died before x date', () => {
// })
// it('only list author lived between x-y date', () => {
// })
})
34 changes: 34 additions & 0 deletions test/books.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SELF } from 'cloudflare:test'
import { describe, expect, it, assert } from 'vitest'

describe('Books Endpoints', () => {
describe('List', () => {
it('Basic', async () => {
const response = await SELF.fetch('http://example.com/libros')
const json = await response.json()
expect(json).containSubset([
{
title: 'Fragmentos a su imán',
},
])
})
it('Should return the amount of books determined in the per_page param', async () => {
const response = await SELF.fetch(
'http://example.com/libros?per_page=5'
)
const json = await response.json()
expect(json).toHaveLength(5)
})
it('Should paginate', async () => {
const page1 = await SELF.fetch(
'http://example.com/libros?page=1&per_page=2'
)
const json1 = await page1.json()
const page2 = await SELF.fetch(
'http://example.com/libros?page=2&per_page=2'
)
const json2 = await page2.json()
expect(json1).not.containSubset(json2)
})
})
})
89 changes: 69 additions & 20 deletions test/poems.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
import { SELF } from 'cloudflare:test'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, assert } from 'vitest'

describe('Poems Endpoints', () => {
it('list all authors', async () => {
const response = await SELF.fetch('http://example.com/autores')
expect(await response.json()).containSubset([
{
name: 'José Lezama Lima',
},
])
describe('List', () => {
it('Basic', async () => {
const response = await SELF.fetch('http://example.com/poemas')
const json = await response.json()
expect(json).containSubset([
{
title: 'LAS HORAS REGLADAS',
},
])
})
it('Should return the amount of poems determined in the per_page param', async () => {
const response = await SELF.fetch(
'http://example.com/poemas?per_page=5'
)
const json = await response.json()
expect(json).toHaveLength(5)
})
it('Should paginate', async () => {
const page1 = await SELF.fetch(
'http://example.com/poemas?page=1&per_page=2'
)
const json1 = await page1.json()

const page2 = await SELF.fetch(
'http://example.com/poemas?page=2&per_page=2'
)
const json2 = await page2.json()

expect(json1).not.containSubset(json2)
})
})
describe('Fetch', () => {
it('Find the first poem when multiple have the same title', async () => {
const response = await SELF.fetch(
'http://example.com/poema?title=dador'
)
expect(await response.json()).containSubset({
title: 'DADOR',
subindex: 1,
})
})
it('Find the subindexed poem when multiple have the same title', async () => {
const response = await SELF.fetch(
'http://example.com/poema?title=dador&subindex=3'
)
expect(await response.json()).containSubset({
title: 'DADOR',
subindex: 3,
})
})
it('Find by id', async () => {
const response = await SELF.fetch(
'http://example.com/poema?id=rec_cpkejg8v9iqcnjbc1pu0'
)
expect(await response.json()).containSubset({
title: 'AGUA DEL ESPEJO',
xata_id: 'rec_cpkejg8v9iqcnjbc1pu0',
})
})
it('Prioritize id over everything', async () => {
const response = await SELF.fetch(
'http://example.com/poema?id=rec_cpkejg8v9iqcnjbc1pu0&title=DADOR'
)
expect(await response.json()).containSubset({
title: 'AGUA DEL ESPEJO',
xata_id: 'rec_cpkejg8v9iqcnjbc1pu0',
})
})
})
// it('only list author that match a certain country', () => {
// })
// it('only list author born after x date', () => {
// })
// it('only list author born before x date', () => {
// })
// it('only list author that died after x date', () => {
// })
// it('only list author that died before x date', () => {
// })
// it('only list author lived between x-y date', () => {
// })
})

0 comments on commit ce34ba5

Please sign in to comment.