-
Notifications
You must be signed in to change notification settings - Fork 0
QueryBuilder.test.js
QueryBuilder Tests Documentation
.../QueryBuilder.test.js
This document provides an overview of the unit tests written for the QueryBuilder class in the QueryBuilder.test.js file. These tests verify that the QueryBuilder correctly constructs SQL queries based on the methods and parameters used.
The QueryBuilder class aims to provide a safe and efficient way to construct SQL queries, minimizing the risk of SQL injection and syntax errors. Proper testing is essential to ensure that it behaves as expected under different conditions.
All manipulations presented in this document were designed, constructed and launched using v29.7.0 package
Note
This document provides an overview of the unit tests written for the QueryBuilder class using Jest. The QueryBuilder class is designed to build SQL queries programmatically, ensuring correctness and security. The tests are organized into sections based on the functionality being tested.
This suite tests the ability of the QueryBuilder to handle SELECT queries.
-
Purpose: Verify that the
QueryBuildercreates a basicSELECTquery request with specified fields.- # Documentation for
QueryBuilder.test.js
This document provides an overview of the unit tests written for the
QueryBuilder
class in the
QueryBuilder.test.js
file. These tests verify that the
QueryBuilder
correctly constructs SQL queries based on the methods and parameters used.
- Introduction
- Test Suites
-
- SELECT Operations
- Test: Should Create Basic SELECT Query
- Test: Should Create SELECT * Query When No Fields Specified
-
- WHERE Conditions
- Test: Should Create Query with WHERE Clause
-
- Conclusion
The
QueryBuilder
class provides methods to construct SQL queries programmatically. The tests in
QueryBuilder.test.js
aim to ensure that each method works as expected and that the queries produced are accurate and syntactically correct.
This suite tests the ability of the
QueryBuilder
to handle SELECT queries.
- Purpose: Verify that the
QueryBuilder
can create a basic SELECT query with specified fields.
-
Code:
test('should create basic SELECT query', () => { const query = new QueryBuilder() .select('id', 'name') .from('users') .build(); expect(query.unprepared).toBe('SELECT id, name FROM users'); });
-
Description:
- Step 1: Create a new instance of
QueryBuilder
.
- Step 2: Use the
select('id', 'name')
method to specify the fields.
- Step 3: Specify the table with
from('users')
.
- Step 4: Build the query using
build()
.
-
Assertion: The unprepared query should be
'SELECT id, name FROM users'.
- Purpose: Ensure that the
QueryBuilder
defaults to SELECT * when no fields are specified.
-
Code:
test('should create SELECT * query when no fields specified', () => { const query = new QueryBuilder() .select() .from('users') .build(); expect(query.unprepared).toBe('SELECT * FROM users'); });
-
Description:
- Step 1: Create a new instance of
QueryBuilder
.
- Step 2: Call
select()
with no arguments.
- Step 3: Specify the table with
from('users')
.
- Step 4: Build the query using
build()
.
-
Assertion: The unprepared query should be
'SELECT * FROM users'.
This suite tests the functionality of adding WHERE clauses to queries.
- Purpose: Verify that the
QueryBuilder
adds a WHERE clause correctly.
-
Code:
test('should create query with WHERE clause', () => { const query = new QueryBuilder() .select('*') .from('users') .where('age', '>', 18) .build(); expect(query.unprepared).toBe('SELECT * FROM users WHERE age > 18'); });
-
Description:
- Step 1: Create a new instance of
QueryBuilder
.
- Step 2: Use
select('*')
to select all fields.
- Step 3: Specify the table with
from('users')
.
-
Step 4: Add a
WHEREcondition using
where('age', '>', 18)
.
- Step 5: Build the query using
build()
.
-
Assertion: The unprepared query should be
'SELECT * FROM users WHERE age > 18'.
The tests in
QueryBuilder.test.js
ensure that the
QueryBuilder
class functions correctly for constructing basic SELECT queries and adding WHERE clauses. By verifying the unprepared queries, the tests confirm that the SQL statements generated match the expected syntax and structure.
These tests are crucial for maintaining the reliability of the
QueryBuilder
, especially when integrating it into larger applications where dynamic SQL query generation is required.
Note: Additional tests should be written to cover more complex scenarios, such as joins, aggregations, grouping, ordering, and handling of different SQL clauses to fully validate the robustness of the QueryBuilder class.
- [x] The SELECT method is called with specific fields, such as 'id' and 'name'.
- [x] The FROM method specifies the table, e.g., 'users'.
- [x] The .build() method constructs the query.
- [x] The test asserts that the unprepared query matches the expected SQL statement: SELECT 'id', 'name' FROM 'users'.
test('Create basic SELECT query', () => {
const query = new QueryBuilder()
.select('id', 'name')
.from('users')
.build();
expect(query.unprepared).toBe('SELECT id, name FROM users');
});-
Purpose: Ensure that when no fields are specified in the
SELECTmethod, the query defaults toSELECT *. -
Description:
- A new
QueryBuilderinstance is created. - The
SELECTmethod is called without arguments. - The
FROMmethod specifies the table. - After building the query, the test checks that the unprepared query is
SELECT * FROM 'users'.
- A new
-
Purpose: Verify that a
WHEREclause is correctly added to the query. -
Description:
- The
SELECTmethod selects all fields. - The
FROMmethod specifies the table. - The
WHEREmethod adds a condition, such as'age' > 18. - The query is built, and the test asserts that the unprepared query includes the
WHEREclause with the specified condition.
- The
-
Purpose: Check that the
LIMITclause is correctly added to prepared statements using a placeholder. -
Description:
- A
QueryBuilderinstance is created. - The
SELECTmethod selects all fields. - The
FROMmethod specifies the table. - The
LIMITmethod sets a limit, e.g.,20. - The
.build()method constructs the query. - The test verifies that the prepared query is
SELECT * FROM users LIMIT ?. - The
paramsarray is checked to ensure it contains[20].
- A
-
Purpose: Ensure that both
LIMITandOFFSETclauses are correctly added to prepared statements with placeholders. -
Description:
- The
SELECTmethod selects all fields. - The
FROMmethod specifies the table. - The
LIMITmethod sets a limit. - The
OFFSETmethod sets an offset. - After building the query, the test checks that the prepared query is
SELECT * FROM users LIMIT ? OFFSET ?. - The
paramsarray should be[10, 20], matching the specified limit and offset.
- The
-
Purpose: Verify that setting
LIMITafterOFFSETstill produces a correct SQL query. -
Description:
- The
SELECTmethod selects all fields. - The
FROMmethod specifies the table. - The
OFFSETmethod sets an offset first. - The
LIMITmethod sets a limit afterward. - The query is built.
- The test asserts that the unprepared query is
SELECT * FROM users LIMIT 10 OFFSET 20, confirming that the order of method calls does not affect the final SQL syntax.
- The
The tests in QueryBuilder.test.js validate the functionality of the QueryBuilder class, ensuring that it correctly builds SQL queries for various scenarios. By testing different combinations of methods and parameters, these tests help maintain the reliability and correctness of the query-building process.