summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-06-14 23:09:03 -0400
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2017-06-15 05:09:03 +0200
commit8fcda0442e60209d7e6783c95d96f20ff04dbdd6 (patch)
tree8b47fcaab6003453ec319fc4960d959a2d06e6e9 /models
parentbf48c8ebdd3975d1090115a9106069796c67a6da (diff)
downloadgitea-8fcda0442e60209d7e6783c95d96f20ff04dbdd6.tar.gz
gitea-8fcda0442e60209d7e6783c95d96f20ff04dbdd6.zip
Fix search by issue type (#1914)
* Fix search by issue type
Diffstat (limited to 'models')
-rw-r--r--models/issue.go52
-rw-r--r--models/unit_tests.go40
2 files changed, 46 insertions, 46 deletions
diff --git a/models/issue.go b/models/issue.go
index 8340595fca..d4d0a5a2ff 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -1223,7 +1223,6 @@ func parseCountResult(results []map[string][]byte) int64 {
// IssueStatsOptions contains parameters accepted by GetIssueStats.
type IssueStatsOptions struct {
- FilterMode int
RepoID int64
Labels string
MilestoneID int64
@@ -1241,7 +1240,7 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
countSession := func(opts *IssueStatsOptions) *xorm.Session {
sess := x.
Where("issue.repo_id = ?", opts.RepoID).
- And("is_pull = ?", opts.IsPull)
+ And("issue.is_pull = ?", opts.IsPull)
if len(opts.IssueIDs) > 0 {
sess.In("issue.id", opts.IssueIDs)
@@ -1252,8 +1251,8 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
if err != nil {
log.Warn("Malformed Labels argument: %s", opts.Labels)
} else if len(labelIDs) > 0 {
- sess.Join("INNER", "issue_label", "issue.id = issue_id").
- In("label_id", labelIDs)
+ sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").
+ In("issue_label.label_id", labelIDs)
}
}
@@ -1262,11 +1261,11 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
}
if opts.AssigneeID > 0 {
- sess.And("assignee_id = ?", opts.AssigneeID)
+ sess.And("issue.assignee_id = ?", opts.AssigneeID)
}
if opts.PosterID > 0 {
- sess.And("poster_id = ?", opts.PosterID)
+ sess.And("issue.poster_id = ?", opts.PosterID)
}
if opts.MentionedID > 0 {
@@ -1279,40 +1278,15 @@ func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
}
var err error
- switch opts.FilterMode {
- case FilterModeAll, FilterModeAssign:
- stats.OpenCount, err = countSession(opts).
- And("is_closed = ?", false).
- Count(new(Issue))
-
- stats.ClosedCount, err = countSession(opts).
- And("is_closed = ?", true).
- Count(new(Issue))
- case FilterModeCreate:
- stats.OpenCount, err = countSession(opts).
- And("poster_id = ?", opts.PosterID).
- And("is_closed = ?", false).
- Count(new(Issue))
-
- stats.ClosedCount, err = countSession(opts).
- And("poster_id = ?", opts.PosterID).
- And("is_closed = ?", true).
- Count(new(Issue))
- case FilterModeMention:
- stats.OpenCount, err = countSession(opts).
- Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
- And("issue_user.uid = ?", opts.PosterID).
- And("issue_user.is_mentioned = ?", true).
- And("issue.is_closed = ?", false).
- Count(new(Issue))
-
- stats.ClosedCount, err = countSession(opts).
- Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
- And("issue_user.uid = ?", opts.PosterID).
- And("issue_user.is_mentioned = ?", true).
- And("issue.is_closed = ?", true).
- Count(new(Issue))
+ stats.OpenCount, err = countSession(opts).
+ And("issue.is_closed = ?", false).
+ Count(new(Issue))
+ if err != nil {
+ return stats, err
}
+ stats.ClosedCount, err = countSession(opts).
+ And("issue.is_closed = ?", true).
+ Count(new(Issue))
return stats, err
}
diff --git a/models/unit_tests.go b/models/unit_tests.go
index a3459e49f7..16eb09968c 100644
--- a/models/unit_tests.go
+++ b/models/unit_tests.go
@@ -37,13 +37,31 @@ func PrepareTestDatabase() error {
return LoadFixtures()
}
+type testCond struct {
+ query interface{}
+ args []interface{}
+}
+
+// Cond create a condition with arguments for a test
+func Cond(query interface{}, args ...interface{}) interface{} {
+ return &testCond{query: query, args: args}
+}
+
+func whereConditions(sess *xorm.Session, conditions []interface{}) {
+ for _, condition := range conditions {
+ switch cond := condition.(type) {
+ case *testCond:
+ sess.Where(cond.query, cond.args...)
+ default:
+ sess.Where(cond)
+ }
+ }
+}
+
func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
sess := x.NewSession()
defer sess.Close()
-
- for _, cond := range conditions {
- sess = sess.Where(cond)
- }
+ whereConditions(sess, conditions)
return sess.Get(bean)
}
@@ -65,6 +83,16 @@ func AssertExistsAndLoadBean(t *testing.T, bean interface{}, conditions ...inter
return bean
}
+// GetCount get the count of a bean
+func GetCount(t *testing.T, bean interface{}, conditions ...interface{}) int {
+ sess := x.NewSession()
+ defer sess.Close()
+ whereConditions(sess, conditions)
+ count, err := sess.Count(bean)
+ assert.NoError(t, err)
+ return int(count)
+}
+
// AssertNotExistsBean assert that a bean does not exist in the test database
func AssertNotExistsBean(t *testing.T, bean interface{}, conditions ...interface{}) {
exists, err := loadBeanIfExists(bean, conditions...)
@@ -80,7 +108,5 @@ func AssertSuccessfulInsert(t *testing.T, beans ...interface{}) {
// AssertCount assert the count of a bean
func AssertCount(t *testing.T, bean interface{}, expected interface{}) {
- actual, err := x.Count(bean)
- assert.NoError(t, err)
- assert.EqualValues(t, expected, actual)
+ assert.EqualValues(t, expected, GetCount(t, bean))
}