Testing Strategies

Build confidence through comprehensive testing - Master unit testing, integration testing, and mocking strategies for robust CQL applications

Testing is essential for building reliable applications. This guide covers comprehensive testing strategies for CQL applications, from unit tests to integration tests, with practical examples and best practices.

Testing Fundamentals

Testing Pyramid

CQL applications benefit from a well-structured testing pyramid:

Test Types Overview

Test Type
Speed
Isolation
Database
Purpose

Unit

Fast

High

Mocked

Business logic, validations

Integration

Medium

Medium

Test DB

Database operations, queries

End-to-End

Slow

Low

Test DB

Full application flows


Test Environment Setup

Test Database Configuration

# spec/spec_helper.cr
require "spec"
require "../src/myapp"

# Configure test database
TestDB = CQL::Schema.define(
  :test_db,
  adapter: CQL::Adapter::SQLite,
  uri: "sqlite3://:memory:") do
  # Define your schema here
  table :users do
    primary :id, Int64, auto_increment: true
    column :name, String
    column :email, String
    column :active, Bool, default: true
    column :role, String, default: "user"
    timestamps
  end

  table :posts do
    primary :id, Int64, auto_increment: true
    column :user_id, Int64
    column :title, String
    column :content, String
    column :published, Bool, default: false
    timestamps

    foreign_key :user_id, references: :users
  end
end

# Build test database schema
TestDB.build

# Test cleanup helpers
module TestHelpers
  # Clean database between tests
  def cleanup_database
    TestDB.exec("DELETE FROM posts")
    TestDB.exec("DELETE FROM users")
    # Reset auto-increment counters
    TestDB.exec("DELETE FROM sqlite_sequence") if TestDB.adapter == CQL::Adapter::SQLite
  end

  # Transaction rollback for faster cleanup
  def with_rollback(&block)
    TestDB.transaction do |tx|
      begin
        yield
      ensure
        tx.rollback
      end
    end
  end
end

# Configure Spec hooks
Spec.before_each do
  TestHelpers.cleanup_database
end

Spec.after_suite do
  TestDB.close
end

Environment-Specific Configuration


Unit Testing

Testing Model Logic

Testing Callbacks

Testing Custom Validators


Integration Testing

Database Integration Tests

Transaction Testing


Mocking and Stubbing

Database Mocking

Service Mocking


Test Data Factories

Simple Factory Pattern


Database Testing Patterns

Shared Examples

Database Cleaning Strategies


Performance Testing

Query Performance Tests


Testing Relationships

Association Testing


Testing Best Practices

Do This:

Test Organization:

Clear Test Names:

Test Data Management:

Assertions:

Avoid This:

Database Pollution:

Slow Tests:

Brittle Tests:


Testing Checklist

Model Testing Checklist

Integration Testing Checklist

Test Quality Checklist


Testing is not about finding bugs, it's about preventing them - Comprehensive testing strategies help you build confidence in your code and catch issues before they reach production.

Next Steps:

Last updated

Was this helpful?