Categorygithub.com/bdpiprava/testkit
repositorypackage
1.11.1
Repository: https://github.com/bdpiprava/testkit.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

Testkit - A Go Integration Testing Framework

The testkit library is a Go integration testing framework that provides utilities to set up and tear down around following:

  • PostgreSQL database
  • Kafka broker
  • Elasticsearch cluster
  • Http server

Prerequisites

  • Go (version 1.16 or later)
  • PostgreSQL

Installation

go get github.com/bdpiprava/testkit@latest

Configuration

The project uses a configuration file .testkit.config.yml to set up the PostgreSQL connection and logging level.

Example Configuration

---
log_level: trace

# PostgreSQL connection configuration
postgres:
  host: localhost:5432
  user: testkit
  password: badger
  database: testkit_db
  query_params:
    sslmode: disable

# Go migration configuration
go-migrate:
  database_name: template1
  migration_path: path/to/migrations
  fresh: true
  is_template: false

# Elasticsearch connection configuration
elasticsearch:
  addresses: http://localhost:9200 # comma separated list of addresses
  username: testkit
  password: badger

# APIMock configuration
api-mock:
  address: http://localhost:8080

Configuration Fields

FieldDescription
log_levelLog level for the testkit library. Default is info.
postgresPostgreSQL connection configuration.
go-migrateGo migration configuration.

PostgreSQL Configuration Fields

This is the configuration for the PostgreSQL connection. Ideally connection details should be provided in the configuration file. The query_params field is optional and can be used to provide additional query parameters for the PostgreSQL connection.

FieldDescription
hostPostgreSQL host and port.
userPostgreSQL user.
passwordPostgreSQL password.
databasePostgreSQL database name.
query_paramsAdditional query parameters for the PostgreSQL connection

Go Migration Configuration Fields

This is the configuration for the Go migration tool. If configured, the migration tool will run the migrations based on configuration.

FieldDescription
database_namePostgreSQL database name.
migration_pathPath to the directory containing migration files.
freshRecreate the database if exist before running migrations.
is_templateCreate the database as a template database.

Elasticsearch Configuration Fields

This is the configuration for the Elasticsearch connection.

FieldDescription
addressesComma separated list of Elasticsearch addresses.
usernameElasticsearch username.
passwordElasticsearch password.

APIMock Configuration Fields

This is the configuration for the APIMock server. It uses wiremock to mock the API responses.

FieldDescription
addressAddress of the wiremock server.

Usage

The testkit library provides a Suite struct that can be embedded in the test suite struct. The Suite struct provides the following methods to set up and tear down the resources:

PostgreSQL Helper Methods

  • RequiresPostgresDatabase - Sets up a PostgreSQL database and returns a *sqlx.DB connection.

Kafka Helper Methods

  • RequiresKafka - Sets up a Kafka cluster and returns the server address.
  • Produce - Produces a message to the Kafka topic.
  • Consume - Consumes a message from the Kafka topic on message read callback function is called. Return true from callback function to stop consuming messages.

Elasticsearch Helper Methods

  • CreateIndex - Creates an Elasticsearch index with the given name and other params.
  • DeleteIndex - Deletes the Elasticsearch index.
  • IndexExists - Checks if the Elasticsearch index exists.
  • CloseIndices - Closes the Elasticsearch indices.
  • FindIndices - Finds the Elasticsearch indices.
  • GetIndexSettings - Gets the settings of the Elasticsearch index.
  • EventuallyBlockStatus - Checks the index block for the given index.

APIMock Helper Methods

  • SetupAPIMocksFromFile - Sets up the services mock from a file and returns the URLs. It takes the file path and a map of dynamic parameters as parameters to replace the template values in the file.

Example

package example_test

import (
	"testing"

	"github.com/stretchr/testify/suite"

	"github.com/bdpiprava/testkit"
)

type ExampleTestSuite struct {
	testkit.Suite
}

func TestDatabaseIntegrationTestSuite(t *testing.T) {
	testkit.Run(t, new(ExampleTestSuite))
}

func (s *ExampleTestSuite) TestSuite_ExampleTest() {
	db := s.RequiresPostgresDatabase("test")

	var version string
	err := db.Get(&version, "SELECT VERSION()")
	s.Require().NoError(err)

	s.Require().NotEmpty(version)
	s.Contains(version, "PostgreSQL")
}