Skip to content

Commit

Permalink
#1206 more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 30, 2024
1 parent bae32cb commit 434b13b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
16 changes: 12 additions & 4 deletions objects/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def initialize(pgsql, human, message)
end

def take(name)
raise Nb::Urror, "#{@human} can't take a flag from bout ##{@id}" unless @message.mine?
raise Nb::Urror, "#{@human} can't take a flag from message ##{@id}" unless @message.mine?
require_relative 'flag'
Nb::Flag.new(@pgsql, @message, name)
end

def attach(name)
raise Nb::Urror, "#{@human} can't attach a flag to bout ##{@id}" unless @message.mine?
raise Nb::Urror, "#{@human} can't attach a flag to message ##{@id}" unless @message.mine?
raise Nb::Urror, "Invalid flag '#{name}'" unless name.match?(/^[a-z][a-z0-9-]+$/)
@pgsql.exec(
'INSERT INTO flag (message, name, author) VALUES ($1, $2, $3)',
Expand All @@ -54,15 +54,23 @@ def attach(name)
take(name)
end

def detach(name)
raise Nb::Urror, "#{@human} can't detach a flag from message ##{@id}" unless @message.mine?
@pgsql.exec(
'DELETE FROM flag WHERE message=$1 AND name=$2',
[@message.id, name]
)
end

def each
raise Nb::Urror, "#{@human} can't list flags in bout ##{@id}" unless @message.mine?
raise Nb::Urror, "#{@human} can't list flags in message ##{@id}" unless @message.mine?
@pgsql.exec('SELECT * FROM flag WHERE message=$1', [@message.id]).each do |row|
yield take(row['name'])
end
end

def to_a
raise Nb::Urror, "#{@human} can't serialize flags in bout ##{@id}" unless @message.mine?
raise Nb::Urror, "#{@human} can't serialize flags in message ##{@id}" unless @message.mine?
array = []
each { |f| array << f.to_h }
array
Expand Down
55 changes: 50 additions & 5 deletions test/test_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,63 @@ def app
end

def test_bout
identity = test_name
human = Nb::Humans.new(test_pgsql).take(identity).create
token = human.tokens.get
header('X-Netbout-Token', token)
human = test_user
bout = human.bouts.start('hey, друг!')
get("/bout/#{bout.id}")
json = JSON.parse(last_response.body)
assert_equal(bout.id, json['id'])
assert(Time.parse(json['created']) < Time.now)
assert_equal(identity, json['owner'])
assert_equal(human.identity, json['owner'])
assert(json['title'].include?('друг'))
assert(json['tags'].empty?)
assert(json['guests'].empty?)
end

def test_message
human = test_user
bout = human.bouts.start('hello, друг!')
msg = bout.post('how are you, товарищ?')
get("/message/#{msg.id}")
json = JSON.parse(last_response.body)
assert_equal(msg.id, json['id'])
assert(Time.parse(json['created']) < Time.now)
assert_equal(human.identity, json['author'])
assert(json['text'].include?('товарищ'))
assert(json['flags'].empty?)
end

def test_tags
human = test_user
bout = human.bouts.start('hey, друг!')
bout.tags.put('foo1', 'как дела?')
get("/tags/#{bout.id}")
json = JSON.parse(last_response.body)
assert_equal(1, json.size)
assert_equal('foo1', json.first['name'])
assert_equal('как дела?', json.first['value'])
end

def test_flags
human = test_user
bout = human.bouts.start('hello, друг!')
msg = bout.post('just a test')
msg.flags.attach('bar')
get("/flags/#{msg.id}")
json = JSON.parse(last_response.body)
assert_equal(1, json.size)
assert_equal('bar', json.first['name'])
msg.flags.detach('bar')
get("/flags/#{msg.id}")
json = JSON.parse(last_response.body)
assert(json.empty?)
end

private

def test_user
human = Nb::Humans.new(test_pgsql).take(test_name).create
token = human.tokens.get
header('X-Netbout-Token', token)
human
end
end

0 comments on commit 434b13b

Please sign in to comment.