Skip to content

Unit tests for token and update models #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2016

Conversation

ethantkoenig
Copy link
Member

Taking a cue from @bkcsoft's comment (#86 (comment)), this PR adds unit tests for model/token.go and models/update.go using an in-memory SQLite database.

@ethantkoenig ethantkoenig changed the title Unit tests token and update models Unit tests for token and update models Dec 29, 2016
@ethantkoenig ethantkoenig force-pushed the models/unittests branch 2 times, most recently from 37a945d to 709a336 Compare December 29, 2016 01:27
@lunny lunny added this to the 1.1.0 milestone Dec 29, 2016
@lunny lunny added type/enhancement An improvement of existing functionality type/testing labels Dec 29, 2016

func insert(insertBeans ...interface{}) error {
sess := x.NewSession()
_, err := sess.Insert(insertBeans...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer sess.Close() MUST add this.


func delete(deleteBeans ...interface{}) error {
sess := x.NewSession()
// non-zero limit allows for condition-less deletes. Since it is very
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer sess.Close() MUST add this.

suite.NoError(AddUpdateTask(task))

sess := x.NewSession()
has, err := sess.Get(task)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

suite.NoError(PopulateUpdateTaskData())
suite.NoError(DeleteUpdateTaskByUUID("uuid1"))
sess := x.NewSession()
has, err := sess.Get(&UpdateTask{UUID: "uuid1"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

@ethantkoenig
Copy link
Member Author

ethantkoenig commented Dec 29, 2016

@lunny Thanks for the quick feedback, I've added defer sess.Close() calls

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 29, 2016
@ethantkoenig ethantkoenig force-pushed the models/unittests branch 2 times, most recently from c6ee02b to fd075c0 Compare December 29, 2016 02:29
import (
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" // for the test engine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like you use tidb as test db since go-sqlite3 will ask cgo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we can use build tags to allow choose what DB we will run test agains: MySQL/PG/SQLite/etc

Copy link
Member Author

@ethantkoenig ethantkoenig Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testfixtures does not support tidb, so we won't be able to use both testfixtures and tidb. Should I switch to tidb and continue manually deleting/inserting beans, or keep using sqlite and use testfixtures?

(By the way, what does CGO stand for?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep SQLite for now, until I can add support to TiDB on testfixtures.

@lunny What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

Copy link
Contributor

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ethantkoenig

I think this approach is very close to what the team had in mind.

/cc @bkcsoft

// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should be named something_test.go, so it whould not be compiled if not running tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to setup_for_test.go.

import (
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" // for the test engine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we can use build tags to allow choose what DB we will run test agains: MySQL/PG/SQLite/etc

return nil
}

func deleteTestBeans(deleteBeans... interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use testfixtures instead of manually deleting/inserting beans?

The biggest problem of doing that manually is having a lot of foreign key violations while running tests.

import (
"github.com/stretchr/testify/suite"
"testing"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import grouping:

import (
	"testing"

	"github.com/stretchr/testify/suite"
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

"github.com/stretchr/testify/suite"
"testing"
"time"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import ( 
 	"container/list"
 	"testing"
 	"time"

 	"code.gitea.io/git"

 	"github.com/stretchr/testify/suite"
 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


type TokenTestSuite struct {
suite.Suite
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a suite or just call assert functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used test suites because they can ensure that CreateTestEngine() is called before any tests run. If there is some way to ensure this without suites, I'd be open to changing.

Copy link
Contributor

@andreynering andreynering Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with both approaches, but I think this would work, too:

func TestMain(m *testing.M) {
    // create engine here
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we have multiple test files, won't there be a name collision if they each have a TestMain?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethantkoenig You're right, we can have only one per package. If that is not enough I'm fine with keeping the suite, but isn't it enough to init the engine once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, updated.

Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'd suggest using https://github.com/go-testfixtures/testfixtures instead of hard-coded data :)

}

// CreateTestEngine create an xorm engine for testing
func CreateTestEngine() error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of using custom db-setups for this, though seeing as models.LoadConfigs requires the use of app.ini it can't easily be done w/o that...

@bkcsoft bkcsoft added the pr/wip This PR is not ready for review label Dec 29, 2016
@ethantkoenig ethantkoenig force-pushed the models/unittests branch 2 times, most recently from c8924c8 to d076fe6 Compare December 30, 2016 18:31
@ethantkoenig ethantkoenig changed the title Unit tests for token and update models [WIP] Unit tests for token and update models Dec 30, 2016
@andreynering
Copy link
Contributor

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Dec 30, 2016
@bkcsoft
Copy link
Member

bkcsoft commented Dec 30, 2016

LGTM 🎉

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 30, 2016
Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this one

},
{
"checksumSHA1": "Dp8TnT65nINpRF/PrxrgmnYLsyA=",
"path": "github.com/stretchr/testify/suite",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still in use? otherwise please remove it :)

@ethantkoenig
Copy link
Member Author

@bkcsoft Whoops, removed

@ethantkoenig ethantkoenig changed the title [WIP] Unit tests for token and update models Unit tests for token and update models Dec 30, 2016
@andreynering andreynering removed the pr/wip This PR is not ready for review label Dec 30, 2016
@bkcsoft bkcsoft merged commit de8b73d into go-gitea:master Dec 31, 2016
@ethantkoenig ethantkoenig deleted the models/unittests branch December 31, 2016 16:38
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality type/testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants