|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | 2 | import { QuerybuilderTest } from '../utils' |
3 | 3 |
|
| 4 | +import { FetchTypes } from '../../src/enums' |
| 5 | + |
4 | 6 | describe('Raw Query Tests', () => { |
5 | 7 | it('raw SELECT query with fetchType ALL', async () => { |
6 | | - const result = new QuerybuilderTest().raw('SELECT * FROM testTable WHERE name = ?1', ['testName']).getQueryAll() |
| 8 | + const result = new QuerybuilderTest().raw({ |
| 9 | + query: 'SELECT * FROM testTable WHERE name = ?1', |
| 10 | + args: ['testName'], |
| 11 | + fetchType: FetchTypes.ALL, |
| 12 | + }) |
7 | 13 | expect(result.query).toEqual('SELECT * FROM testTable WHERE name = ?1') |
8 | 14 | expect(result.arguments).toEqual(['testName']) |
9 | | - expect(result.fetchType).toEqual('ALL') |
| 15 | + expect(result.fetchType).toEqual(FetchTypes.ALL) |
10 | 16 | }) |
11 | 17 |
|
12 | 18 | it('raw SELECT query with fetchType ONE', async () => { |
13 | | - const result = new QuerybuilderTest().raw('SELECT * FROM testTable WHERE id = ?1', [1]).getQueryOne() |
| 19 | + const result = new QuerybuilderTest().raw({ |
| 20 | + query: 'SELECT * FROM testTable WHERE id = ?1', |
| 21 | + args: [1], |
| 22 | + fetchType: FetchTypes.ONE, |
| 23 | + }) |
14 | 24 | expect(result.query).toEqual('SELECT * FROM testTable WHERE id = ?1') |
15 | 25 | expect(result.arguments).toEqual([1]) |
16 | | - expect(result.fetchType).toEqual('ONE') |
| 26 | + expect(result.fetchType).toEqual(FetchTypes.ONE) |
17 | 27 | }) |
18 | 28 |
|
19 | 29 | it('raw SELECT query without fetchType (default behavior)', async () => { |
20 | | - // Assuming default behavior for raw queries without explicit fetchType is to not set fetchType, |
21 | | - // or it might depend on the specific implementation (e.g., execute without returning results). |
22 | | - // For this example, let's assume it prepares the query and arguments but fetchType is undefined or a specific default. |
23 | | - const qb = new QuerybuilderTest() |
24 | | - const rawQuery = qb.raw('SELECT * FROM testTable') |
25 | | - // Depending on the library, `getQuery` might exist or you might need to call `execute` or similar |
26 | | - // For now, let's assume getQuery() would give us the prepared statement if that's how the library works |
27 | | - // or that we are testing the state of the builder before execution. |
28 | | - // This test might need adjustment based on actual library behavior for `raw()` without `getQueryAll/One()`. |
29 | | - |
30 | | - // If raw() itself doesn't imply a fetch type until getQueryAll/One/Execute is called: |
31 | | - // We'll test what raw() returns directly if it's a builder instance we can inspect. |
32 | | - // Or, if raw() is meant to be chained with getQueryAll/One, this test might be redundant |
33 | | - // with the ones above, or it tests a different aspect like "execute" |
34 | | - const result = rawQuery.getQuery() // Assuming a generic getQuery() or similar for non-fetching ops |
35 | | - |
| 30 | + const result = new QuerybuilderTest().raw({ query: 'SELECT * FROM testTable' }) |
36 | 31 | expect(result.query).toEqual('SELECT * FROM testTable') |
37 | 32 | expect(result.arguments).toBeUndefined() |
38 | | - // fetchType might be undefined or a specific default like 'NONE' or 'EXECUTE' |
39 | | - // Adjust based on actual library behavior. For now, expecting undefined. |
40 | 33 | expect(result.fetchType).toBeUndefined() |
41 | 34 | }) |
42 | 35 |
|
43 | 36 | it('raw INSERT query', async () => { |
44 | | - const result = new QuerybuilderTest() |
45 | | - .raw('INSERT INTO testTable (name, value) VALUES (?1, ?2)', ['newName', 100]) |
46 | | - .getQuery() |
| 37 | + const result = new QuerybuilderTest().raw({ |
| 38 | + query: 'INSERT INTO testTable (name, value) VALUES (?1, ?2)', |
| 39 | + args: ['newName', 100], |
| 40 | + }) |
47 | 41 | expect(result.query).toEqual('INSERT INTO testTable (name, value) VALUES (?1, ?2)') |
48 | 42 | expect(result.arguments).toEqual(['newName', 100]) |
49 | | - // INSERT operations typically don't have a fetchType like SELECT, or it might be 'EXECUTE' or undefined |
50 | | - expect(result.fetchType).toBeUndefined() // Or specific value like 'NONE' or 'EXECUTE' |
| 43 | + expect(result.fetchType).toBeUndefined() |
51 | 44 | }) |
52 | 45 |
|
53 | 46 | it('raw UPDATE query', async () => { |
54 | | - const result = new QuerybuilderTest() |
55 | | - .raw('UPDATE testTable SET name = ?1 WHERE id = ?2', ['updatedName', 2]) |
56 | | - .getQuery() |
| 47 | + const result = new QuerybuilderTest().raw({ |
| 48 | + query: 'UPDATE testTable SET name = ?1 WHERE id = ?2', |
| 49 | + args: ['updatedName', 2], |
| 50 | + }) |
57 | 51 | expect(result.query).toEqual('UPDATE testTable SET name = ?1 WHERE id = ?2') |
58 | 52 | expect(result.arguments).toEqual(['updatedName', 2]) |
59 | | - expect(result.fetchType).toBeUndefined() // Or specific value like 'NONE' or 'EXECUTE' |
| 53 | + expect(result.fetchType).toBeUndefined() |
60 | 54 | }) |
61 | 55 |
|
62 | 56 | it('raw DELETE query', async () => { |
63 | | - const result = new QuerybuilderTest().raw('DELETE FROM testTable WHERE id = ?1', [3]).getQuery() |
| 57 | + const result = new QuerybuilderTest().raw({ query: 'DELETE FROM testTable WHERE id = ?1', args: [3] }) |
64 | 58 | expect(result.query).toEqual('DELETE FROM testTable WHERE id = ?1') |
65 | 59 | expect(result.arguments).toEqual([3]) |
66 | | - expect(result.fetchType).toBeUndefined() // Or specific value like 'NONE' or 'EXECUTE' |
| 60 | + expect(result.fetchType).toBeUndefined() |
67 | 61 | }) |
68 | 62 |
|
69 | 63 | it('raw query with positional parameters (?)', async () => { |
70 | | - const result = new QuerybuilderTest() |
71 | | - .raw('SELECT * FROM anotherTable WHERE col1 = ? AND col2 = ?', ['val1', 'val2']) |
72 | | - .getQueryAll() |
| 64 | + const result = new QuerybuilderTest().raw({ |
| 65 | + query: 'SELECT * FROM anotherTable WHERE col1 = ? AND col2 = ?', |
| 66 | + args: ['val1', 'val2'], |
| 67 | + fetchType: FetchTypes.ALL, |
| 68 | + }) |
73 | 69 | expect(result.query).toEqual('SELECT * FROM anotherTable WHERE col1 = ? AND col2 = ?') |
74 | 70 | expect(result.arguments).toEqual(['val1', 'val2']) |
75 | | - expect(result.fetchType).toEqual('ALL') |
| 71 | + expect(result.fetchType).toEqual(FetchTypes.ALL) |
76 | 72 | }) |
77 | 73 | }) |
0 commit comments