Skip to content

Commit fcfa50e

Browse files
committed
Refactoring
1 parent e0e570f commit fcfa50e

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

src/main/scala/codecheck/github/operations/SearchOp.scala

+8-21
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,38 @@ import scala.concurrent.ExecutionContext.Implicits.global
66
import codecheck.github.api.GitHubAPI
77
import codecheck.github.models.SearchInput
88
import codecheck.github.models.SearchRepositoryResult
9-
//import codecheck.github.models.SortDirection
109
import codecheck.github.models.SearchCodeResult
1110
import codecheck.github.models.SearchIssueResult
1211
import codecheck.github.models.SearchUserResult
1312

1413
trait SearchOp {
1514
self: GitHubAPI =>
1615

17-
def searchRepositories(input: SearchInput): Future[Option[SearchRepositoryResult]] = {
16+
def searchRepositories(input: SearchInput): Future[SearchRepositoryResult] = {
1817
val path = s"/search/repositories?q=${input.q}&sort=${input.sort}&order=${input.order}"
1918
exec("GET", path ).map { res =>
20-
res.statusCode match {
21-
case 200 => Some(SearchRepositoryResult(res.body))
22-
case 404 => None
23-
}
19+
SearchRepositoryResult(res.body)
2420
}
2521
}
2622

27-
def searchCode(input: SearchInput): Future[Option[SearchCodeResult]] = {
23+
def searchCode(input: SearchInput): Future[SearchCodeResult] = {
2824
val path = s"/search/code?q=${input.q}&sort=${input.sort}&order=${input.order}"
2925
exec("GET", path ).map { res =>
30-
res.statusCode match {
31-
case 200 => Some(SearchCodeResult(res.body))
32-
case 404 => None
33-
}
26+
SearchCodeResult(res.body)
3427
}
3528
}
3629

37-
def searchIssues(input: SearchInput): Future[Option[SearchIssueResult]] = {
30+
def searchIssues(input: SearchInput): Future[SearchIssueResult] = {
3831
val path = s"/search/issues?q=${input.q}&sort=${input.sort}&order=${input.order}"
3932
exec("GET", path ).map { res =>
40-
res.statusCode match {
41-
case 200 => Some(SearchIssueResult(res.body))
42-
case 404 => None
43-
}
33+
SearchIssueResult(res.body)
4434
}
4535
}
4636

47-
def searchUser(input: SearchInput): Future[Option[SearchUserResult]] = {
37+
def searchUser(input: SearchInput): Future[SearchUserResult] = {
4838
val path = s"/search/users?q=${input.q}&sort=${input.sort}&order=${input.order}"
4939
exec("GET", path ).map { res =>
50-
res.statusCode match {
51-
case 200 => Some(SearchUserResult(res.body))
52-
case 404 => None
53-
}
40+
SearchUserResult(res.body)
5441
}
5542
}
5643
}

src/test/scala/SearchOpSpec.scala

+29-35
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,38 @@ class SearchOpSpec extends FunSpec
1818
var q = "tetris language:assembly"
1919
val q1 = q.trim.replaceAll(" ","+");
2020
val input = SearchInput(q1,sort=Some(SearchSort.stars),order=SortDirection.desc)
21-
Await.result(api.searchRepositories(input), TIMEOUT).map { res =>
22-
assert(res.total_count >= 1)
23-
assert(res.items(0).id >= 1 )
24-
assert(res.items(0).name.length >= 1)
25-
assert(res.items(0).full_name.length >= 1)
26-
assert(res.items(0).description.isDefined)
27-
assert(res.items(0).open_issues_count >= 0)
28-
assert(res.items(0).language == "Assembly")
29-
assert(res.items(0).stargazers_count > res.items(1).stargazers_count)
30-
}
21+
val res = Await.result(api.searchRepositories(input), TIMEOUT)
22+
assert(res.total_count >= 1)
23+
assert(res.items(0).id >= 1 )
24+
assert(res.items(0).name.length >= 1)
25+
assert(res.items(0).full_name.length >= 1)
26+
assert(res.items(0).description.isDefined)
27+
assert(res.items(0).open_issues_count >= 0)
28+
assert(res.items(0).language == "Assembly")
29+
assert(res.items(0).stargazers_count > res.items(1).stargazers_count)
3130
}
3231
it("with valid changed query(q) SearchInput should succeed") {
3332
var q = "jquery in:name,description"
3433
val q1 = q.trim.replaceAll(" ","+");
3534
val input = SearchInput(q1,sort=Some(SearchSort.stars),order=SortDirection.desc)
36-
Await.result(api.searchRepositories(input), TIMEOUT).map { res =>
37-
assert(res.total_count >= 1)
38-
assert(res.items(0).id >= 1 )
39-
assert(res.items(0).name.length >= 1)
40-
assert(res.items(0).full_name.length >= 1)
41-
assert(res.items(0).description.isDefined)
42-
assert(res.items(0).open_issues_count >= 0)
43-
}
35+
val res = Await.result(api.searchRepositories(input), TIMEOUT)
36+
assert(res.total_count >= 1)
37+
assert(res.items(0).id >= 1 )
38+
assert(res.items(0).name.length >= 1)
39+
assert(res.items(0).full_name.length >= 1)
40+
assert(res.items(0).description.isDefined)
41+
assert(res.items(0).open_issues_count >= 0)
4442
}
4543
}
4644
describe("searchCode") {
4745
it("with valid SearchInput q,no SortOrder should succeed") {
4846
var q = "addClass in:file language:js repo:jquery/jquery"
4947
val q1 = q.trim.replaceAll(" ","+");
5048
val input = SearchInput(q1,sort=None,order=SortDirection.desc)
51-
Await.result(api.searchCode(input), TIMEOUT).map { res =>
52-
assert(res.total_count >= 1)
53-
assert(res.items(0).Repo.id >= 1 )
54-
assert(res.items(0).Repo.full_name == "jquery/jquery")
55-
}
49+
val res = Await.result(api.searchCode(input), TIMEOUT)
50+
assert(res.total_count >= 1)
51+
assert(res.items(0).Repo.id >= 1 )
52+
assert(res.items(0).Repo.full_name == "jquery/jquery")
5653
}
5754
//Following test results in error:
5855
// "message" : "Validation Failed",
@@ -77,26 +74,23 @@ class SearchOpSpec extends FunSpec
7774
var q = "windows label:bug language:python state:open"
7875
val q1 = q.trim.replaceAll(" ","+");
7976
val input = SearchInput(q1,sort=Some(SearchSort.created),order=SortDirection.asc)
80-
Await.result(api.searchIssues(input), TIMEOUT).map { res =>
81-
assert(res.total_count >= 1)
82-
assert(res.items(0).labels(0).name == "bug" )
83-
assert(res.items(0).state == "open")
84-
assert(((res.items(0).created_at).compareTo(res.items(1).created_at)) > 0)
85-
}
77+
val res = Await.result(api.searchIssues(input), TIMEOUT)
78+
assert(res.total_count >= 1)
79+
assert(res.items(0).labels(0).name == "bug" )
80+
assert(res.items(0).state == "open")
81+
assert(((res.items(0).created_at).compareTo(res.items(1).created_at)) > 0)
8682
}
8783
}
8884
describe("searchUser") {
8985
it("with valid SearchInput should succeed") {
9086
var q = "tom repos:>42 followers:>1000"
9187
q = q.trim.replaceAll(" ","+")
9288
val q1 = q.replaceAll(">","%3E")
93-
println("QUERY: searchUser" + q1)
9489
val input = SearchInput(q1,sort=None,order=SortDirection.desc)
95-
Await.result(api.searchUser(input), TIMEOUT).map { res =>
96-
assert(res.total_count >= 1)
97-
assert(res.items(0).login.length >= 1)
98-
assert(res.items(0).id >= 1)
99-
}
90+
val res = Await.result(api.searchUser(input), TIMEOUT)
91+
assert(res.total_count >= 1)
92+
assert(res.items(0).login.length >= 1)
93+
assert(res.items(0).id >= 1)
10094
}
10195
}
10296
}

0 commit comments

Comments
 (0)