DEV Community

Discussion on: Building a finance tracking REST API using Go with TDD - Part 1

Collapse
 
alirezabashiri profile image
Alireza Bashiri • Edited

Good catch! $GOPATH I mean. That's obviously a mistake. About the test cases in a single file I have no problem with it, for example; if you look at Django testing that's how they're doing it. I believe in one simple package and many test files and I always do it.

Here's a simple example;

budgets.go
budgets_test.go
categories.go
categories_test.go
transactions.go
transactions_test.go

Also as I'm going to show, with github.com/go-pg/orm.CreateTableOptions which has a specific field called Temp we're going to create a temporary database for each test case and do the isolation.

func TestMain(m *testing.M) {
    testServer = newServer(
        pg.Connect(&pg.Options{
            User:     "alireza",
            Password: "alireza",
            Database: "alireza_test",
        }),
        mux.NewRouter(),
    )

    testServer.db.CreateTable(&budget{}, &orm.CreateTableOptions{
        Temp: true,
    })

    os.Exit(m.Run())
}

I've more than 5 years of experience developing various web applications using RubyOnRails and Django but I've to tell you there's too much complexity in them but they're productive for people who are master and I think one reason that Go got too much attention was that it's easy for even juniors to master and write a simple web application or a RESTful API. But simplicity comes at a cost and using a programming language and a specific framework needs experience and dedication and choosing Go could be a wrong decision in some cases.