Giter Site home page Giter Site logo

reltest's Introduction

reltest

GoDoc Test Go Report Card codecov Gitter chat

Database unit testing for Golang.

Example

package main

import (
	"context"
	"fmt"

	"github.com/go-rel/rel/where"
	"github.com/go-rel/reltest"
)

type Movie struct {
	ID    int
	Title string
}

func main() {
	var (
		repo = reltest.New()
	)

	// Mock query
	repo.ExpectFind(where.Eq("id", 1)).Result(Movie{ID: 1, Title: "Golang"})

	// Application code
	var movie Movie
	repo.MustFind(context.Background(), &movie, where.Eq("id", 1))
	fmt.Println(movie.Title)
	// Output: Golang
}

More Examples:

License

Released under the MIT License

reltest's People

Contributors

dependabot[bot] avatar fs02 avatar ddanurwenda avatar deepsource-autofix[bot] avatar lafriks avatar

Stargazers

Basel Ajarmah avatar

Watchers

 avatar  avatar

reltest's Issues

multiple expectations on the same statement

I see that go-rel/reltest is not merely relocated lines of code from go-rel/rel/reltest. One thing I still find it confusing is the feature I mentioned on the title is no longer supported in go-rel/reltest. For example, test below (slightly modified from repository_test.go) will pass on go-rel/rel/reltest but fail on go-rel/reltest

func TestRepository_Transaction_error(t *testing.T) {
	var (
		repo   = New()
		result = Book{Title: "Golang for dummies"}
		book   = Book{ID: 1, Title: "Golang for dummies"}
	)

	repo.ExpectTransaction(func(repo *Repository) {
		repo.ExpectInsert()
		repo.ExpectUpdate()
		repo.ExpectUpdate().ConnectionClosed()
	})

	assert.Equal(t, sql.ErrConnDone, repo.Transaction(context.TODO(), func(ctx context.Context) error {
		if err := repo.Insert(ctx, &result); err != nil {
			return err
		}
		if err := repo.Update(ctx, &result); err != nil {
			return err
		}
		return repo.Update(ctx, &result)
	}))

	assert.Equal(t, book, result)
	repo.AssertExpectations(t)
}

Is there any suggestion how to refactor above test to pass on go-rel/reltest as well? Thank you

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.