Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
hedon954 committed Sep 4, 2024
2 parents 95bed7f + a020a26 commit 97d0e84
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest]
go-version: ['1.22']
go-version: ['1.20']
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4
Expand Down
9 changes: 0 additions & 9 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ linters-settings:
line-length: 140
misspell:
locale: US
mnd:
checks: # "argument", "case", "condition", "operation", "return", "assign"
- case
- return
- assign
- condition
- operation
stylecheck:
checks:
- ST1000
Expand Down Expand Up @@ -73,7 +66,6 @@ linters:
- gocyclo
- gofmt
- goimports
- mnd
- goprintffuncname
- gosec
- gosimple
Expand Down Expand Up @@ -104,7 +96,6 @@ issues:
exclude-rules:
- path: _test\.go
linters:
- gomnd
- funlen
exclude-dirs:
- vendor
Expand Down
13 changes: 6 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ repos:
rev: v0.5.1
hooks:
- id: go-mod-tidy # run go mod tidy -v.
- repo: local
- repo: https://github.com/golangci/golangci-lint # golangci-lint hook repo
rev: v1.54.2 # golangci-lint hook repo revision
hooks:
- id: golangci-lint
name: golangci-lint
description: run golangci-lint
entry: golangci-lint run -v ./...
language: golang
files: \.*$
pass_filenames: false
stages: [ pre-commit ]
language_version: 1.20.14
- repo: local
hooks:
- id: go-unit-tests
name: go-unit-tests
description: run go tests with race detector
Expand Down
40 changes: 21 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
# Changelog

---
## [1.0.1](https://github.com/hedon-go-road/template-web/compare/v1.0.0..v1.0.1) - 2024-08-22
## [1.0.3](https://github.com/hedon-go-road/template-web/compare/v0.0.1..v1.0.3) - 2024-08-27

1. rename project to `go-mysql-mocker`;
2. improve unit tests for `InitData`;
3. auto get unused port from local machine if not specify.
publish stable version

### 📚 Documentation

