{"id":21316097,"url":"https://github.com/cjsaylor/sqlfixture","last_synced_at":"2026-05-18T22:05:38.300Z","repository":{"id":57598994,"uuid":"92346679","full_name":"cjsaylor/sqlfixture","owner":"cjsaylor","description":"Go library that enables simple pre-populating a MySQL database with data to be used during testing","archived":false,"fork":false,"pushed_at":"2017-05-26T00:55:13.000Z","size":121,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T10:36:28.681Z","etag":null,"topics":["fixtures","go","golang","mysql","unittest"],"latest_commit_sha":null,"homepage":null,"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/cjsaylor.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":"2017-05-24T23:59:04.000Z","updated_at":"2022-10-19T07:10:24.000Z","dependencies_parsed_at":"2022-09-26T19:53:35.764Z","dependency_job_id":null,"html_url":"https://github.com/cjsaylor/sqlfixture","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/cjsaylor%2Fsqlfixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjsaylor%2Fsqlfixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjsaylor%2Fsqlfixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cjsaylor%2Fsqlfixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cjsaylor","download_url":"https://codeload.github.com/cjsaylor/sqlfixture/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243791866,"owners_count":20348532,"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":["fixtures","go","golang","mysql","unittest"],"created_at":"2024-11-21T18:29:41.077Z","updated_at":"2026-05-18T22:05:38.152Z","avatar_url":"https://github.com/cjsaylor.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlfixture\n\n[![GoDoc](https://godoc.org/github.com/cjsaylor/sqlfixture?status.png)](https://godoc.org/github.com/cjsaylor/sqlfixture)\n[![Build Status](https://travis-ci.org/cjsaylor/sqlfixture.svg?branch=master)](https://travis-ci.org/cjsaylor/sqlfixture)\n\nsqlfixture is a go library that enables simple pre-populating a MySQL database with data\nto be used during testing.\n\nFixtures are supported via:\n\n* Native `go` code\n* `json` files\n* `yaml` files\n\n# Example\n\nThis example will truncate `my_table` and `my_other_table` and insert data into them\nbefore each test is run.\n\n\u003e In foo_test.go\n\n```go\npackage foo_test\n\nimport (\n\t\"database/sql\"\n\t\"os\"\n\t\"testing\"\n\n\t\"github.com/cjsaylor/sqlfixture\"\n\t_ \"github.com/go-sql-driver/mysql\"\n)\n\nfunc TestMain(m *testing.M) {\n\tdb, _ := sql.Open(\"mysql\", \"tcp(localhost:3306)/test\")\n\terr := db.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tsetup(db)\n\tcode := m.Run()\n\tos.Exit(code)\n}\n\nfunc setup(db *sql.DB) {\n\t// Setup your table schema here\n\tfixture := sqlfixture.New(db, sqlfixture.Tables{\n\t\tsqlfixture.Table{\n\t\t\tName: \"my_table\",\n\t\t\tRows: sqlfixture.Rows{\n\t\t\t\tsqlfixture.Row{\n\t\t\t\t\t\"id\": \"1\",\n\t\t\t\t\t\"name\": \"Some value\",\n\t\t\t\t\t\"slug\": \"some-value\",\n\t\t\t\t\t\"date\": \"2017-05-15 00:00:00\",\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tsqlfixture.Table{\n\t\t\tName: \"my_other_table\",\n\t\t\tRows: sqlfixture.Rows{\n\t\t\t\tsqlfixture.Row{\n\t\t\t\t\t\"id\": \"1\",\n\t\t\t\t\t\"item\": \"Some item\",\n\t\t\t\t\t\"quantity\": 9,\n\t\t\t\t},\n\t\t\t\tsqlfixture.Row{\n\t\t\t\t\t\"id\": \"2\",\n\t\t\t\t\t\"item\": \"Some other item\",\n\t\t\t\t\t\"quantity\": 3,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t})\n\tfixture.Populate()\n}\n```\n\n# Installation\n\n```\ngo get github.com/cjsaylor/sqlfixture\n```\n\n## YAML support\n\nYou can import fixtures from YAML files.\n\nWe can rewrite the example above:\n\n\u003e In foo_test.go\n\n```go\npackage foo_test\n\nimport (\n\t\"database/sql\"\n\t\"io/ioutil\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/cjsaylor/sqlfixture\"\n\t_ \"github.com/go-sql-driver/mysql\"\n)\n\nfunc TestMain(m *testing.M) {\n\tdb, _ := sql.Open(\"mysql\", \"tcp(localhost:3306)/test\")\n\terr := db.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tsetup(db)\n\tcode := m.Run()\n\tos.Exit(code)\n}\n\nfunc setup(db *sql.DB) {\n\t// Setup your table schema here\n\tfilename, _ := filepath.Abs(\"./fixtures/test.yaml\")\n\tyamlFile, err := ioutil.ReadFile(filename)\n\tif err != nil {\n\t\tt.Error(\"Unable to find fixture file.\")\n\t\treturn\n\t}\n\tfixture := sqlfixture.FromYAML(db, yamlFile)\n\tfixture.Populate()\n}\n```\n\n\u003e in fixtures/test.yaml\n\n```yaml\n- name: my_table\n  rows:\n    - id: 1\n      name: \"some value\"\n      slug: \"some-value\"\n      date: \"2017-05-15 00:00:00\"\n- name: my_other_table\n  rows:\n    - id: 1\n      item: some item\n      quantity: 9\n    - id: 2\n      item: some other item\n      quantity: 3\n```\n\n## JSON Support\n\nSimilar to the `YAML` support, sqlfixture also support importing data via `JSON`:\n\n\u003e `foo_test.go`\n\n```go\npackage foo_test\n\nimport (\n\t\"database/sql\"\n\t\"io/ioutil\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"testing\"\n\n\t\"github.com/cjsaylor/sqlfixture\"\n\t_ \"github.com/go-sql-driver/mysql\"\n)\n\nfunc TestMain(m *testing.M) {\n\tdb, _ := sql.Open(\"mysql\", \"tcp(localhost:3306)/test\")\n\terr := db.Ping()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tsetup(db)\n\tcode := m.Run()\n\tos.Exit(code)\n}\n\nfunc setup(db *sql.DB) {\n\t// Setup your table schema here\n\tfilename, _ := filepath.Abs(\"./fixtures/test.json\")\n\tjsonFile, err := ioutil.ReadFile(filename)\n\tif err != nil {\n\t\tt.Error(\"Unable to find fixture file.\")\n\t\treturn\n\t}\n\tfixture := sqlfixture.FromJSON(db, jsonFile)\n\tfixture.Populate()\n}\n```\n\n\u003e `fixtures/test.json`\n```json\n[\n  {\n    \"name\": \"my_table\",\n    \"rows\": [\n      {\n        \"id\": 1,\n        \"name\": \"some value\",\n        \"slug\": \"some-value\",\n        \"date\": \"2017-05-15 00:00:00\"\n      }\n    ]\n  },\n  {\n    \"name\": \"my_other_table\",\n    \"rows\": [\n      {\n        \"id\": 1,\n        \"item\": \"some item\",\n        \"quantity\": 9\n      },\n      {\n        \"id\": 2,\n        \"item\": \"some other item\",\n        \"quantity\": 3\n      }\n    ]\n  }\n]\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjsaylor%2Fsqlfixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjsaylor%2Fsqlfixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjsaylor%2Fsqlfixture/lists"}