Skip to content

Commit 70e5c3b

Browse files
authored
fix tests (#278)
1 parent ce730d0 commit 70e5c3b

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

src/branch.v

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string,
5151
select from Branch where repo_id == repository_id && name == branch_name limit 1
5252
} or { []Branch{} }
5353

54+
// app.debug("branches: ${branches}")
55+
5456
if branches.len != 0 {
5557
branch := branches.first()
5658
app.update_branch(branch.id, author, hash, date)!
@@ -66,6 +68,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string,
6668
date: date
6769
}
6870

71+
app.debug("inserting branch: ${new_branch}")
72+
6973
sql app.db {
7074
insert new_branch into Branch
7175
}!

src/commit.v

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s
8686
select from Commit where repo_id == repo_id && branch_id == branch_id && hash == last_hash limit 1
8787
} or { []Commit{} }
8888

89+
// $dbg;
90+
8991
if commits.len > 0 {
9092
return
9193
}
@@ -100,6 +102,7 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s
100102
message: message
101103
}
102104

105+
// $dbg;
103106
sql app.db {
104107
insert new_commit into Commit
105108
}!

src/commit_routes.v

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import time
66
import api
77

88
@['/api/v1/:user/:repo_name/:branch_name/commits/count']
9-
fn (mut app App) handle_commits_count(username string, repo_name string, branch_name string) veb.Result {
9+
fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result {
1010
has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username,
1111
repo_name)
1212

@@ -18,17 +18,20 @@ fn (mut app App) handle_commits_count(username string, repo_name string, branch_
1818
return ctx.json_error('Not found')
1919
}
2020

21+
2122
branch := app.find_repo_branch_by_name(repo.id, branch_name)
2223
count := app.get_repo_commit_count(repo.id, branch.id)
2324

25+
// app.debug("${branch} ${count}" )
26+
2427
return ctx.json(api.ApiCommitCount{
2528
success: true
2629
result: count
2730
})
2831
}
2932

3033
@['/:username/:repo_name/:branch_name/commits/:page']
31-
pub fn (mut app App) commits(username string, repo_name string, branch_name string, page int) veb.Result {
34+
pub fn (mut app App) commits(mut ctx Context, username string, repo_name string, branch_name string, page int) veb.Result {
3235
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
3336

3437
branch := app.find_repo_branch_by_name(repo.id, branch_name)
@@ -72,7 +75,7 @@ pub fn (mut app App) commits(username string, repo_name string, branch_name stri
7275
}
7376

7477
@['/:username/:repo_name/commit/:hash']
75-
pub fn (mut app App) commit(username string, repo_name string, hash string) veb.Result {
78+
pub fn (mut app App) commit(mut ctx Context, username string, repo_name string, hash string) veb.Result {
7679
repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
7780

7881
is_patch_request := hash.ends_with('.patch')

src/repo.v

+13-2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ fn (mut app App) increment_repo_issues(repo_id int) ! {
207207
}!
208208
}
209209

210+
fn (mut app App) get_count_repo() int {
211+
return sql app.db {
212+
select count from Repo
213+
} or {0}
214+
}
215+
210216
fn (mut app App) add_repo(repo Repo) ! {
211217
sql app.db {
212218
insert repo into Repo
@@ -288,17 +294,19 @@ fn (mut app App) update_repo_from_fs(mut repo Repo) ! {
288294
app.info('Repo updated')
289295
}
290296

297+
// fn (mut app App) update_repo_branch_from_fs(mut ctx Context, mut repo Repo, branch_name string) ! {
291298
fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! {
292299
repo_id := repo.id
293300
branch := app.find_repo_branch_by_name(repo.id, branch_name)
294301

295302
if branch.id == 0 {
296303
return
297304
}
305+
// $dbg;
298306

299307
data := repo.git('--no-pager log ${branch_name} --abbrev-commit --abbrev=7 --pretty="%h${log_field_separator}%aE${log_field_separator}%cD${log_field_separator}%s${log_field_separator}%aN"')
300-
println('DATA=')
301-
println(data)
308+
// println('DATA=')
309+
// println(data)
302310

303311
for line in data.split_into_lines() {
304312
args := line.split(log_field_separator)
@@ -323,6 +331,8 @@ fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) !
323331
commit_author_id = user.id
324332
}
325333

334+
// $dbg;
335+
326336
app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author,
327337
commit_author_id, commit_message, int(commit_date.unix()))!
328338
}
@@ -397,6 +407,7 @@ fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) ! {
397407
commit_author_id = user.id
398408
}
399409

410+
// $dbg;
400411
app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author,
401412
commit_author_id, commit_message, int(commit_date.unix()))!
402413
}

src/repo_routes.v

+7-2
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
225225
}
226226
}
227227
repo_path := os.join_path(app.config.repo_storage_path, ctx.user.username, name)
228+
id := app.get_count_repo() + 1
228229
mut new_repo := &Repo{
229230
git_repo: git.new_repo(repo_path)
230231
name: name
232+
id: id
231233
description: description
232234
git_dir: repo_path
233235
user_id: ctx.user.id
@@ -240,7 +242,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
240242
os.mkdir(new_repo.git_dir) or { panic(err) }
241243
new_repo.git('init --bare')
242244
} else {
243-
println('GO CLONING:')
245+
app.debug("cloning")
244246
// t := time.now()
245247

246248
spawn app.foo(mut new_repo)
@@ -256,8 +258,11 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
256258
return ctx.redirect('/new')
257259
}
258260
repo_id := new_repo2.id
261+
// $dbg;
259262
// primary_branch := git.get_repository_primary_branch(repo_path)
260263
primary_branch := new_repo2.primary_branch
264+
// app.debug("new_repo2: ${new_repo2}")
265+
261266
app.update_repo_primary_branch(repo_id, primary_branch) or {
262267
ctx.error('There was an error while adding the repo')
263268
return app.new(mut ctx)
@@ -284,7 +289,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str
284289

285290
pub fn (mut app App) foo(mut new_repo Repo) {
286291
new_repo.clone()
287-
println('CLONING DONE')
292+
app.debug("cloning done")
288293
app.update_repo_from_fs(mut new_repo) or {}
289294
// git.clone(valid_clone_url, repo_path)
290295
}

tests/first_run.v

+5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ fn main() {
4242
assert get_repo_branch_count(token, test_username, 'test1') == 0
4343

4444
test_create_repo(token, 'test2', test_github_repo_url)
45+
// wait while repo is cloning
46+
time.sleep(3 * time.second)
47+
// get repo
4548
assert get_repo_commit_count(token, test_username, 'test2', test_github_repo_primary_branch) > 0
4649
assert get_repo_issue_count(token, test_username, 'test2') == 0
4750
assert get_repo_branch_count(token, test_username, 'test2') > 0
51+
ilog("all tests passed!")
4852

4953
after()!
5054
}
@@ -230,6 +234,7 @@ fn get_repo_commit_count(token string, username string, repo_name string, branch
230234
response_json := json.decode(api.ApiCommitCount, response.body) or {
231235
exit_with_message(err.str())
232236
}
237+
dump(response_json.result)
233238

234239
return response_json.result
235240
}

0 commit comments

Comments
 (0)