npm package diff
Package: @forge/sql
Versions: 2.2.3 - 2.3.0-next.0
File: package/out/__test__/sql.test.js
Index: package/out/__test__/sql.test.js
===================================================================
--- package/out/__test__/sql.test.js
+++ package/out/__test__/sql.test.js
@@ -20,13 +20,63 @@
const response = new node_fetch_1.Response(JSON.stringify(body), { status: 200 });
mockFetch.mockResolvedValue(response);
return body;
}
+ describe('storageApi', () => {
+ it('should send a request and return the response body', async () => {
+ mockFetchExecute([]);
+ const result = await sqlClient['storageApi']('SELECT * FROM test', [], undefined, sql_1.SQL_API_ENDPOINTS.EXECUTE);
+ expect(mockFetch).toHaveBeenCalledWith('/api/v1/execute', {
+ method: 'POST',
+ body: JSON.stringify({ query: 'SELECT * FROM test', params: [], method: 'all' }),
+ redirect: 'follow',
+ headers: { 'Content-Type': 'application/json' }
+ });
+ expect(result).toEqual({ rows: [] });
+ });
+ it('should send a request with parameters and method', async () => {
+ mockFetchExecute([]);
+ const params = [1];
+ const result = await sqlClient['storageApi']('SELECT * FROM test WHERE id = ?', params, 'one', sql_1.SQL_API_ENDPOINTS.EXECUTE);
+ expect(mockFetch).toHaveBeenCalledWith('/api/v1/execute', {
+ method: 'POST',
+ body: JSON.stringify({ query: 'SELECT * FROM test WHERE id = ?', params, method: 'one' }),
+ redirect: 'follow',
+ headers: { 'Content-Type': 'application/json' }
+ });
+ expect(result).toEqual({ rows: [] });
+ });
+ it('should send a request to DDL endpoint with method', async () => {
+ mockFetchExecute([]);
+ const result = await sqlClient['storageApi']('CREATE TABLE test (id INT)', [], 'one', sql_1.SQL_API_ENDPOINTS.EXECUTE_DDL);
+ expect(mockFetch).toHaveBeenCalledWith('/api/v1/execute/ddl', {
+ method: 'POST',
+ body: JSON.stringify({ query: 'CREATE TABLE test (id INT)', params: [], method: 'one' }),
+ redirect: 'follow',
+ headers: { 'Content-Type': 'application/json' }
+ });
+ expect(result).toEqual({ rows: [] });
+ });
+ it('should handle invalid JSON body', async () => {
+ const responseText = 'Invalid JSON';
+ const response = new node_fetch_1.Response(responseText, { status: 200 });
+ mockFetch.mockResolvedValue(response);
+ await expect(sqlClient['storageApi']('SELECT * from strange;', [], 'one', sql_1.SQL_API_ENDPOINTS.EXECUTE)).rejects.toThrow(`Unexpected error. Response was not valid JSON: ${responseText}`);
+ });
+ it('should throw ForgeSQLAPIError on API error', async () => {
+ const forgeError = { code: 'INVALID_QUERY', message: 'Invalid SQL query' };
+ const mockResponse = new node_fetch_1.Response(JSON.stringify(forgeError), {
+ status: 400
+ });
+ mockFetch.mockResolvedValue(mockResponse);
+ await expect(sqlClient['storageApi']('INVALID SQL QUERY', [], undefined, sql_1.SQL_API_ENDPOINTS.EXECUTE)).rejects.toThrow(new errors_1.ForgeSQLAPIError({ status: mockResponse.status, statusText: mockResponse.statusText }, forgeError));
+ });
+ });
describe('sendRequest', () => {
it('should send a request with the correct options and return the response', async () => {
const mockResponse = { ok: true, status: 200 };
mockFetch.mockResolvedValue(mockResponse);
- const path = 'api/v1/execute';
+ const path = '/api/v1/execute';
const options = { method: 'GET', headers: { 'Custom-Header': 'value' } };
const response = await sqlClient['sendRequest'](path, options);
expect(mockFetch).toHaveBeenCalledWith(path, {
...options,
@@ -40,9 +90,9 @@
});
it('should handle requests without options', async () => {
const mockResponse = { ok: true, status: 200 };
mockFetch.mockResolvedValue(mockResponse);
- const path = 'api/v1/execute';
+ const path = '/api/v1/execute';
const response = await sqlClient['sendRequest'](path);
expect(mockFetch).toHaveBeenCalledWith(path, {
redirect: 'follow',
headers: {
@@ -53,52 +103,13 @@
});
it('should propagate fetch errors', async () => {
const mockError = new Error('Network error');
mockFetch.mockRejectedValue(mockError);
- const path = 'api/v1/execute';
+ const path = '/api/v1/execute';
await expect(sqlClient['sendRequest'](path)).rejects.toThrow(mockError);
expect(mockFetch).toHaveBeenCalledWith(path, expect.any(Object));
});
});
- describe('storageApi', () => {
- it('should send a request and return the response body', async () => {
- mockFetchExecute([]);
- const result = await sqlClient.storageApi('SELECT * FROM test');
- expect(mockFetch).toHaveBeenCalledWith('api/v1/execute', {
- method: 'POST',
- body: JSON.stringify({ query: 'SELECT * FROM test', params: [], method: 'all' }),
- redirect: 'follow',
- headers: { 'Content-Type': 'application/json' }
- });
- expect(result).toEqual({ rows: [] });
- });
- it('should send a request with parameters and method', async () => {
- mockFetchExecute([]);
- const params = [1];
- const result = await sqlClient.storageApi('SELECT * FROM test WHERE id = ?', params, 'one');
- expect(mockFetch).toHaveBeenCalledWith('api/v1/execute', {
- method: 'POST',
- body: JSON.stringify({ query: 'SELECT * FROM test WHERE id = ?', params, method: 'one' }),
- redirect: 'follow',
- headers: { 'Content-Type': 'application/json' }
- });
- expect(result).toEqual({ rows: [] });
- });
- it('should handle invalid JSON body', async () => {
- const responseText = 'Invalid JSON';
- const response = new node_fetch_1.Response(responseText, { status: 200 });
- mockFetch.mockResolvedValue(response);
- await expect(sqlClient.storageApi('SELECT * from strange;')).rejects.toThrow(`Unexpected error. Response was not valid JSON: ${responseText}`);
- });
- it('should throw ForgeSQLAPIError on API error', async () => {
- const forgeError = { code: 'INVALID_QUERY', message: 'Invalid SQL query' };
- const mockResponse = new node_fetch_1.Response(JSON.stringify(forgeError), {
- status: 400
- });
- mockFetch.mockResolvedValue(mockResponse);
- await expect(sqlClient.storageApi('INVALID SQL QUERY')).rejects.toThrow(new errors_1.ForgeSQLAPIError({ status: mockResponse.status, statusText: mockResponse.statusText }, forgeError));
- });
- });
describe('prepare', () => {
it('should return a SqlStatement instance with query', () => {
const statement = sqlClient.prepare('INSERT INTO test VALUES (?, ?)');
expect(statement).toBeInstanceOf(sql_statement_1.SqlStatement);
@@ -155,5 +166,26 @@
mockFetch.mockResolvedValue(mockResponse);
await expect(sqlClient._provision()).rejects.toThrow('Unexpected error in provision request');
});
});
+ describe('executeDDL', () => {
+ it('should return a valid Result object when DDL query gexecuted successfully', async () => {
+ const mockResponse = {
+ ok: true,
+ status: 200,
+ text: jest.fn().mockResolvedValue(JSON.stringify({ success: true }))
+ };
+ mockFetch.mockResolvedValue(mockResponse);
+ const result = await sqlClient.executeDDL('CREATE TABLE test (id INT)');
+ expect(result).toEqual({ success: true });
+ expect(mockFetch).toHaveBeenCalledWith('/api/v1/execute/ddl', expect.objectContaining({ method: 'POST' }));
+ });
+ it('should throw ForgeSQLError when response is not valid JSON', async () => {
+ const forgeError = { code: 'INVALID_QUERY', message: 'Invalid SQL query' };
+ const mockResponse = new node_fetch_1.Response(JSON.stringify(forgeError), {
+ status: 400
+ });
+ mockFetch.mockResolvedValue(mockResponse);
+ await expect(sqlClient.executeDDL('INVALID SQL QUERY')).rejects.toThrow(new errors_1.ForgeSQLAPIError(mockResponse, forgeError));
+ });
+ });
});