Skip to content

Commit

Permalink
Merge pull request go-gitea#112 from phillip-hopper/feature/HashtagFi…
Browse files Browse the repository at this point in the history
…lterFix

Fix missing filter for repo owner
  • Loading branch information
richmahn authored Dec 9, 2016
2 parents d06c0d1 + cd205ad commit 91de627
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 233 deletions.
10 changes: 5 additions & 5 deletions models/hashtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ type Hashtag struct {
}

// GetHashtagSummary gets a summary of the hashtags for repositories with the specified prefix.
func GetHashtagSummary(repoPrefix string) ([]map[string]string, error) {
return getHashtagSummary(x, repoPrefix)
func GetHashtagSummary(repoPrefix string, ownerID int64) ([]map[string]string, error) {
return getHashtagSummary(x, repoPrefix, ownerID)
}

func getHashtagSummary(engine *xorm.Engine, repoPrefix string) ([]map[string]string, error) {
func getHashtagSummary(engine *xorm.Engine, repoPrefix string, ownerID int64) ([]map[string]string, error) {

sql := `SELECT h.tag_name, COUNT(*) AS count_of_occurrences
FROM repository AS r INNER JOIN hashtag AS h ON r.id = h.repo_id
WHERE r.name LIKE ?
WHERE r.owner_id = ? AND r.name LIKE ?
GROUP BY h.tag_name
ORDER BY LOWER(h.tag_name)`

// get the requested list of tags
results, err := engine.Query(sql, repoPrefix + "%")
results, err := engine.Query(sql, ownerID, repoPrefix + "%")

if err != nil {
return nil, err
Expand Down
54 changes: 50 additions & 4 deletions models/hashtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ import (
func TestHashtag(t *testing.T) {

userSql := `INSERT INTO user (id, lower_name, name, full_name, email, passwd, login_type, login_source, login_name, type, location, website, rands, salt, created_unix, updated_unix, last_login_unix, last_repo_visibility, max_repo_creation, is_active, is_admin, allow_git_hook, allow_import_local, prohibit_login, avatar, avatar_email, use_custom_avatar, num_followers, num_following, num_stars, num_repos, description, num_teams, num_members, diff_view_style)
VALUES ('1', 'phil', 'phil', '', '[email protected]', 'c024408767b2b15755e65c6799154c38ea9975c4a0d4642f47981c7169e76ca6cc507b4b8587efa2e656e0afd65017951bbd', '0', '0', '', '0', '', '', 'nekqlODpS1', 'fjsVRQvR0M', '1479588664', '1480428210', '1480428191', 'false', '-1', 'true', 'true', 'false', 'false', 'false', 'c1f199e0525420aceca0859cd4c9f990', '[email protected]', 'false', '0', '0', '0', '1', '', '0', '0', '')`
VALUES ('1', 'phil', 'phil', '', '[email protected]', 'c024408767b2b15755e65c6799154c38ea9975c4a0d4642f47981c7169e76ca6cc507b4b8587efa2e656e0afd65017951bbd', '0', '0', '', '0', '', '', 'nekqlODpS1', 'fjsVRQvR0M', '1479588664', '1480428210', '1480428191', 'false', '-1', 'true', 'true', 'false', 'false', 'false', 'c1f199e0525420aceca0859cd4c9f990', '[email protected]', 'false', '0', '0', '0', '1', '', '0', '0', ''),
('2', 'rich', 'rich', '', '[email protected]', 'c024408767b2b15755e65c6799154c38ea9975c4a0d4642f47981c7169e76ca6cc507b4b8587efa2e656e0afd65017951bbd', '0', '0', '', '0', '', '', 'nekqlODpS1', 'fjsVRQvR0M', '1479588664', '1480428210', '1480428191', 'false', '-1', 'true', 'true', 'false', 'false', 'false', 'c1f199e0525420aceca0859cd4c9f990', '[email protected]', 'false', '0', '0', '0', '1', '', '0', '0', ''),
('3', 'bruce', 'bruce', '', '[email protected]', 'c024408767b2b15755e65c6799154c38ea9975c4a0d4642f47981c7169e76ca6cc507b4b8587efa2e656e0afd65017951bbd', '0', '0', '', '0', '', '', 'nekqlODpS1', 'fjsVRQvR0M', '1479588664', '1480428210', '1480428191', 'false', '-1', 'true', 'true', 'false', 'false', 'false', 'c1f199e0525420aceca0859cd4c9f990', '[email protected]', 'false', '0', '0', '0', '1', '', '0', '0', '')`

repoSql := `INSERT INTO repository (id, owner_id, lower_name, name, description, website, default_branch, num_watches, num_stars, num_forks, num_issues, num_closed_issues, num_pulls, num_closed_pulls, num_milestones, num_closed_milestones, is_private, is_bare, is_mirror, enable_wiki, enable_external_wiki, external_wiki_url, enable_issues, enable_external_tracker, external_tracker_url, external_tracker_format, external_tracker_style, enable_pulls, is_fork, fork_id, created_unix, updated_unix)
VALUES ('1', '1', 'en-ubn-tags', 'en-ubn-tags', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1479589835', '1479731504'),
('2', '1', 'en-ubn', 'en-ubn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426137', '1480426138'),
('3', '1', 'en-tw', 'en-tw', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426161', '1480426161'),
('4', '1', 'en-ubnn', 'en-ubnn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426633', '1480426633'),
('5', '1', 'en-ub-test', 'en-ub-test', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480428210', '1480428210')`
('5', '1', 'en-ub-test', 'en-ub-test', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480428210', '1480428210'),
('6', '2', 'en-ubn-tags', 'en-ubn-tags', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1479589835', '1479731504'),
('7', '2', 'en-ubn', 'en-ubn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426137', '1480426138'),
('8', '2', 'en-tw', 'en-tw', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426161', '1480426161'),
('9', '2', 'en-ubnn', 'en-ubnn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426633', '1480426633'),
('10', '2', 'en-ub-test', 'en-ub-test', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480428210', '1480428210'),
('11', '3', 'en-ubn-tags', 'en-ubn-tags', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1479589835', '1479731504'),
('12', '3', 'en-ubn', 'en-ubn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426137', '1480426138'),
('13', '3', 'en-tw', 'en-tw', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426161', '1480426161'),
('14', '3', 'en-ubnn', 'en-ubnn', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480426633', '1480426633'),
('15', '3', 'en-ub-test', 'en-ub-test', '', '', 'master', '1', '0', '0', '0', '0', '0', '0', '0', '0', 'false', 'false', 'false', 'true', 'false', '', 'true', 'false', '', '', 'numeric', 'true', 'false', '0', '1480428210', '1480428210')`

hashtagSql := `INSERT INTO hashtag (id, user_id, repo_id, lang, tag_name, file_path, created_unix, updated_unix)
VALUES ('1','1','1','en','amill','article1.md','1480336882','1480336882'),
Expand Down Expand Up @@ -51,7 +63,34 @@ VALUES ('1','1','1','en','amill','article1.md','1480336882','1480336882'),
('24','1','1','en','bapt','article1.md','1480336882','1480336882'),
('25','1','2','en','episangl','article3.md','1480336882','1480336882'),
('26','1','2','en','jew','article3.md','1480336882','1480336882'),
('27','1','2','en','luth','article3.md','1480336882','1480336882')`
('27','1','2','en','luth','article3.md','1480336882','1480336882'),
('28','2','6','en','amill','article1.md','1480336882','1480336882'),
('29','2','7','en','amill','article2.md','1480336882','1480336882'),
('30','2','7','en','amill','article3.md','1480336882','1480336882'),
('31','2','6','en','salv','article1.md','1480336882','1480336882'),
('32','2','7','en','salv','article3.md','1480336882','1480336882'),
('33','2','7','en','m-asiaminor','article2.md','1480336882','1480336882'),
('34','2','7','en','m-endofearth','article3.md','1480336882','1480336882'),
('35','2','6','en','m-galilee','article1.md','1480336882','1480336882'),
('36','2','7','en','m-greece','article3.md','1480336882','1480336882'),
('37','2','7','en','xq-gen12:3','article2.md','1480336882','1480336882'),
('38','2','7','en','xa-isa40:3','article3.md','1480336882','1480336882'),
('39','2','6','en','xa-jer12:1','article1.md','1480336882','1480336882'),
('40','2','7','en','A1933','article3.md','1480336882','1480336882'),
('41','2','7','en','G58','article3.md','1480336882','1480336882'),
('42','2','7','en','H305','article3.md','1480336882','1480336882'),
('43','2','7','en','da-hsbaptism','article2.md','1480336882','1480336882'),
('44','2','7','en','da-kingdomofgod','article3.md','1480336882','1480336882'),
('45','2','6','en','da-modeofbaptism','article1.md','1480336882','1480336882'),
('46','2','7','en','da-ntuseofot','article3.md','1480336882','1480336882'),
('47','2','7','en','da-spiritualgifts','article3.md','1480336882','1480336882'),
('48','2','7','en','da-waterbaptism','article3.md','1480336882','1480336882'),
('49','2','7','en','dg-brother','article2.md','1480336882','1480336882'),
('50','2','7','en','dg-pentecost','article3.md','1480336882','1480336882'),
('51','2','6','en','bapt','article1.md','1480336882','1480336882'),
('52','2','7','en','episangl','article3.md','1480336882','1480336882'),
('53','2','7','en','jew','article3.md','1480336882','1480336882'),
('54','2','7','en','luth','article3.md','1480336882','1480336882')`

statements := []string{userSql, repoSql, hashtagSql}

Expand Down Expand Up @@ -86,7 +125,7 @@ VALUES ('1','1','1','en','amill','article1.md','1480336882','1480336882'),
defer os.RemoveAll(tempDir)

Convey("Test Hashtag.GetHashtagSummary()", t, func() {
results, err := getHashtagSummary(testEngine, "en-ubn")
results, err := getHashtagSummary(testEngine, "en-ubn", 1)
So(err, ShouldBeNil)

//print("Hashtags found: " + strconv.Itoa(len(results)) + "\n")
Expand All @@ -96,5 +135,12 @@ VALUES ('1','1','1','en','amill','article1.md','1480336882','1480336882'),
//print(fmt.Sprintf("%v: %v", hashtag["tag_name"], hashtag["count_of_occurrences"]) + "\n")
So(hashtag["tag_name"], ShouldNotBeEmpty)
}

// check for correct count
var tag = results[0] // A1933
So(tag["count_of_occurrences"], ShouldEqual, "1")

tag = results[1] // amill
So(tag["count_of_occurrences"], ShouldEqual, "3")
})
}
Loading

0 comments on commit 91de627

Please sign in to comment.