- **(changelog)** delete useless records - ([2a2af4c](https://github.com/hedon-go-road/template-web/commit/2a2af4c9c061f9237e0828c83cfbe41f4b0e4494)) - hedon954
- **(readme)** add go reference badge - ([1c05472](https://github.com/hedon-go-road/template-web/commit/1c05472ad6bf701f9efdc5be0216749e36051775)) - hedon954
- **(readme)** update installation - ([9ab61b9](https://github.com/hedon-go-road/template-web/commit/9ab61b9e765491eb543234832ba5969fc2d03caf)) - Hedon
- **(readme)** update installation - ([2551f4b](https://github.com/hedon-go-road/template-web/commit/2551f4b19cfb5d5e0cb90d2a00457923eedec303)) - hedon954

<!-- generated by git-cliff -->

---
## [0.0.1] - 2024-08-23

Finish go-mysql-mocker basic features and support for go1.20 projects

### ⛰️ Features

- finish gmm - ([ea8f4a0](https://github.com/hedon-go-road/template-web/commit/ea8f4a0fe2cd0320ea3cc78a637338de72b8c4e9)) - wangjiahan
- remove some useless error check and improve unit test for `InitData` - ([280eb23](https://github.com/hedon-go-road/template-web/commit/280eb2396addc2d13fd7acb4164298e507788e08)) - hedon954

### 🐛 Bug Fixes
Expand All @@ -19,24 +32,13 @@

- **(gmm)** add some comments for get free port - ([572fb4c](https://github.com/hedon-go-road/template-web/commit/572fb4c5b728c7057ec07b8a11ae2b7e584a5153)) - hedon954
- **(readme)** fix wrong - ([ac3e0fb](https://github.com/hedon-go-road/template-web/commit/ac3e0fb95ff9dc2d348b29e28223ded4f04f42ae)) - hedon954
- **(readme)** add requirements note - ([abfe473](https://github.com/hedon-go-road/template-web/commit/abfe4738e2f8219cd055dc2ea9f9022914ca0d91)) - wangjiahan
- add readme - ([b9cd37a](https://github.com/hedon-go-road/template-web/commit/b9cd37a411ce52151540c6d328908c0d63e07a71)) - hedon954

### 🚜 Refactor

- rename repository to go-mysql-mocker - ([599dbd0](https://github.com/hedon-go-road/template-web/commit/599dbd0eb47cadf0a485076ee78ae1a8e71a1b9c)) - hedon954

<!-- generated by git-cliff -->

---
## [1.0.0] - 2024-08-22

finish gmm(go-mysql-mocker) basic features

### ⛰️ Features

- finish gmm - ([ea8f4a0](https://github.com/hedon-go-road/template-web/commit/ea8f4a0fe2cd0320ea3cc78a637338de72b8c4e9)) - wangjiahan

### 📚 Documentation

- add readme - ([b9cd37a](https://github.com/hedon-go-road/template-web/commit/b9cd37a411ce52151540c6d328908c0d63e07a71)) - hedon954
- do not use slog to support lower go version user - ([fc235aa](https://github.com/hedon-go-road/template-web/commit/fc235aa48aa10d3f7451c910acabc011b58e7370)) - wangjiahan
- downgrade go-mysql-server to 0.18.0 to support go1.20 projects - ([f337dc8](https://github.com/hedon-go-road/template-web/commit/f337dc8301df31924d1a920f04c8ec2afcf92a8c)) - wangjiahan

<!-- generated by git-cliff -->
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
[![codecov](https://codecov.io/gh/hedon954/go-mysql-mocker/graph/badge.svg?token=RtwHYWTrso)](https://codecov.io/gh/hedon954/go-mysql-mocker)
[![CI](https://github.com/hedon954/go-mysql-mocker/workflows/build/badge.svg)](https://github.com/hedon954/go-mysql-mocker/actions)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/hedon954/go-mysql-mocker?sort=semver)](https://github.com/hedon954/go-mysql-mocker/releases)
[![Go Reference](https://pkg.go.dev/badge/github.com/hedon954/go-mysql-mocker.svg)](https://pkg.go.dev/github.com/hedon954/go-mysql-mocker@v0.0.1)


`go-mysql-mocker(gmm)` was created to provide developers with a reliable and straightforward tool for mocking MySQL interactions in Go applications, particularly for automated testing environments. By simulating a MySQL server, `go-mysql-mocker` allows developers to conduct integration tests without the overhead of setting up a real database, thus speeding up test cycles and reducing external dependencies. This is especially useful in continuous integration (CI) environments where test reliability and speed are paramount.
`go-mysql-mocker(gmm)` was created to provide developers with a reliable and straightforward tool for mocking MySQL interactions in Go applications, particularly for automated testing environments. By simulating a MySQL server, `go-mysql-mocker` allows developers to conduct unit tests without the overhead of setting up a real database, thus speeding up test cycles and reducing external dependencies. This is especially useful in continuous integration (CI) environments where test reliability and speed are paramount.

![architecture](./design/img/architecture.png)

Expand All @@ -21,6 +22,10 @@
- Uses in-memory storage to speed up tests.
- Automatic table creation and data seeding using GORM models or SQL statements.

## Requirements

- Go 1.20 (or later versions)

## Installation

```bash
Expand Down
20 changes: 10 additions & 10 deletions gmm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"net"
"os"
"reflect"
"strconv"

"github.com/dolthub/go-mysql-server/server"
"github.com/google/uuid"
"golang.org/x/exp/slog"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
Expand Down Expand Up @@ -85,7 +85,7 @@ func (b *GMMBuilder) Build() (sDB *sql.DB, gDB *gorm.DB, shutdown func(), err er
}

// Start mysql server
slog.Info("start go mysql mocker server, listening at 127.0.0.1:" + strconv.Itoa(b.port))
log.Print("start go mysql mocker server, listening at 127.0.0.1:" + strconv.Itoa(b.port))
go func() {
if err := b.server.Start(); err != nil {
panic(err)
Expand Down Expand Up @@ -179,22 +179,22 @@ func (b *GMMBuilder) initTables() {
return
}

slog.Info("start to init tables, count = " + strconv.Itoa(len(b.tables)))
log.Print("start to init tables, count = " + strconv.Itoa(len(b.tables)))
for _, table := range b.tables {
if err := b.gormDB.AutoMigrate(table); err != nil {
b.err = fmt.Errorf("failed to auto migrate(type=%T): %w", table, err)
return
}
}
slog.Info("init tables successfully, count = " + strconv.Itoa(len(b.tables)))
log.Print("init tables successfully, count = " + strconv.Itoa(len(b.tables)))
}

func (b *GMMBuilder) initWithModels() {
if b.err != nil || len(b.models) == 0 {
return
}

slog.Info("start to init data with models, count = " + strconv.Itoa(len(b.models)))
log.Print("start to init data with models, count = " + strconv.Itoa(len(b.models)))
for _, model := range b.models {
if err := b.gormDB.AutoMigrate(model); err != nil {
b.err = fmt.Errorf("failed to auto migrate(type=%T): %w", model, err)
Expand All @@ -205,14 +205,14 @@ func (b *GMMBuilder) initWithModels() {
return
}
}
slog.Info("init data with models successfully, count = " + strconv.Itoa(len(b.models)))
log.Print("init data with models successfully, count = " + strconv.Itoa(len(b.models)))
}

func (b *GMMBuilder) initWithStmts() {
if b.err != nil || len(b.sqlStmts) == 0 {
return
}
slog.Info("start to init data with sql stmts, count = " + strconv.Itoa(len(b.sqlStmts)))
log.Print("start to init data with sql stmts, count = " + strconv.Itoa(len(b.sqlStmts)))
for _, stmt := range b.sqlStmts {
stmts, err := splitSQLStatements(stmt)
if err != nil {
Expand All @@ -224,14 +224,14 @@ func (b *GMMBuilder) initWithStmts() {
return
}
}
slog.Info("init data with sql stmts successfully, count = " + strconv.Itoa(len(b.sqlStmts)))
log.Print("init data with sql stmts successfully, count = " + strconv.Itoa(len(b.sqlStmts)))
}

func (b *GMMBuilder) initWithFiles() {
if b.err != nil || len(b.sqlFiles) == 0 {
return
}
slog.Info("start to init data with sql files, count = " + strconv.Itoa(len(b.sqlFiles)))
log.Print("start to init data with sql files, count = " + strconv.Itoa(len(b.sqlFiles)))
for _, file := range b.sqlFiles {
stmts, err := splitSQLFile(file)
if err != nil {
Expand All @@ -243,7 +243,7 @@ func (b *GMMBuilder) initWithFiles() {
return
}
}
slog.Info("init data with sql files successfully, count = " + strconv.Itoa(len(b.sqlFiles)))
log.Print("init data with sql files successfully, count = " + strconv.Itoa(len(b.sqlFiles)))
}

func (b *GMMBuilder) executeSQLStatements(stmts []string) error {
Expand Down
13 changes: 11 additions & 2 deletions gmm/server.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package gmm

import (
"context"
"fmt"

sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/vitess/go/mysql"
)

func createMySQLServer(dbName string, port int) (*server.Server, error) {
// create a new database
db := memory.NewDatabase(dbName)
db.BaseDatabase.EnablePrimaryKeyIndexes()

// create a new server engine
pro := memory.NewDBProvider(db)
engine := sqle.NewDefault(pro)

config := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("127.0.0.1:%d", port),
}
s, err := server.NewServer(config, engine, memory.NewSessionBuilder(pro), nil)

// create a new server
s, err := server.NewServer(config, engine, func(ctx context.Context, c *mysql.Conn, addr string) (sql.Session, error) {
session := memory.NewSession(sql.NewBaseSession(), pro)
return session, nil
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to create server: %w", err)
}
Expand Down
23 changes: 12 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/hedon954/go-mysql-mocker

go 1.22
go 1.20

require (
github.com/dolthub/go-mysql-server v0.18.1
github.com/dolthub/go-mysql-server v0.18.0
github.com/dolthub/vitess v0.0.0-20240228192915-d55088cef56a
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.11
gorm.io/gorm v1.25.7
)

require (
Expand All @@ -18,26 +18,27 @@ require (
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e // indirect
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
github.com/dolthub/vitess v0.0.0-20240404214255-c5a87fc7b325 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d // indirect
github.com/gocraft/dbr/v2 v2.7.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tetratelabs/wazero v1.1.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
Loading

0 comments on commit 97d0e84

Please sign in to comment.