-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[WIP] Write tests for issue API #86
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ _testmain.go | |
coverage.out | ||
|
||
*.db | ||
*.sqlite3 | ||
*.log | ||
|
||
/gitea | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,10 @@ const ( | |
HTTPS Scheme = "https" | ||
FCGI Scheme = "fcgi" | ||
UNIX_SOCKET Scheme = "unix" | ||
|
||
RUN_MODE_PROD = "prod" | ||
RUN_MODE_DEV = "dev" | ||
RUN_MODE_TEST = "test" | ||
) | ||
|
||
type LandingPage string | ||
|
@@ -60,6 +64,8 @@ var ( | |
AppPath string | ||
AppDataPath string | ||
|
||
AppRunMode string | ||
|
||
// Server settings | ||
Protocol Scheme | ||
Domain string | ||
|
@@ -263,6 +269,10 @@ var ( | |
HasRobotsTxt bool | ||
) | ||
|
||
func GiteaPath() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exported functions should have be commented 😄 |
||
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/go-gitea/gitea") | ||
} | ||
|
||
// DateLang transforms standard language locale name to corresponding value in datetime plugin. | ||
func DateLang(lang string) string { | ||
name, ok := dateLangs[lang] | ||
|
@@ -361,7 +371,6 @@ func NewContext() { | |
please consider changing to GITEA_CUSTOM`) | ||
} | ||
} | ||
|
||
if len(CustomConf) == 0 { | ||
CustomConf = CustomPath + "/conf/app.ini" | ||
} | ||
|
@@ -390,6 +399,7 @@ please consider changing to GITEA_CUSTOM`) | |
if AppUrl[len(AppUrl)-1] != '/' { | ||
AppUrl += "/" | ||
} | ||
AppRunMode = Cfg.Section("").Key("RUN_MODE").In(RUN_MODE_PROD, []string{RUN_MODE_PROD, RUN_MODE_DEV, RUN_MODE_TEST}) | ||
|
||
// Check if has app suburl. | ||
url, err := url.Parse(AppUrl) | ||
|
@@ -498,8 +508,8 @@ please consider changing to GITEA_CUSTOM`) | |
}[Cfg.Section("time").Key("FORMAT").MustString("RFC1123")] | ||
|
||
RunUser = Cfg.Section("").Key("RUN_USER").String() | ||
// Does not check run user when the install lock is off. | ||
if InstallLock { | ||
// Does not check run user when the install lock is off or is running tests | ||
if InstallLock && AppRunMode != RUN_MODE_TEST { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change code for testing is a bad practice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. This is a WIP, we should abstract things using interfaces. I am open for suggestions on how to do it right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, thank you. It's very important to write testable code 👍 |
||
currentUser, match := IsRunUserMatchCurrentUser(RunUser) | ||
if !match { | ||
log.Fatal(4, "Expect user '%s' but current user is: %s", RunUser, currentUser) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package repo_test | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-gitea/gitea/models" | ||
"github.com/go-gitea/gitea/testutil" | ||
api "github.com/go-gitea/go-sdk/gitea" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
testutil.TestGlobalInit() | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestIssueIndex(t *testing.T) { | ||
testutil.PrepareTestDatabase() | ||
|
||
w, r := testutil.NewTestContext("GET", "/api/v1/repos/user1/foo/issues", "", nil, "1") | ||
testutil.ServeHTTP(w, r) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
} | ||
|
||
func TestIssueShow(t *testing.T) { | ||
testutil.PrepareTestDatabase() | ||
|
||
w, r := testutil.NewTestContext("GET", "/api/v1/repos/user1/foo/issues/1", "", nil, "1") | ||
testutil.ServeHTTP(w, r) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
|
||
issue := new(api.Issue) | ||
err := json.Unmarshal(w.Body.Bytes(), &issue) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "Title", issue.Title) | ||
assert.Equal(t, "Content", issue.Body) | ||
assert.Equal(t, "user1", issue.Poster.UserName) | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
testutil.PrepareTestDatabase() | ||
|
||
bytes, _ := json.Marshal(api.Issue{ | ||
Title: "A issue title", | ||
Body: "Please fix", | ||
}) | ||
count := testutil.TableCount("issue") | ||
w, r := testutil.NewTestContext("POST", "/api/v1/repos/user1/foo/issues", testutil.CONTENT_TYPE_JSON, bytes, "1") | ||
testutil.ServeHTTP(w, r) | ||
assert.Equal(t, http.StatusCreated, w.Code) | ||
assert.Equal(t, count+1, testutil.TableCount("issue")) | ||
issue, _ := models.GetIssueByID(testutil.LastId("issue")) | ||
assert.Equal(t, "A issue title", issue.Title) | ||
assert.Equal(t, "Please fix", issue.Content) | ||
} | ||
|
||
func TestEdit(t *testing.T) { | ||
testutil.PrepareTestDatabase() | ||
|
||
bytes, _ := json.Marshal(api.Issue{ | ||
Title: "Edited title", | ||
Body: "Edited content", | ||
}) | ||
w, r := testutil.NewTestContext("PATCH", "/api/v1/repos/user1/foo/issues/1", testutil.CONTENT_TYPE_JSON, bytes, "1") | ||
testutil.ServeHTTP(w, r) | ||
assert.Equal(t, http.StatusCreated, w.Code) | ||
issue, _ := models.GetIssueByID(1) | ||
assert.Equal(t, "Edited title", issue.Title) | ||
assert.Equal(t, "Edited content", issue.Content) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# THIS FILE EXISTS TO BE USED TO UNIT TESTS | ||
# DON'T CHANGE IT. THIS IS NOT THE FILE GOGS WILL USE IN PRODUCTION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
APP_NAME = Gogs: Go Git Service | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
RUN_MODE = test | ||
|
||
[database] | ||
DB_TYPE = sqlite3 | ||
PATH = testdata/gitea_test.sqlite3 | ||
|
||
[repository] | ||
ROOT = testdata/gitea-test-repositories | ||
|
||
[mailer] | ||
ENABLED = false | ||
|
||
[service] | ||
|
||
[picture] | ||
DISABLE_GRAVATAR = true | ||
|
||
[session] | ||
PROVIDER = memory | ||
|
||
[log] | ||
MODE = file | ||
LEVEL = Info | ||
|
||
[security] | ||
INSTALL_LOCK = true | ||
SECRET_KEY = foobar |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
- | ||
id: 1 | ||
repo_id: 1 | ||
index: 1 | ||
name: Title | ||
poster_id: 1 | ||
content: Content | ||
is_pull: false | ||
is_closed: false | ||
|
||
- | ||
id: 2 | ||
repo_id: 1 | ||
index: 2 | ||
name: Title | ||
poster_id: 1 | ||
content: Content | ||
is_pull: false | ||
is_closed: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
- | ||
id: 1 | ||
owner_id: 1 | ||
lower_name: foo | ||
name: foo | ||
num_issues: 2 | ||
|
||
- | ||
id: 2 | ||
owner_id: 2 | ||
lower_name: bar | ||
name: bar | ||
|
||
- | ||
id: 3 | ||
owner_id: 3 | ||
lower_name: foo | ||
name: foo | ||
|
||
- | ||
id: 4 | ||
owner_id: 4 | ||
lower_name: bar | ||
name: bar |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
- | ||
id: 1 | ||
lower_name: user1 | ||
name: user1 | ||
full_name: User 1 | ||
email: [email protected] | ||
passwd: "" | ||
avatar: "" | ||
avatar_email: "" | ||
|
||
- | ||
id: 2 | ||
lower_name: user2 | ||
name: user2 | ||
full_name: User 2 | ||
email: [email protected] | ||
passwd: "" | ||
avatar: "" | ||
avatar_email: "" |
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.
And now we can execute SQL queries from handlers?