Skip to content

QueryBuilder.test.js

Ilya edited this page Nov 4, 2024 · 1 revision

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.


Introduction

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 jest 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.


SELECT Operator Tests Suite

This suite tests the ability of the QueryBuilder to handle SELECT queries.


Test 1: Basic SELECT Query Request

  • Purpose: Verify that the QueryBuilder creates a basic SELECT query 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.


Table of Contents

  • Introduction
  • Test Suites
      1. SELECT Operations
      • Test: Should Create Basic SELECT Query
      • Test: Should Create SELECT * Query When No Fields Specified
      1. WHERE Conditions
      • Test: Should Create Query with WHERE Clause
  • Conclusion

Introduction

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.


Test Suites

1. SELECT Operations

This suite tests the ability of the

QueryBuilder

to handle SELECT queries.

Test: Should Create Basic SELECT Query

  • 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'.

Test: Should Create SELECT * Query When No Fields Specified

  • 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'.

2. WHERE Conditions

This suite tests the functionality of adding WHERE clauses to queries.

Test: Should Create Query with WHERE Clause

  • 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 WHERE condition using

where('age', '>', 18)

.

  • Step 5: Build the query using

build()

.

  • Assertion: The unprepared query should be 'SELECT * FROM users WHERE age > 18'.

Conclusion

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');
});

Test: SELECT * Query When no Fields Specified

  • Purpose: Ensure that when no fields are specified in the SELECT method, the query defaults to SELECT *.
  • Description:
    • A new QueryBuilder instance is created.
    • The SELECT method is called without arguments.
    • The FROM method specifies the table.
    • After building the query, the test checks that the unprepared query is SELECT * FROM 'users'.

WHERE Conditions

Test: Query with WHERE Clause

  • Purpose: Verify that a WHERE clause is correctly added to the query.
  • Description:
    • The SELECT method selects all fields.
    • The FROM method specifies the table.
    • The WHERE method adds a condition, such as 'age' > 18.
    • The query is built, and the test asserts that the unprepared query includes the WHERE clause with the specified condition.

LIMIT and OFFSET

Test 1 -- Adding LIMIT in Prepared Statements

  • Purpose: Check that the LIMIT clause is correctly added to prepared statements using a placeholder.
  • Description:
    • A QueryBuilder instance is created.
    • The SELECT method selects all fields.
    • The FROM method specifies the table.
    • The LIMIT method 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 params array is checked to ensure it contains [20].

Test: Adding LIMIT and OFFSET in Prepared Statements

  • Purpose: Ensure that both LIMIT and OFFSET clauses are correctly added to prepared statements with placeholders.
  • Description:
    • The SELECT method selects all fields.
    • The FROM method specifies the table.
    • The LIMIT method sets a limit.
    • The OFFSET method sets an offset.
    • After building the query, the test checks that the prepared query is SELECT * FROM users LIMIT ? OFFSET ?.
    • The params array should be [10, 20], matching the specified limit and offset.

Test: Setting LIMIT After OFFSET

  • Purpose: Verify that setting LIMIT after OFFSET still produces a correct SQL query.
  • Description:
    • The SELECT method selects all fields.
    • The FROM method specifies the table.
    • The OFFSET method sets an offset first.
    • The LIMIT method 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.

Conclusion

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.