Giter VIP home page Giter VIP logo

dbmanager's Introduction

DBManager

Description

dbmanager is a simple helper for creating postgresql records for tests.

It uses squirrel for creating test records fast and easily.

The idea is that, given a set of default values for your relations, creating a record is as easy as calling Create with the table name and a set of modifiers which will then override the default value for each specified field.

Despite requiring the db connection handler, this was built for postgresql and won't work with any databases since there are a couple of implementation details that are specific to postgresql: - setting the PlaceholderFormat to the dollar sign; - all queries being built with ON CONFLICT DO NOTHING so unique constraints are ignored.

Usage

  • The recommended usage would be to add the initialization code in a package accessible to all other specific db logic packages (e.g. models/dbmanager.go) and then:
    • define a set of default values for each of the applications' relations;
    • create a constructor for initializing a dbmanager with such default values.
var defaultRelationValuesMap = map[string]dbmanager.RelationValues{
	"users": {
		"id":         "c09d0a2d-3dab-4a60-bbfa-54c8ed397062",
		"username":   "tlins",
		"email":      "[email protected]",
		"password":   "super_secret",
		"created_at": sq.Expr("CURRENT_TIMESTAMP"),
		"updated_at": sq.Expr("CURRENT_TIMESTAMP"),
	},
}

func NewTestDBManager(db *sql.DB, t *testing.T) dbmanager.DBManager {
	return dbmanager.New(
		db,
		t,
		defaultRelationValuesMap,
	)
}
  • When writing database tests, define a runBefore function that can set the db state to whatever it's required for the test. At that point, it's just a matter of initializing the db manager and creating the test records. This would look somewhat like this:
	cases := []struct {
		title     string
		runBefore func(*sql.DB, *testing.T)
		exp       expType
		expErr    error
	}{
		{
			name: "fails to create existing user",
			runBefore: func(db *sql.DB, t *testing.T) { // injecting the db handler can be done at the execution time
				dbMan := models.NewTestDBManager(db, t)
				dbMan.ResolveTestRecord(
					"users",
					dbmanager.SetFieldValue("id", existingUserID), // presuming there's a unique constraint on the id field, this would cause the creation to fail
				)
			},
		},
	}

Other thoughts

  • The initial idea and the reason why it was decided to ignore unique constraints was that we could simply seed the database once and run all tests against that given state. Doing it like that we could simply call the same seed function for every test and not have to worry about execution order, etc.. It turned out to be really annoying to design tests having to consider a static db state (specially once your models start growing and the relations between them become more complex).

  • For that reason, it was decided that having a clean state for each test case was a lot better instead. The tool choosen for this job was the awesome dbcleaner. It provides a nice and easy way to lock databases and clean them after each test case.

Credits

License

MIT

dbmanager's People

Contributors

titolins avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.