storage_test_helper

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 11 Imported by: 0

README

Storage Test Helper

一、这是什么

用于辅助测试Storage的实现是否OK的测试工具,辅助提高开发效率。

二、安装依赖

go get -u github.com/storage-lock/go-storage-test-helper

三、如何使用

3.1 实现Storage

根据存储介质的特性自行实现Storage,这里就随便找一个空实现的例子,这个例子是肯定跑不过测试的,只是为了演示流程使用:

package examples

import (
	"context"
	"github.com/golang-infrastructure/go-iterator"
	"github.com/storage-lock/go-storage"
	"time"
)

type FooStorage struct {
}

var _ storage.Storage = &FooStorage{}

func (x *FooStorage) GetName() string {
	return ""
}

func (x *FooStorage) Init(ctx context.Context) error {
	return nil
}

func (x *FooStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error {
	return nil
}

func (x *FooStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error {
	return nil
}

func (x *FooStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error {
	return nil
}

func (x *FooStorage) Get(ctx context.Context, lockId string) (string, error) {
	return "", nil
}

func (x *FooStorage) GetTime(ctx context.Context) (time.Time, error) {
	return time.Now(), nil
}

func (x *FooStorage) Close(ctx context.Context) error {
	return nil
}

func (x *FooStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error) {
	return nil, nil
}

3.2 添加依赖

在你自己的Storage实现的差不多了的时候,在此项目中执行:

go get -u github.com/storage-lock/go-storage-test-helper

把此测试库添加到你自己的Storage的依赖中。

3.3 创建单元测试

为你的Storage创建一个单元测试,比如下面是测试FooStorage:

package examples

import (
	storage_test_helper "github.com/storage-lock/go-storage-test-helper"
	"testing"
)

func TestFooStorage(t *testing.T) {
	// 在要测试的Storage实现的仓库中创建好Storage,然后传递给方法测试
	storage_test_helper.TestStorage(t, &FooStorage{})
}

再看一个更贴近实际例子的,比如下面是一个MySQL Storage的单元测试:

package mysql_storage

import (
	"context"
	storage_test_helper "github.com/storage-lock/go-storage-test-helper"
	"github.com/stretchr/testify/assert"
	"os"
	"testing"
)

func TestNewMySQLStorage(t *testing.T) {
	envName := "STORAGE_LOCK_MYSQL_DSN"
	dsn := os.Getenv(envName)
	assert.NotEmpty(t, dsn)
	connectionGetter := NewMySQLConnectionManagerFromDSN(dsn)
	s, err := NewMySQLStorage(context.Background(), &MySQLStorageOptions{
		ConnectionManager: connectionGetter,
		TableName:         "storage_lock_test",
	})
	assert.Nil(t, err)
    // 重点在与这一句,把 *testing.T 和 storage.Storage 传进去 
	storage_test_helper.TestStorage(t, s)
}

保证此单元测试通过,你可以在CI中执行单元测试以保证你每次修改之后Storage都能够正常工作。

Documentation

Index

Constants

View Source
const (

	// TestDatabaseName 测试时使用到的数据库的名称
	TestDatabaseName = "storage_lock_test"

	// TestTableName 测试时使用到的表的名称
	TestTableName = "storage_lock_test"

	// TestLockId 测试时使用的锁的名称
	TestLockId = "lock_id_for_test"

	// TestLockVersion 测试时使用到的版本号
	TestLockVersion = 1

	// TestOwnerIdA 竞争锁的其中一个owner
	TestOwnerIdA = "owner_id_A"
	// TestOwnerIdB 竞争锁的另一个owner
	TestOwnerIdB = "owner_id_B"
)

单元测试统一使用的一些常量

Variables

View Source
var (
	// DefaultContextTimeout 操作需要在五分钟内完成
	DefaultContextTimeout = time.Minute * 5
)

Functions

func BuildTestLockInformation

func BuildTestLockInformation(t *testing.T, version ...storage.Version) *storage.LockInformation

BuildTestLockInformation 创建一个单元测试中使用的锁的信息

func TestEnsureLockNotExists

func TestEnsureLockNotExists(t *testing.T, s storage.Storage, lockId ...string)

TestEnsureLockNotExists 确保给定的锁在数据库中不存在,如果存在的话则将其删除

func TestStorage

func TestStorage(t *testing.T, storage storage.Storage)

TestStorage 用于测试Storage的实现是否OK

func TestStorage_Close

func TestStorage_Close(t *testing.T, storage storage.Storage)

TestStorage_Close 用于测试Storage的Close实现是否正确

func TestStorage_CreateWithVersion added in v0.0.2

func TestStorage_CreateWithVersion(t *testing.T, s storage.Storage)

func TestStorage_DeleteWithVersion

func TestStorage_DeleteWithVersion(t *testing.T, s storage.Storage)

func TestStorage_Get

func TestStorage_Get(t *testing.T, s storage.Storage)

func TestStorage_GetName

func TestStorage_GetName(t *testing.T, storage storage.Storage)

func TestStorage_GetTime

func TestStorage_GetTime(t *testing.T, s storage.Storage)

func TestStorage_Init

func TestStorage_Init(t *testing.T, storage storage.Storage)

func TestStorage_List added in v0.0.3

func TestStorage_List(t *testing.T, s storage.Storage)

func TestStorage_UpdateWithVersion

func TestStorage_UpdateWithVersion(t *testing.T, storage storage_pkg.Storage)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL