|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { collectionQureyBuilder } from '../../src/runtime/internal/query' |
| 3 | + |
| 4 | +// Mock tables from manifest |
| 5 | +vi.mock('#content/manifest', () => ({ |
| 6 | + tables: { |
| 7 | + articles: '_articles', |
| 8 | + }, |
| 9 | +})) |
| 10 | + |
| 11 | +// Mock fetch function |
| 12 | +const mockFetch = vi.fn().mockResolvedValue(Promise.resolve([{}])) |
| 13 | +const mockCollection = 'articles' as never |
| 14 | + |
| 15 | +describe('collectionQureyBuilder', () => { |
| 16 | + beforeEach(() => { |
| 17 | + mockFetch.mockClear() |
| 18 | + }) |
| 19 | + |
| 20 | + it('builds a simple select query', async () => { |
| 21 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 22 | + await query.all() |
| 23 | + |
| 24 | + expect(mockFetch).toHaveBeenCalledWith('articles', 'SELECT * FROM _articles ORDER BY stem ASC') |
| 25 | + }) |
| 26 | + |
| 27 | + it('builds query with where clause', async () => { |
| 28 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 29 | + await query.where('title', '=', 'Test Article').all() |
| 30 | + |
| 31 | + expect(mockFetch).toHaveBeenCalledWith( |
| 32 | + 'articles', |
| 33 | + 'SELECT * FROM _articles WHERE ("title" = \'Test Article\') ORDER BY stem ASC', |
| 34 | + ) |
| 35 | + }) |
| 36 | + |
| 37 | + it('builds query with multiple where clauses', async () => { |
| 38 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 39 | + await query |
| 40 | + .where('title', '=', 'Test Article') |
| 41 | + .where('published', '=', true) |
| 42 | + .all() |
| 43 | + |
| 44 | + expect(mockFetch).toHaveBeenCalledWith( |
| 45 | + 'articles', |
| 46 | + 'SELECT * FROM _articles WHERE ("title" = \'Test Article\') AND ("published" = \'true\') ORDER BY stem ASC', |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it('builds query with IN operator', async () => { |
| 51 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 52 | + await query |
| 53 | + .where('category', 'IN', ['news', 'tech']) |
| 54 | + .all() |
| 55 | + |
| 56 | + expect(mockFetch).toHaveBeenCalledWith( |
| 57 | + 'articles', |
| 58 | + 'SELECT * FROM _articles WHERE ("category" IN (\'news\', \'tech\')) ORDER BY stem ASC', |
| 59 | + ) |
| 60 | + }) |
| 61 | + |
| 62 | + it('builds query with BETWEEN operator', async () => { |
| 63 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 64 | + await query |
| 65 | + .where('date', 'BETWEEN', ['2023-01-01', '2023-12-31']) |
| 66 | + .all() |
| 67 | + |
| 68 | + expect(mockFetch).toHaveBeenCalledWith( |
| 69 | + 'articles', |
| 70 | + 'SELECT * FROM _articles WHERE ("date" BETWEEN \'2023-01-01\' AND \'2023-12-31\') ORDER BY stem ASC', |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it('builds query with selected fields', async () => { |
| 75 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 76 | + await query |
| 77 | + .select('title', 'date', 'author') |
| 78 | + .all() |
| 79 | + |
| 80 | + expect(mockFetch).toHaveBeenCalledWith( |
| 81 | + 'articles', |
| 82 | + 'SELECT "title", "date", "author" FROM _articles ORDER BY stem ASC', |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it('builds query with order by', async () => { |
| 87 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 88 | + await query |
| 89 | + .order('date', 'DESC') |
| 90 | + .all() |
| 91 | + |
| 92 | + expect(mockFetch).toHaveBeenCalledWith( |
| 93 | + 'articles', |
| 94 | + 'SELECT * FROM _articles ORDER BY "date" DESC', |
| 95 | + ) |
| 96 | + }) |
| 97 | + |
| 98 | + it('builds query with limit without skip', async () => { |
| 99 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 100 | + await query |
| 101 | + .limit(5) |
| 102 | + .all() |
| 103 | + |
| 104 | + expect(mockFetch).toHaveBeenCalledWith( |
| 105 | + 'articles', |
| 106 | + 'SELECT * FROM _articles ORDER BY stem ASC LIMIT 5', |
| 107 | + ) |
| 108 | + }) |
| 109 | + |
| 110 | + it('builds query with limit and offset', async () => { |
| 111 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 112 | + await query |
| 113 | + .limit(10) |
| 114 | + .skip(20) |
| 115 | + .all() |
| 116 | + |
| 117 | + expect(mockFetch).toHaveBeenCalledWith( |
| 118 | + 'articles', |
| 119 | + 'SELECT * FROM _articles ORDER BY stem ASC LIMIT 10 OFFSET 20', |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it('builds query with first()', async () => { |
| 124 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 125 | + await query.first() |
| 126 | + |
| 127 | + expect(mockFetch).toHaveBeenCalledWith( |
| 128 | + 'articles', |
| 129 | + 'SELECT * FROM _articles ORDER BY stem ASC LIMIT 1', |
| 130 | + ) |
| 131 | + }) |
| 132 | + |
| 133 | + it('builds count query', async () => { |
| 134 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 135 | + await query.count() |
| 136 | + |
| 137 | + expect(mockFetch).toHaveBeenCalledWith( |
| 138 | + 'articles', |
| 139 | + 'SELECT COUNT( *) as count FROM _articles ORDER BY stem ASC', |
| 140 | + ) |
| 141 | + }) |
| 142 | + |
| 143 | + it('builds distinct count query', async () => { |
| 144 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 145 | + await query.count('author', true) |
| 146 | + |
| 147 | + expect(mockFetch).toHaveBeenCalledWith( |
| 148 | + 'articles', |
| 149 | + 'SELECT COUNT(DISTINCT author) as count FROM _articles ORDER BY stem ASC', |
| 150 | + ) |
| 151 | + }) |
| 152 | + |
| 153 | + it('builds query with complex where conditions using andWhere', async () => { |
| 154 | + const query = collectionQureyBuilder(mockCollection, mockFetch) |
| 155 | + await query |
| 156 | + .where('published', '=', true) |
| 157 | + .andWhere(group => group |
| 158 | + .where('category', '=', 'tech') |
| 159 | + .orWhere(subgroup => subgroup |
| 160 | + .where('tags', 'LIKE', '%javascript%') |
| 161 | + .where('tags', 'LIKE', '%typescript%'), |
| 162 | + ), |
| 163 | + ) |
| 164 | + .all() |
| 165 | + |
| 166 | + expect(mockFetch).toHaveBeenCalledWith( |
| 167 | + 'articles', |
| 168 | + 'SELECT * FROM _articles WHERE ("published" = \'true\') AND ("category" = \'tech\' AND ("tags" LIKE \'%javascript%\' OR "tags" LIKE \'%typescript%\')) ORDER BY stem ASC', |
| 169 | + ) |
| 170 | + }) |
| 171 | + |
| 172 | + it('builds query with path', async () => { |
| 173 | + const query = collectionQureyBuilder('articles' as never, mockFetch) |
| 174 | + await query |
| 175 | + .path('/blog/my-article') |
| 176 | + .all() |
| 177 | + |
| 178 | + expect(mockFetch).toHaveBeenCalledWith( |
| 179 | + 'articles', |
| 180 | + 'SELECT * FROM _articles WHERE ("path" = \'/blog/my-article\') ORDER BY stem ASC', |
| 181 | + ) |
| 182 | + }) |
| 183 | +}) |
0 commit comments