{"id":24499034,"url":"https://github.com/hedon954/go-mysql-mocker","last_synced_at":"2025-04-14T05:33:30.930Z","repository":{"id":254273618,"uuid":"845522246","full_name":"hedon954/go-mysql-mocker","owner":"hedon954","description":"gmm(go-mysql-mocker) is a simple but useful tool to mock mysql server in Go project, it is especially useful in unit testing. This tool uses dolthub/go-mysql-server as memory server and gorm to auto create table and init data.","archived":false,"fork":false,"pushed_at":"2025-03-07T14:50:17.000Z","size":225,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T19:21:30.408Z","etag":null,"topics":["database","go-mock-testing","go-mocking","go-mysql","go-mysql-server","go-unit-testing","golang","mock","mock-data","mock-server","mock-test","mysql-server","mysql-test","unit-test","unit-test-framework","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hedon954.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-21T12:15:10.000Z","updated_at":"2025-03-07T14:50:15.000Z","dependencies_parsed_at":"2024-08-27T07:44:45.075Z","dependency_job_id":"23cbc3a8-6feb-4266-a405-f0196013eb35","html_url":"https://github.com/hedon954/go-mysql-mocker","commit_stats":null,"previous_names":["hedon954/gmm","hedon954/go-mysql-mocker"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedon954%2Fgo-mysql-mocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedon954%2Fgo-mysql-mocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedon954%2Fgo-mysql-mocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hedon954%2Fgo-mysql-mocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hedon954","download_url":"https://codeload.github.com/hedon954/go-mysql-mocker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248827483,"owners_count":21167886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","go-mock-testing","go-mocking","go-mysql","go-mysql-server","go-unit-testing","golang","mock","mock-data","mock-server","mock-test","mysql-server","mysql-test","unit-test","unit-test-framework","unit-testing"],"created_at":"2025-01-21T22:12:57.438Z","updated_at":"2025-04-14T05:33:30.920Z","avatar_url":"https://github.com/hedon954.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-mysql-mocker(gmm)\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/hedon954/go-mysql-mocker)](https://goreportcard.com/report/github.com/hedon954/go-mysql-mocker)\n[![codecov](https://codecov.io/gh/hedon954/go-mysql-mocker/graph/badge.svg?token=RtwHYWTrso)](https://codecov.io/gh/hedon954/go-mysql-mocker)\n[![CI](https://github.com/hedon954/go-mysql-mocker/workflows/build/badge.svg)](https://github.com/hedon954/go-mysql-mocker/actions)\n[![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)\n[![Go Reference](https://pkg.go.dev/badge/github.com/hedon954/go-mysql-mocker.svg)](https://pkg.go.dev/github.com/hedon954/go-mysql-mocker)\n\n`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.\n\n![architecture](./design/img/architecture.png)\n\n## Dependencies\n\n- [go-mysql-server](https://github.com/dolthub/go-mysql-server): used for MySQL memory server\n- [gorm](https://github.com/go-gorm/gorm): used for creating table and initializing data\n\n## Features\n\n- Easy setup and integration with Go projects.\n- Uses in-memory storage to speed up tests.\n- Automatic table creation and data seeding using GORM models or SQL statements.\n\n## Requirements\n\n- Go 1.20 (\u003c=v1.0.6)\n- Go 1.24 (\u003e=v1.0.7)\n\n## Installation\n\n```bash\ngo get -u github.com/hedon954/go-mysql-mocker\n```\n\n## Usage\n\n- `Port(port int)`: Sets the MySQL server port to the specified value. This method allows you to customize the port on which the MySQL server listens.\n- `CreateTable(model)`: Creates a database table based on the schema defined in the provided model. This method facilitates the automatic creation of tables directly from GORM models.\n- `InitData(data)`: Populates the database with test data using instances of Go structs. This method supports initializing data from single instances or slices of instances that implement the schema.Tabler interface.\n- `SQLStmts(stmts)`: Executes the provided SQL statements to generate test data. This method allows for direct insertion or modification of data using raw SQL commands.\n- `SQLFiles(files)`: Reads SQL statements from specified files and executes them to generate test data. This method is useful for initializing the database with a larger set of pre-defined SQL operations.\n- `Build()`: Initializes and starts the MySQL server with all specified configurations and initializes the data. This method must be called to execute the configurations set up by the preceding methods.\n\n## Quick Start\n\n### prepare a data model and implement `TableName()` interface\n\n```go\ntype UserState struct {\n\tUID   string `gorm:\"primaryKey;column:uid\"`\n\tState string `gorm:\"column:state\"`\n}\n\nfunc (u UserState) TableName() string {\n\treturn \"user_state\"\n}\n```\n\n### write a business logic\n\n```go\nfunc ChangeUserStateToMatch(db *sql.DB, uid string) (int64, error) {\n\tres, err := db.Exec(\"UPDATE user_state SET state = 'match' WHERE uid = ?\", uid)\n\tif err != nil {\n\t\treturn 0, err\n\t}\n\treturn res.RowsAffected()\n}\n```\n\n### use `gmm` to test it\n\n```go\nfunc TestChangeUserStateToMatch(t *testing.T) {\n\tt.Run(\"no data should have no affect row\", func(t *testing.T) {\n\t\tdb, _, shutdown, err := gmm.Builder(\"db-name\").Port(20201).CreateTable(UserState{}).Build()\n\t\tassert.Nil(t, err)\n\t\tdefer shutdown()\n\t\tres, err := ChangeUserStateToMatch(db, \"1\")\n\t\tassert.Nil(t, err)\n\t\tassert.Equal(t, int64(0), res)\n\t})\n\n\tt.Run(\"has uid 1 should affect 1 row and change state to `match`\", func(t *testing.T) {\n\t\t// prepare db and init data\n\t\torigin := UserState{State: \"no-match\", UID: \"1\"}\n\t\tdb, gDB, shutdown, err := gmm.Builder().CreateTable(UserState{}).InitData(\u0026origin).Build()\n\t\tassert.Nil(t, err)\n\t\tdefer shutdown()\n\n\t\t// check before change state\n\t\tvar before = UserState{}\n\t\tassert.Nil(t, gDB.Select(\"uid\", \"state\").Where(\"uid=?\", \"1\").Find(\u0026before).Error)\n\t\tassert.Equal(t, origin, before)\n\n\t\t// run biz logic\n\t\tres, err := ChangeUserStateToMatch(db, \"1\")\n\t\tassert.Nil(t, err)\n\t\tassert.Equal(t, int64(1), res)\n\n\t\t// check after change state\n\t\tvar after = UserState{}\n\t\tassert.Nil(t, gDB.Select(\"uid\", \"state\").Where(\"uid=?\", \"1\").Find(\u0026after).Error)\n\t\tassert.Equal(t, UserState{\n\t\t\tUID:   \"1\",\n\t\t\tState: \"match\",\n\t\t}, after)\n\t})\n}\n```\n\nFor more detail, please see [examples](./examples) folder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedon954%2Fgo-mysql-mocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhedon954%2Fgo-mysql-mocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhedon954%2Fgo-mysql-mocker/lists"}