Skip to content

Commit

Permalink
#100 test and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 22, 2020
1 parent fb29b8b commit 46c5ccc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
35 changes: 24 additions & 11 deletions objects/badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,31 @@ def attach(text)
Xia::Rank.new(@project.author).enter('badges.attach')
raise DuplicateError, "The badge #{text.inspect} is already attached" if exists?(text)
raise Xia::Urror, "The badge #{text.inspect} looks wrong" unless /^([a-z0-9]{3,12}|L[0-9])$/.match?(text)
if /^(newbie|L[0-9])$/.match?(text)
after = text == 'newbie' ? 0 : text[1].to_i
before = level
Xia::Rank.new(@project.author).enter("badges.promote-to-#{text}") if after > before
Xia::Rank.new(@project.author).enter("badges.degrade-from-L#{before}") if after < before
elsif all.length >= 5
raise Xia::Urror, 'Too many badges already'
id = @pgsql.transaction do |t|
if /^(newbie|L[0-9])$/.match?(text)
after = text == 'newbie' ? 0 : text[1].to_i
before = level
reviews = Xia::Reviews.new(@pgsql, @project, log: @log)
if after > before
Xia::Rank.new(@project.author).enter("badges.promote-to-#{text}")
reviews.post("The project has been promoted from L#{before} to L#{after}")
end
if after < before
Xia::Rank.new(@project.author).enter("badges.degrade-from-L#{before}")
reviews.post("The project has been degraded from L#{before} to L#{after}")
end
t.exec(
'DELETE FROM badge WHERE project=$1 AND text SIMILAR TO \'(newbie|L[0-9])\'',
[@project.id]
)
elsif all.length >= 5
raise Xia::Urror, 'Too many badges already'
end
t.exec(
'INSERT INTO badge (project, text) VALUES ($1, $2) RETURNING id',
[@project.id, text]
)[0]['id'].to_i
end
id = @pgsql.exec(
'INSERT INTO badge (project, text) VALUES ($1, $2) RETURNING id',
[@project.id, text]
)[0]['id'].to_i
get(id)
end
end
3 changes: 2 additions & 1 deletion objects/reviews.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

require 'loog'
require 'redcarpet'
require 'securerandom'
require_relative 'xia'
require_relative 'review'
require_relative 'rank'
Expand Down Expand Up @@ -53,7 +54,7 @@ def exists?(hash)
)[0]['count'].to_i.zero?
end

def post(text, hash)
def post(text, hash = SecureRandom.hex)
Xia::Rank.new(@project.author).enter('reviews.post')
raise Xia::Urror, 'The project is dead, can\'t review' unless @project.deleted.nil?
raise Xia::Urror, 'The review is too short' if text.length < 60 && @project.author.login != '-test-'
Expand Down
12 changes: 12 additions & 0 deletions test/test_badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ def test_attaches_badge
assert(!badge.id.nil?)
badge.detach
end

def test_promotes_and_degrades
author = Xia::Authors.new(t_pgsql).named('-test-')
projects = author.projects
project = projects.submit('github', "yegor256/cactoos#{rand(999)}")
badges = project.badges
badges.attach('L3')
assert_equal(1, badges.all.count)
assert_equal(3, badges.level)
badges.attach('newbie')
assert_equal(0, badges.level)
end
end

0 comments on commit 46c5ccc

Please sign in to comment.