Skip to content

Commit

Permalink
test: add unit tests for query builder, navigation, and search
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Dec 3, 2024
1 parent 6e137e6 commit 9443f1f
Show file tree
Hide file tree
Showing 5 changed files with 927 additions and 0 deletions.
183 changes: 183 additions & 0 deletions test/unit/collectionQureyBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { collectionQureyBuilder } from '../../src/runtime/internal/query'

// Mock tables from manifest
vi.mock('#content/manifest', () => ({
tables: {
articles: '_articles',
},
}))

// Mock fetch function
const mockFetch = vi.fn().mockResolvedValue(Promise.resolve([{}]))
const mockCollection = 'articles' as never

describe('collectionQureyBuilder', () => {
beforeEach(() => {
mockFetch.mockClear()
})

it('builds a simple select query', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query.all()

expect(mockFetch).toHaveBeenCalledWith('articles', 'SELECT * FROM _articles ORDER BY stem ASC')
})

it('builds query with where clause', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query.where('title', '=', 'Test Article').all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("title" = \'Test Article\') ORDER BY stem ASC',
)
})

it('builds query with multiple where clauses', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.where('title', '=', 'Test Article')
.where('published', '=', true)
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("title" = \'Test Article\') AND ("published" = \'true\') ORDER BY stem ASC',
)
})

it('builds query with IN operator', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.where('category', 'IN', ['news', 'tech'])
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("category" IN (\'news\', \'tech\')) ORDER BY stem ASC',
)
})

it('builds query with BETWEEN operator', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.where('date', 'BETWEEN', ['2023-01-01', '2023-12-31'])
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("date" BETWEEN \'2023-01-01\' AND \'2023-12-31\') ORDER BY stem ASC',
)
})

it('builds query with selected fields', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.select('title', 'date', 'author')
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT "title", "date", "author" FROM _articles ORDER BY stem ASC',
)
})

it('builds query with order by', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.order('date', 'DESC')
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles ORDER BY "date" DESC',
)
})

it('builds query with limit without skip', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.limit(5)
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles ORDER BY stem ASC LIMIT 5',
)
})

it('builds query with limit and offset', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.limit(10)
.skip(20)
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles ORDER BY stem ASC LIMIT 10 OFFSET 20',
)
})

it('builds query with first()', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query.first()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles ORDER BY stem ASC LIMIT 1',
)
})

it('builds count query', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query.count()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT COUNT( *) as count FROM _articles ORDER BY stem ASC',
)
})

it('builds distinct count query', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query.count('author', true)

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT COUNT(DISTINCT author) as count FROM _articles ORDER BY stem ASC',
)
})

it('builds query with complex where conditions using andWhere', async () => {
const query = collectionQureyBuilder(mockCollection, mockFetch)
await query
.where('published', '=', true)
.andWhere(group => group
.where('category', '=', 'tech')
.orWhere(subgroup => subgroup
.where('tags', 'LIKE', '%javascript%')
.where('tags', 'LIKE', '%typescript%'),
),
)
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("published" = \'true\') AND ("category" = \'tech\' AND ("tags" LIKE \'%javascript%\' OR "tags" LIKE \'%typescript%\')) ORDER BY stem ASC',
)
})

it('builds query with path', async () => {
const query = collectionQureyBuilder('articles' as never, mockFetch)
await query
.path('/blog/my-article')
.all()

expect(mockFetch).toHaveBeenCalledWith(
'articles',
'SELECT * FROM _articles WHERE ("path" = \'/blog/my-article\') ORDER BY stem ASC',
)
})
})
35 changes: 35 additions & 0 deletions test/unit/decompressSQLDump.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest'
import { decompressSQLDump } from '../../src/runtime/internal/dump'

describe('decompressSQLDump', () => {
it('should decompress a gzip compressed base64 string', async () => {
// This is a gzip compressed base64 string containing "hello world"
const compressed = 'H4sIAAAAAAAAA8tIzcnJVyjPL8pJAQCFEUoNCwAAAA=='

const result = await decompressSQLDump(compressed)

expect(result).toEqual(['hello world'])
})

it('should handle empty input', async () => {
const emptyString = ''

await expect(decompressSQLDump(emptyString))
.rejects.toThrow()
})

it('should throw error on invalid base64 input', async () => {
const invalidBase64 = 'invalid-base64!'

await expect(decompressSQLDump(invalidBase64))
.rejects.toThrow()
})

it('should throw error on invalid compression format', async () => {
const compressed = 'H4sIAAAAAAAAA8tIzcnJVyjPL8pJAQCFEUoNCwAAAA=='

// @ts-expect-error Testing invalid compression type
await expect(decompressSQLDump(compressed, 'invalid-format'))
.rejects.toThrow()
})
})
Loading

0 comments on commit 9443f1f

Please sign in to comment.