|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { CloudflareD1Adapter } from "../../../../src/lib/adapters/cloudflare/database"; |
| 3 | +import { IPreparedStatement } from "../../../../src/lib/interfaces"; |
| 4 | + |
| 5 | +describe("CloudflareD1Adapter", () => { |
| 6 | + let mockD1: D1Database; |
| 7 | + let mockStatement: D1PreparedStatement; |
| 8 | + let adapter: CloudflareD1Adapter; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + mockStatement = { |
| 12 | + bind: vi.fn().mockReturnThis(), |
| 13 | + all: vi.fn().mockResolvedValue({ results: [], success: true }), |
| 14 | + first: vi.fn().mockResolvedValue(null), |
| 15 | + run: vi.fn().mockResolvedValue({ success: true, meta: {} }) |
| 16 | + } as unknown as D1PreparedStatement; |
| 17 | + |
| 18 | + mockD1 = { |
| 19 | + prepare: vi.fn().mockReturnValue(mockStatement), |
| 20 | + batch: vi.fn().mockResolvedValue([]) |
| 21 | + } as unknown as D1Database; |
| 22 | + |
| 23 | + adapter = new CloudflareD1Adapter(mockD1); |
| 24 | + }); |
| 25 | + |
| 26 | + describe("prepare", () => { |
| 27 | + it("should prepare a statement", () => { |
| 28 | + const result = adapter.prepare("SELECT * FROM table"); |
| 29 | + |
| 30 | + expect(mockD1.prepare).toHaveBeenCalledWith("SELECT * FROM table"); |
| 31 | + expect(result).toBeDefined(); |
| 32 | + expect(result.all).toBeDefined(); |
| 33 | + expect(result.first).toBeDefined(); |
| 34 | + expect(result.run).toBeDefined(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("bind", () => { |
| 39 | + it("should bind parameters to statement", async () => { |
| 40 | + const stmt = adapter.prepare("SELECT * FROM table WHERE id = ?"); |
| 41 | + |
| 42 | + stmt.bind(1, 2, 3); |
| 43 | + |
| 44 | + expect(mockStatement.bind).toHaveBeenCalledWith(1, 2, 3); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should return statement after binding", async () => { |
| 48 | + const stmt = adapter.prepare("SELECT * FROM table"); |
| 49 | + const boundStmt = stmt.bind("param1"); |
| 50 | + |
| 51 | + expect(boundStmt).toBe(stmt); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("all", () => { |
| 56 | + it("should return all results", async () => { |
| 57 | + mockStatement.all = vi.fn().mockResolvedValue({ |
| 58 | + results: [{ id: 1 }, { id: 2 }], |
| 59 | + success: true |
| 60 | + }); |
| 61 | + |
| 62 | + const stmt = adapter.prepare("SELECT * FROM table"); |
| 63 | + const result = await stmt.all(); |
| 64 | + |
| 65 | + expect(result.results).toEqual([{ id: 1 }, { id: 2 }]); |
| 66 | + expect(result.success).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should return empty results array when no results", async () => { |
| 70 | + mockStatement.all = vi.fn().mockResolvedValue({ |
| 71 | + results: undefined, |
| 72 | + success: true |
| 73 | + }); |
| 74 | + |
| 75 | + const stmt = adapter.prepare("SELECT * FROM table"); |
| 76 | + const result = await stmt.all(); |
| 77 | + |
| 78 | + expect(result.results).toBeUndefined(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("first", () => { |
| 83 | + it("should return first result", async () => { |
| 84 | + const mockData = { id: 1, name: "test" }; |
| 85 | + mockStatement.first = vi.fn().mockResolvedValue(mockData); |
| 86 | + |
| 87 | + const stmt = adapter.prepare("SELECT * FROM table LIMIT 1"); |
| 88 | + const result = await stmt.first(); |
| 89 | + |
| 90 | + expect(result).toEqual(mockData); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should return null when no results", async () => { |
| 94 | + mockStatement.first = vi.fn().mockResolvedValue(null); |
| 95 | + |
| 96 | + const stmt = adapter.prepare("SELECT * FROM table LIMIT 1"); |
| 97 | + const result = await stmt.first(); |
| 98 | + |
| 99 | + expect(result).toBeNull(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("run", () => { |
| 104 | + it("should execute statement and return result", async () => { |
| 105 | + mockStatement.run = vi.fn().mockResolvedValue({ |
| 106 | + success: true, |
| 107 | + meta: { changes: 1, last_row_id: 123 } |
| 108 | + }); |
| 109 | + |
| 110 | + const stmt = adapter.prepare("INSERT INTO table (name) VALUES (?)"); |
| 111 | + const result = await stmt.run(); |
| 112 | + |
| 113 | + expect(result.success).toBe(true); |
| 114 | + expect(result.meta?.changes).toBe(1); |
| 115 | + expect(result.meta?.last_row_id).toBe(123); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should return success false on failure", async () => { |
| 119 | + const mockError = new Error("Database error"); |
| 120 | + mockStatement.run = vi.fn().mockResolvedValue({ |
| 121 | + success: false, |
| 122 | + error: mockError |
| 123 | + }); |
| 124 | + |
| 125 | + const stmt = adapter.prepare("INSERT INTO table (name) VALUES (?)"); |
| 126 | + const result = await stmt.run(); |
| 127 | + |
| 128 | + expect(result.success).toBe(false); |
| 129 | + expect(result.error).toBeDefined(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should return success false when error is string", async () => { |
| 133 | + mockStatement.run = vi.fn().mockResolvedValue({ |
| 134 | + success: false, |
| 135 | + error: "SQL error" |
| 136 | + }); |
| 137 | + |
| 138 | + const stmt = adapter.prepare("INSERT INTO table (name) VALUES (?)"); |
| 139 | + const result = await stmt.run(); |
| 140 | + |
| 141 | + expect(result.success).toBe(false); |
| 142 | + expect(result.error).toBe("SQL error"); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe("batch", () => { |
| 147 | + it("should execute multiple statements", async () => { |
| 148 | + const stmt1 = adapter.prepare("INSERT INTO table (name) VALUES (?)"); |
| 149 | + const stmt2 = adapter.prepare("UPDATE table SET name = ? WHERE id = ?"); |
| 150 | + |
| 151 | + const results: unknown[] = [{ success: true }, { success: true }]; |
| 152 | + mockD1.batch = vi.fn().mockResolvedValue(results); |
| 153 | + |
| 154 | + const result = await adapter.batch([stmt1, stmt2]); |
| 155 | + |
| 156 | + expect(mockD1.batch).toHaveBeenCalledWith([ |
| 157 | + expect.objectContaining({}), |
| 158 | + expect.objectContaining({}) |
| 159 | + ]); |
| 160 | + expect(result).toEqual(results); |
| 161 | + }); |
| 162 | + |
| 163 | + it("should throw error when statement is not CloudflareD1Statement", async () => { |
| 164 | + const stmt1 = adapter.prepare("SELECT * FROM table"); |
| 165 | + const invalidStmt = { |
| 166 | + bind: vi.fn(), |
| 167 | + all: vi.fn(), |
| 168 | + first: vi.fn(), |
| 169 | + run: vi.fn() |
| 170 | + } as IPreparedStatement; |
| 171 | + |
| 172 | + await expect(adapter.batch([stmt1, invalidStmt])).rejects.toThrow( |
| 173 | + "Invalid statement type for D1 batch" |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should handle empty batch array", async () => { |
| 178 | + const results: unknown[] = []; |
| 179 | + mockD1.batch = vi.fn().mockResolvedValue(results); |
| 180 | + |
| 181 | + const result = await adapter.batch([]); |
| 182 | + |
| 183 | + expect(mockD1.batch).toHaveBeenCalledWith([]); |
| 184 | + expect(result).toEqual([]); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments