From 00bc8f2ed8dc4e325ced089faebfbffb0ecdd3b0 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Wed, 16 Oct 2024 22:41:45 -0700 Subject: [PATCH] test: test coverage should be at least 50% Fixes #24. --- Gemfile | 3 ++- Gemfile.lock | 8 ++++++++ app/models/message.rb | 5 ++--- test/models/list_test.rb | 4 ++++ test/models/message_test.rb | 12 ++++++++++++ test/test_helper.rb | 5 +++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index be53209..defca27 100644 --- a/Gemfile +++ b/Gemfile @@ -68,10 +68,11 @@ group :test do # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing] gem "capybara" gem "selenium-webdriver" + gem 'simplecov' end group :production do gem 'pg' end -gem 'aws-sdk-s3', '~> 1' \ No newline at end of file +gem 'aws-sdk-s3', '~> 1' diff --git a/Gemfile.lock b/Gemfile.lock index 43720ee..a69de22 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,7 @@ GEM debug (1.9.2) irb (~> 1.10) reline (>= 0.3.8) + docile (1.4.1) drb (2.2.1) erubi (1.13.0) globalid (1.2.1) @@ -256,6 +257,12 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + simplecov (0.22.0) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.13.1) + simplecov_json_formatter (0.1.4) sprockets (4.2.1) concurrent-ruby (~> 1.0) rack (>= 2.2.4, < 4) @@ -318,6 +325,7 @@ DEPENDENCIES rails (~> 7.1.3, >= 7.1.3.4) rubocop selenium-webdriver + simplecov sprockets-rails sqlite3 (~> 1.4) stimulus-rails diff --git a/app/models/message.rb b/app/models/message.rb index 80d2364..50c9210 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -10,9 +10,8 @@ class Message < ApplicationRecord # https://blade.ruby-lang.org/ruby-talk/410000 is not. self.skip_time_zone_conversion_for_attributes = [:published_at] - def self.from_s3(list_name, list_seq) - client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION) - obj = client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}") + def self.from_s3(list_name, list_seq, s3_client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)) + obj = s3_client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}") m = self.from_string(obj.body.read) m.list_id = List.find_by_name(list_name).id diff --git a/test/models/list_test.rb b/test/models/list_test.rb index 44985f9..04836a4 100644 --- a/test/models/list_test.rb +++ b/test/models/list_test.rb @@ -4,4 +4,8 @@ class ListTest < ActiveSupport::TestCase test 'name' do assert_equal 'ruby-list', List::find_by_name('ruby-list').name end + + test 'find_by_id' do + assert_equal 'ruby-list', List.find_by_id(1).name + end end diff --git a/test/models/message_test.rb b/test/models/message_test.rb index 62095d7..15a8921 100644 --- a/test/models/message_test.rb +++ b/test/models/message_test.rb @@ -14,4 +14,16 @@ class MessageTest < ActiveSupport::TestCase assert_equal DateTime.parse('2005-12-15T19:32:40+09:00'), m.published_at end + + test 'from_s3' do + s3_client = Aws::S3::Client.new(stub_responses: true) + s3_client.stub_responses(:get_object, body: <