]> jaekl.net Git - quanlib.git/commitdiff
Augment unit test for Book class
authorChris Jaekl <chris@jaekl.net>
Fri, 21 Jun 2024 23:19:18 +0000 (19:19 -0400)
committerChris Jaekl <chris@jaekl.net>
Fri, 21 Jun 2024 23:19:18 +0000 (19:19 -0400)
test/book_test.rb
test/conn_mock.rb [new file with mode: 0644]
test/sample_data/LouisaAlcott/LW01_Little_Women.epub [new file with mode: 0644]
test/store_mock.rb [new file with mode: 0644]
test/test_helper.rb

index 90c918a13f05505c4316ca3567d745178bafff3a..bb476c797df46250eb79d65f67e4e54efc94a686 100644 (file)
@@ -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 = "<b>Little Women: Or, Meg, Jo, Beth and Amy</b><br/><i>by Louisa May Alcott</i><br/>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 (file)
index 0000000..c090404
--- /dev/null
@@ -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 (file)
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 (file)
index 0000000..897e79a
--- /dev/null
@@ -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
index 25bf530d28dd78bb729aaca810ac74376007280e..f212ec1ab249686e778ee8c166f5899aa58b0613 100644 (file)
@@ -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