-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
37a945d
to
709a336
Compare
|
||
func insert(insertBeans ...interface{}) error { | ||
sess := x.NewSession() | ||
_, err := sess.Insert(insertBeans...) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
709a336
to
8e151cd
Compare
@lunny Thanks for the quick feedback, I've added |
c6ee02b
to
fd075c0
Compare
import ( | ||
"github.com/go-xorm/core" | ||
"github.com/go-xorm/xorm" | ||
_ "github.com/mattn/go-sqlite3" // for the test engine |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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" | ||
) |
There was a problem hiding this comment.
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"
)
There was a problem hiding this comment.
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" | ||
) |
There was a problem hiding this comment.
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"
)
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, updated.
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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...
c8924c8
to
d076fe6
Compare
LGTM |
LGTM 🎉 |
There was a problem hiding this 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", |
There was a problem hiding this comment.
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 :)
d076fe6
to
12ba4e1
Compare
@bkcsoft Whoops, removed |
Taking a cue from @bkcsoft's comment (#86 (comment)), this PR adds unit tests for
model/token.go
andmodels/update.go
using an in-memory SQLite database.