Skip to content

Commit

Permalink
#5 more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 31, 2024
1 parent d51ac77 commit 3083b70
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/netbout/bout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def take(id)
end

def tags
Netbout::Tags.new(@token, self)
Netbout::Tags.new(@iri, @token, id)
end
end
2 changes: 1 addition & 1 deletion lib/netbout/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def post(hash)

def checked(rsp)
code = rsp.response_code.to_i
raise "Invalid response #{code}: '#{rsp.headers['x-netbout-flash']}'" if code != 200 && code != 303
raise "Invalid response #{code}: '#{rsp.headers['x-netbout-flash']}'" if code != 200 && code != 303 && code != 302
rsp
end

Expand Down
11 changes: 8 additions & 3 deletions lib/netbout/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ def text
end

def flags
[]
JSON.parse(Netbout::Http.new(@iri.append('/flags').append(id), @token).get.response_body)
end

def attach(flag); end
def attach(flag)
Netbout::Http.new(@iri.append('/m').append(id).append('/attach'), @token)
.post('name' => flag)
end

def detach(flag); end
def detach(flag)
Netbout::Http.new(@iri.append('/m').append(id).append('/detach').add(name: flag), @token).get
end
end
16 changes: 12 additions & 4 deletions lib/netbout/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ def initialize(iri, token, query)
end

def each
rsp = Netbout::Http.new(@iri.append('/search').add(q: @query), @token).get
json = JSON.parse(rsp.response_body)
json.each do |h|
yield Netbout::Message.new(@iri, token, h['id'])
entry = @iri.append('/search').add(q: @query)
offset = 0
loop do
rsp = Netbout::Http.new(entry.over(offset: offset), @token).get
json = JSON.parse(rsp.response_body)
seen = 0
json.each do |h|
yield Netbout::Message.new(@iri, token, h['id'])
seen += 1
end
offset += seen
break if seen.zero?
end
end
end
15 changes: 10 additions & 5 deletions lib/netbout/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require_relative 'http'

# Tags.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class Netbout::Tags
def initialize(iri, token, bout)
def initialize(iri, token, id)
@iri = iri
@token = token
@bout = bout
@id = id
end

def each
[]
def to_a
JSON.parse(Netbout::Http.new(@iri.append('/tags').append(@id), @token).get.response_body)
end

def put(key, value); end
def put(key, value)
Netbout::Http.new(@iri.append('/b').append(@id).append('/tag'), @token)
.post('name' => key, 'value' => value)
end
end
10 changes: 7 additions & 3 deletions test/netbout/test_bout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
require 'minitest/autorun'
require_relative '../../lib/netbout'

# Netbout main module test.
# Test of Bout.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class TestBout < Minitest::Test
def test_start_and_post
inbox = Netbout::Inbox.new('test')
bout = inbox.start('Hello!')
bout = inbox.start('Hello, друг!')
assert(bout.id.positive?)
msg = bout.post('How are you?')
assert(bout.title.include?('друг'))
assert_equal('?test', bout.owner)
msg = bout.post('How are you, друг?')
assert(msg.id.positive?)
assert(msg.text.include?('друг'))
assert_equal('?test', msg.author)
end
end
42 changes: 42 additions & 0 deletions test/netbout/test_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

#
# Copyright (c) 2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'minitest/autorun'
require_relative '../../lib/netbout'

# Test of Message.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class TestMessage < Minitest::Test
def test_attach_flag
inbox = Netbout::Inbox.new('test')
bout = inbox.start('Hello, друг!')
msg = bout.post('oops')
tag = 'hey-you'
msg.attach(tag)
assert_equal(tag, msg.flags.first['name'])
msg.detach(tag)
assert(msg.flags.empty?)
end
end
43 changes: 43 additions & 0 deletions test/netbout/test_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

#
# Copyright (c) 2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'minitest/autorun'
require_relative '../../lib/netbout'

# Test of Tags.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2024 Yegor Bugayenko
# License:: MIT
class TestTags < Minitest::Test
def test_put_and_list
inbox = Netbout::Inbox.new('test')
bout = inbox.start('Hello, друг!')
assert(bout.id.positive?)
key = 'tag1'
value = 'привет!'
bout.tags.put(key, value)
t = inbox.take(bout.id).tags.to_a.first
assert_equal(key, t['name'])
assert_equal(value, t['value'])
end
end

0 comments on commit 3083b70

Please sign in to comment.