From: Chris Jaekl Date: Fri, 21 Jun 2024 23:19:18 +0000 (-0400) Subject: Augment unit test for Book class X-Git-Url: http://jaekl.net/gitweb/?a=commitdiff_plain;h=01e498f539f36b734ea2fadbe34bd2d8626af148;p=quanlib.git Augment unit test for Book class --- diff --git a/test/book_test.rb b/test/book_test.rb index 90c918a..bb476c7 100644 --- a/test/book_test.rb +++ b/test/book_test.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true require "test_helper" + require "book" +require "store_mock" class BookTest < Minitest::Test def test_that_it_can_handle_epub_and_pdf_files @@ -28,4 +30,53 @@ class BookTest < Minitest::Test assert_equal expected, Book.grouping_for_title(input) end end + + def test_load_from_file + store = StoreMock.new + store.expects(:get_series).returns(mock_series_LW) + store.connect + book = Book.new(store) + + book.load_from_file!(File.join(TestHelper::SAMPLE_DATA_PATH, "LouisaAlcott", "LW01_Little_Women.epub")) + + author = book.author + + assert_equal "LouisaAlcott", author.grouping + assert_equal "Louisa May Alcott", author.reading_order + assert_equal "Alcott, Louisa May", author.sort_order + + assert_equal "This story follows the lives of the four March sisters&emdash;Meg, Jo, Beth, and Amy&emdash;and details their coming of age.", book.description + assert_equal "en", book.language + assert_equal "Little Women: Or, Meg, Jo, Beth and Amy", book.title + assert_equal mock_series_LW.to_s, book.series_id.to_s + assert_equal 1, book.volume.to_i + end + + def test_heading + store = StoreMock.new + store.expects(:get_series).returns(mock_series_LW) + store.connect + book = Book.new(store) + + book.load_from_file!(File.join(TestHelper::SAMPLE_DATA_PATH, "LouisaAlcott", "LW01_Little_Women.epub")) + + expected = "Little Women: Or, Meg, Jo, Beth and Amy
by Louisa May Alcott
01" + actual = book.heading + + assert_equal expected, actual + end + + private + + def mock_series_LW + id = 1 + series = Series.new(id) + series.age = "ya" + series.genre = "romance" + series.grouping = "LoisaAlcott" + series.code = "LW" + series.descr = "Little Women" + + series + end end diff --git a/test/conn_mock.rb b/test/conn_mock.rb new file mode 100644 index 0000000..c090404 --- /dev/null +++ b/test/conn_mock.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class ConnMock + def initialize + @mock_connected = true + @stmts = [] + end + + def close + raise ".close() called on already closed connection" unless @mock_connected + + @mock_connected = false + end + + def exec(*args, &block) + @stmts << args + return nil + end + + def exec_params(*args, &block) + @stmts << args + return nil + end +end diff --git a/test/sample_data/LouisaAlcott/LW01_Little_Women.epub b/test/sample_data/LouisaAlcott/LW01_Little_Women.epub new file mode 100644 index 0000000..cfccd0c Binary files /dev/null and b/test/sample_data/LouisaAlcott/LW01_Little_Women.epub differ diff --git a/test/store_mock.rb b/test/store_mock.rb new file mode 100644 index 0000000..897e79a --- /dev/null +++ b/test/store_mock.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "conn_mock" +require "store" + +class StoreMock < Store + def initialize + @dbhost = "host" + @dbport = 5432 + @dbname = "dbname" + @dbuser = "dbuser" + @dbpass = "dbpass" + @basePath = "base_path" + + @conn = nil + end + + def connect + raise "Mock error: .connect called when already connected" if @conn + + @conn = ConnMock.new + end + + def disconnect + raise "Mock error: .disconnect called when not connected" unless @conn + + @conn = nil + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 25bf530..f212ec1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,3 +4,7 @@ $LOAD_PATH.unshift File.expand_path("../app", __dir__) require "minitest/autorun" require "mocha/minitest" + +module TestHelper + SAMPLE_DATA_PATH = File.expand_path("./sample_data", __dir__) +end