{"id":13413908,"url":"https://github.com/zhulongcheng/testsql","last_synced_at":"2025-03-14T20:30:47.861Z","repository":{"id":57496926,"uuid":"149873128","full_name":"zhulongcheng/testsql","owner":"zhulongcheng","description":"Generate test data from SQL files before testing and clear it after finished.","archived":false,"fork":false,"pushed_at":"2019-09-26T07:23:40.000Z","size":20,"stargazers_count":17,"open_issues_count":3,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T20:52:59.644Z","etag":null,"topics":["golang","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/zhulongcheng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-22T12:03:50.000Z","updated_at":"2023-11-01T16:13:06.000Z","dependencies_parsed_at":"2022-09-03T03:33:45.003Z","dependency_job_id":null,"html_url":"https://github.com/zhulongcheng/testsql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhulongcheng%2Ftestsql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhulongcheng%2Ftestsql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhulongcheng%2Ftestsql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhulongcheng%2Ftestsql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhulongcheng","download_url":"https://codeload.github.com/zhulongcheng/testsql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243642006,"owners_count":20323948,"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":["golang","testing"],"created_at":"2024-07-30T20:01:52.509Z","updated_at":"2025-03-14T20:30:47.568Z","avatar_url":"https://github.com/zhulongcheng.png","language":"Go","funding_links":[],"categories":["开源类库","Testing","测试","Open source library","Template Engines","Testing Frameworks","测试相关"],"sub_categories":["测试","Testing Frameworks","交流","Test","HTTP Clients","HTTP客户端","查询语"],"readme":"# TestSQL\n\n[![Build Status](https://api.travis-ci.com/zhulongcheng/testsql.svg?branch=master)](https://travis-ci.org/zhulongcheng/testsql)\n[![codecov.io](https://codecov.io/github/zhulongcheng/testsql/branch/master/graph/badge.svg)](https://codecov.io/github/zhulongcheng/testsql)\n[![Go Report Card](https://goreportcard.com/badge/github.com/zhulongcheng/testsql)](https://goreportcard.com/report/github.com/zhulongcheng/testsql)\n[![GoDoc](https://godoc.org/github.com/zhulongcheng/testsql?status.svg)](https://godoc.org/github.com/zhulongcheng/testsql)\n\n\n ![TestSQL](./docs/img/testsql.png)\n\nGenerate test data from SQL files before testing and clear it after finished.\n\n## Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API Reference](#api-reference)\n- [See also](#see-also)\n\n\n## Installation\n```\ngo get github.com/zhulongcheng/testsql\n```\n\n## Usage\nCreate a folder for the table-schema file and sql files:\n```bash\ntestsql\n├── fixtures\n│   └── users.sql\n└── schema.sql\n```\n\n The table-schema file include all tables schema, it would be like this:\n```sql\nCREATE TABLE `users` (\n    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n    `name` varchar(32) CHARACTER SET utf8mb4 NOT NULL,\n    PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n```\n\nThe sql file would be like this:\n```sql\nINSERT INTO `users` (`id`, `name`)\nVALUES\n    (1, 'foo');\n```\n\nYour tests would be like this:\n```go\nvar TS *testsql.TestSQL\n\nfunc newTS() *testsql.TestSQL {\n    dsn := \"user:password@tcp(host:port)/test_db_name\"\n    tableSchemaPath := \"testsql/schema.sql\"\n    dirPath := \"testsql/fixtures\"\n    ts := testsql.New(dsn, tableSchemaPath, dirPath)\n    return ts\n}\n\nfunc initTestDB() {\n    TS = newTS()\n    \n    // set sql-driver/orm to read/write data from TS's DSN\n    // Driver = sql.Open(TS.Config.DSN)\n    // ORM = ORM.New(TS.Config.DSN)\n}\n\nfunc TestMain(m *testing.M) {\n    initTestDB()\n    r := m.Run()\n    TS.DropTestDB()\n    os.Exit(r)\n}\n\nfunc TestUser(t *testing.T) {\n    TS.Use(\"users.sql\")\n    defer TS.Clear()\n    \n    // user := GetUserByID(1)\n    // if user.name != \"foo\" {\n    //    t.Errorf(\"not equal, expected: %s, actual: %s\", \"foo\", user.name) \n    // }\n}\n```\n\n## API Reference \n`TestSQL.Exec` generates test data from sql string.\n\n`TestSQL.Use` generates test data from sql files.\n\n`TestSQL.Clear` deletes data of all tables\n\n`TestSQL.DropTestDB` drops the database created by TestSQL.\n\n## FQA\n### Set sql-driver/orm to read/write data from TestSQL's DSN\n\n```go\n    db := yourSQLDriver.Open(TS.Config.DSN)\n    // or db := yourORM.Open(TS.Config.DSN)\n\n    yourModel.SetDB(db)  // reset db\n```\n\n\n## See also\n[Examples](https://github.com/zhulongcheng/testsql/tree/master/examples)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhulongcheng%2Ftestsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhulongcheng%2Ftestsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhulongcheng%2Ftestsql/lists"}