{"id":20645493,"url":"https://github.com/pedrohenriques/go-dbfixtures","last_synced_at":"2026-06-06T15:05:12.784Z","repository":{"id":187289359,"uuid":"578180831","full_name":"PedroHenriques/go-dbfixtures","owner":"PedroHenriques","description":"A DB fixture manager for automated testing","archived":false,"fork":false,"pushed_at":"2023-08-10T15:31:49.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T09:33:48.844Z","etag":null,"topics":["fixtures","golang","testing","testing-tools"],"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/PedroHenriques.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":"2022-12-14T12:56:35.000Z","updated_at":"2023-08-09T17:00:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"d5992c48-d6b4-429d-aa9c-e36c6d89c8d6","html_url":"https://github.com/PedroHenriques/go-dbfixtures","commit_stats":null,"previous_names":["pedrohenriques/go-dbfixtures"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PedroHenriques%2Fgo-dbfixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PedroHenriques%2Fgo-dbfixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PedroHenriques%2Fgo-dbfixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PedroHenriques%2Fgo-dbfixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PedroHenriques","download_url":"https://codeload.github.com/PedroHenriques/go-dbfixtures/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242714052,"owners_count":20173581,"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","golang","testing","testing-tools"],"created_at":"2024-11-16T16:20:04.272Z","updated_at":"2026-06-06T15:05:12.734Z","avatar_url":"https://github.com/PedroHenriques.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Coverage Status](https://coveralls.io/repos/github/PedroHenriques/go-dbfixtures/badge.svg?branch=main)](https://coveralls.io/github/PedroHenriques/go-dbfixtures?branch=main)\r\n![ci workflow](https://github.com/PedroHenriques/go-dbfixtures/actions/workflows/ci.yml/badge.svg?branch=main)\r\n![cd workflow](https://github.com/PedroHenriques/go-dbfixtures/actions/workflows/cd.yml/badge.svg)\r\n\r\n# Fixtures Manager\r\n\r\nAn abstraction layer for handling database fixtures for automated testing purposes, providing a standardized interface across different database systems.\r\n\r\n## Installation\r\n\r\n```sh\r\ngo get github.com/PedroHenriques/go-dbfixtures\r\n```\r\n\r\n## Features\r\n\r\n* Test runner agnostic\r\n* No dependencies\r\n* Standardized interface across multiple database systems\r\n* Easily set your database for each test's needs\r\n\r\n## Golang versions\r\n\r\n- **Package version `1.0.*`** supports Golang `v1.17` or higher.  \r\n\r\n## Drivers\r\n\r\nThis package will use drivers to handle the database operations.\r\nEach driver will be dedicated to 1 databse system (ex: MongoDb, Postgres).  \r\nYou can set as many drivers as needed and the fixtures will be sent to each one.\r\n\r\n### Driver interface\r\n\r\nThe drivers are expected to use the following interface\r\n\r\n```go\r\ntype IDriver interface {\r\n\t// clears the specified \"tables\" of any content\r\n\tTruncate(tableNames []string) error\r\n\r\n\t// inserts the supplied \"rows\" into the specified \"table\"\r\n\tInsertFixtures(tableName string, fixtures []interface{}) error\r\n\r\n\t// cleanup and terminate the connection to the database\r\n\tClose() error\r\n}\r\n```\r\n\r\n### Current official drivers\r\n\r\n* [MongoDB](https://github.com/PedroHenriques/go-dbfixtures-mongodb-driver)\r\n\r\n## Usage\r\n\r\nThis package exposes the following function\r\n\r\n```go\r\nfunc New(drivers ...IDriver) IDbfixtures\r\n\r\ntype IDbfixtures interface {\r\n\tInsertFixtures(tableNames []string, fixtures map[string][]interface{}) error\r\n\tCloseDrivers() error\r\n}\r\n```\r\n\r\nWhere\r\n\r\n* `InsertFixtures(tableNames []string, fixtures map[string][]interface{}) error`: call this function with the fixtures to be sent to each registered driver.  \r\n**Note:** the fixtures will be inserted in the order they are provided.\r\n\r\n* `CloseDrivers() error`: call this function to run any necessary cleanup operations on all registered drivers.\r\n\r\nThe `IDriver` interface is described in the section **Driver interface** above.\r\n\r\n### Example\r\n\r\n```go\r\npackage driver_test\r\n\r\nimport (\r\n\t\"context\"\r\n\r\n\t\"github.com/PedroHenriques/go-dbfixtures-mongodb-driver/driver\"\r\n\t\"github.com/PedroHenriques/go-dbfixtures/dbfixtures\"\r\n\t\"github.com/stretchr/testify/require\"\r\n\t\"github.com/stretchr/testify/suite\"\r\n\t\"go.mongodb.org/mongo-driver/bson\"\r\n\t\"go.mongodb.org/mongo-driver/mongo\"\r\n\t\"go.mongodb.org/mongo-driver/mongo/options\"\r\n)\r\n\r\ntype driverE2eTestSuite struct {\r\n\tsuite.Suite\r\n\r\n\tmongoCliente *mongo.Client\r\n\tmongoDbName  string\r\n}\r\n\r\nfunc (suite *driverE2eTestSuite) SetupSuite() {\r\n\tConnUrl := \"mongodb://testmongo:27017\"\r\n\topts := options.Client().ApplyURI(ConnUrl)\r\n\r\n\tclient, err := mongo.Connect(context.TODO(), opts)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tsuite.mongoCliente = client\r\n\r\n\tsuite.mongoDbName = \"testCol\"\r\n}\r\n\r\nfunc (suite *driverE2eTestSuite) TearDownSuite() {\r\n\tsuite.mongoCliente.Disconnect(context.TODO())\r\n}\r\n\r\nfunc (suite *driverE2eTestSuite) SetupTest() {\r\n\tdatabase := suite.mongoCliente.Database(suite.mongoDbName)\r\n\terr := database.Drop(context.TODO())\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n}\r\n\r\nfunc (suite *driverE2eTestSuite) TestItShouldWork() {\r\n\tdatabase := suite.mongoCliente.Database(suite.mongoDbName)\r\n\tcol1 := database.Collection(\"col1\")\r\n\r\n\tinsRes1, _ := col1.InsertMany(\r\n\t\tcontext.TODO(),\r\n\t\t[]interface{}{\r\n\t\t\ttestDocument{Name: \"doc 11\", Age: 3},\r\n\t\t\ttestDocument{Name: \"doc 12\", Age: 33},\r\n\t\t},\r\n\t)\r\n\r\n\trequire.Equal(suite.T(), 2, len(insRes1.InsertedIDs))\r\n\r\n\texpectedDocuments := \u0026[]interface{}{\r\n\t\ttestDocument{Name: \"doc 26\", Age: 86},\r\n\t\ttestDocument{Name: \"doc 27\", Age: 87},\r\n\t}\r\n\r\n\tdriver := driver.New(\r\n\t\tsuite.mongoCliente, suite.mongoDbName, \u0026options.DatabaseOptions{},\r\n\t)\r\n\r\n\tfixtureHandler := dbfixtures.New(driver)\r\n\r\n\terr := fixtureHandler.InsertFixtures(\r\n\t\t[]string{\"co12\"},\r\n\t\tmap[string][]interface{}{\r\n\t\t\t\"col1\": *expectedDocuments,\r\n\t\t},\r\n\t)\r\n\r\n\trequire.Nil(suite.T(), err)\r\n\r\n\tcolDocs, _ := col1.Find(context.TODO(), bson.D{})\r\n\tactualDocuments := \u0026[]testDocument{}\r\n\tcolDocs.All(context.TODO(), actualDocuments)\r\n\r\n\trequire.EqualValues(suite.T(), len(*expectedDocuments), len(*actualDocuments))\r\n\tfor i, actualDocument := range *actualDocuments {\r\n\t\trequire.EqualExportedValues(suite.T(), (*expectedDocuments)[i], actualDocument)\r\n\t}\r\n}\r\n```\r\n\r\n## How It Works\r\n\r\nEach registered driver will be called to:\r\n\r\n* clear the \"tables\" that will be used in the current fixture insertion operation from any content.\r\n\r\n* insert the fixtures in the order they were provided.\r\n\r\n* terminate the connection to their database.\r\n\r\n## Testing This Package\r\n\r\n* `cd` into the package's root directory\r\n\r\n* Run `sh cli/test.sh -b -gv 1.20`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedrohenriques%2Fgo-dbfixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpedrohenriques%2Fgo-dbfixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedrohenriques%2Fgo-dbfixtures/lists"}