From f46634e3d19f48015274d9bda5c3124f7b36620b Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 23 Dec 2022 14:40:05 +0200 Subject: [PATCH] moving model spec to good old fashioed minitests --- spec/models/merged/card_spec.rb | 45 ----------- spec/models/merged/card_style_spec.rb | 17 ----- spec/models/merged/image_spec.rb | 25 ------- spec/models/merged/option_definition_spec.rb | 20 ----- spec/models/merged/page_spec.rb | 61 --------------- spec/models/merged/page_style_spec.rb | 19 ----- spec/models/merged/section_spec.rb | 79 -------------------- spec/models/merged/section_style_spec.rb | 28 ------- test/helpers/card_helper.rb | 11 +++ test/helpers/cleanup.rb | 11 +++ test/helpers/section_helper.rb | 11 +++ test/models/merged/card_style_test.rb | 19 +++++ test/models/merged/card_test.rb | 33 ++++++++ test/models/merged/card_write_test.rb | 23 ++++++ test/models/merged/image_test.rb | 28 +++++++ test/models/merged/option_definition_test.rb | 23 ++++++ test/models/merged/page_style_test.rb | 21 ++++++ test/models/merged/page_test.rb | 38 ++++++++++ test/models/merged/page_write_test.rb | 40 ++++++++++ test/models/merged/section_style_test.rb | 26 +++++++ test/models/merged/section_test.rb | 46 ++++++++++++ test/models/merged/section_write_test.rb | 40 ++++++++++ test/test_helper.rb | 2 + 23 files changed, 372 insertions(+), 294 deletions(-) delete mode 100644 spec/models/merged/card_spec.rb delete mode 100644 spec/models/merged/card_style_spec.rb delete mode 100644 spec/models/merged/image_spec.rb delete mode 100644 spec/models/merged/option_definition_spec.rb delete mode 100644 spec/models/merged/page_spec.rb delete mode 100644 spec/models/merged/page_style_spec.rb delete mode 100644 spec/models/merged/section_spec.rb delete mode 100644 spec/models/merged/section_style_spec.rb create mode 100644 test/helpers/card_helper.rb create mode 100644 test/helpers/cleanup.rb create mode 100644 test/helpers/section_helper.rb create mode 100644 test/models/merged/card_style_test.rb create mode 100644 test/models/merged/card_test.rb create mode 100644 test/models/merged/card_write_test.rb create mode 100644 test/models/merged/image_test.rb create mode 100644 test/models/merged/option_definition_test.rb create mode 100644 test/models/merged/page_style_test.rb create mode 100644 test/models/merged/page_test.rb create mode 100644 test/models/merged/page_write_test.rb create mode 100644 test/models/merged/section_style_test.rb create mode 100644 test/models/merged/section_test.rb create mode 100644 test/models/merged/section_write_test.rb diff --git a/spec/models/merged/card_spec.rb b/spec/models/merged/card_spec.rb deleted file mode 100644 index 7e2cd65..0000000 --- a/spec/models/merged/card_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'rails_helper' -require "git" - -module Merged - RSpec.describe Card, type: :model do - let(:first) {Card.first} - - it "has Card.all" do - expect(Card.all.length).to be 20 - end - it "has cards" do - expect(first.class).to be Card - end - it "has index" do - expect(first.index).to eq 1 - end - - it "first has no previous" do - expect(first.index).to be 1 - expect(first.previous_card).to be nil - end - it "first has next" do - expect(first.next_card.index).to be 2 - end - - it "create a new" do - card = Card.new_card("card_normal_square" , 1 , 1) - expect(card.index).to eq 1 - end - - it "deletes " do - id = first.id - first.delete - expect{Card.find(id) }.to raise_error(ActiveHash::RecordNotFound) - end - - it "destroys " do - id = first.id - first.delete - Card.reload - expect{Card.find(id) }.to raise_error(ActiveHash::RecordNotFound) - end - - end -end diff --git a/spec/models/merged/card_style_spec.rb b/spec/models/merged/card_style_spec.rb deleted file mode 100644 index ba2316f..0000000 --- a/spec/models/merged/card_style_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe CardStyle, type: :model do - let(:first_card) {CardStyle.all.first} - - it "has .all" do - expect(CardStyle.all.length).to be 5 - end - - it "has fields" do - expect(first_card.fields.class).to be Array - expect(first_card.fields.length).to be 2 - expect(first_card.fields.first).to eq "header" - end - end -end diff --git a/spec/models/merged/image_spec.rb b/spec/models/merged/image_spec.rb deleted file mode 100644 index 34ea4c4..0000000 --- a/spec/models/merged/image_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe Image, type: :model do - let(:first) {Image.first} - - it "has Image.all" do - expect(Image.all.length).to be 17 - end - it "has image" do - expect(first.class).to be Image - end - it "has name" do - expect(first.name).to eq "Common spaces" - end - it "has height and width" do - expect(first.height).to eq 300 - expect(first.width).to eq 560 - end - it "has height and width" do - expect(first.aspect_ratio).to eq [13,7] - end - - end -end diff --git a/spec/models/merged/option_definition_spec.rb b/spec/models/merged/option_definition_spec.rb deleted file mode 100644 index b233a1e..0000000 --- a/spec/models/merged/option_definition_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe OptionDefinition, type: :model do - let(:first) {OptionDefinition.first} - - it "has OptionDefinition.first" do - expect(OptionDefinition.first.class).to be OptionDefinition - end - it "there are options" do - expect(OptionDefinition.all.length).to be 18 - end - it "has option objects" do - expect(first.class).to be OptionDefinition - end - it "has values" do - expect(first.values.class).to be Array - end - end -end diff --git a/spec/models/merged/page_spec.rb b/spec/models/merged/page_spec.rb deleted file mode 100644 index c34ff74..0000000 --- a/spec/models/merged/page_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe Page, type: :model do - let(:index) {Page.find_by_name('index')} - - it "has Pages.all" do - expect(Page.all.length).to be 2 - end - it "has index page" do - expect(index.class).to be Page - end - it "has sections" do - expect(index.sections.length).to be 10 - end - it "has section array" do - expect(index.sections.first.class).to be Section - end - it "has section indexes" do - index.sections.each_with_index do |section, index| - expect(section.index).to be index + 1 # because we have human index - end - end - it "deletes " do - id = index.id - index.delete - expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) - end - it "destroys " do - id = index.id - index.destroy - Section.reload - expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) - end - it "destroys sections" do - id = index.sections.first.id - index.destroy - Section.reload - expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) - end - it "creates simple section" do - s = index.new_section("section_spacer") - expect(s).not_to be nil - expect(s.template).to eq "section_spacer" - end - it "creates section with right index" do - should_be = index.sections.last.index + 1 - s = index.new_section("section_spacer") - expect(s.index).to eq should_be - end - it "creates page" do - name = "randomname" - page = Page.new_page( name) - expect(page.name).to eq name - end - - it "has type" do - expect(index.type).to eq "page" - end - end -end diff --git a/spec/models/merged/page_style_spec.rb b/spec/models/merged/page_style_spec.rb deleted file mode 100644 index 703c865..0000000 --- a/spec/models/merged/page_style_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe PageStyle, type: :model do - let(:first) {PageStyle.all.first} - - it "finds stye" do - spacer = PageStyle.find_by_type("page") - expect(spacer).not_to be nil - end - - it "has Style.sections" do - expect(PageStyle.all.length).to be 1 - end - it "Spacer has no fields" do - expect(first.description).not_to be nil - end - end -end diff --git a/spec/models/merged/section_spec.rb b/spec/models/merged/section_spec.rb deleted file mode 100644 index fd3e15e..0000000 --- a/spec/models/merged/section_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe Section, type: :model do - let(:first) {Section.first} - let(:last) {Section.last} - - it "has Sections.all" do - expect(Section.all.length).to be 14 - end - it "has index page" do - expect(last.class).to be Section - end - it "has index" do - expect(last.index).to eq 10 - end - it "has cards" do - expect(last.cards.length).to eq 5 - end - it "has cards array" do - expect(last.cards.class).to be ActiveHash::Relation - end - it "has options" do - expect(last.options.class).to be Hash - expect(first.options.length).to be 4 - end - it "has option_definitions" do - expect(last.option_definitions.class).to be Array - expect(last.option_definitions.length).to be 4 - expect(last.option_definitions.second.class).to be OptionDefinition - expect(last.option_definitions.second.name).to eq "handler" - end - it "last has previous" do - expect(last.previous_section.index).to be 9 - end - it "first has no previous" do - expect(first.index).to be 1 - expect(first.previous_section).to be nil - end - it "first has next" do - expect(first.next_section.index).to be 2 - end - it "last has no next" do - expect(last.next_section).to be nil - end - - it "creates new spacer section" do - s = Section.new_section("section_spacer" , 1 , 1) - expect(s.template).to eq "section_spacer" - end - - it "creates card with right index" do - s = Section.find_by_template("section_cards") - length = s.cards.length - c = s.new_card - expect(c.index).to eq length + 1 - end - - it "deletes " do - last_id = last.id - last.delete - expect{Section.find(last_id) }.to raise_error(ActiveHash::RecordNotFound) - end - - it "destroys " do - last_id = last.id - last.destroy - Section.reload - expect{Section.find(last_id) }.to raise_error(ActiveHash::RecordNotFound) - end - it "destroys cards" do - card_id = last.cards.first.id - last.destroy - Section.reload - expect{Card.find(card_id) }.to raise_error(ActiveHash::RecordNotFound) - end - - end -end diff --git a/spec/models/merged/section_style_spec.rb b/spec/models/merged/section_style_spec.rb deleted file mode 100644 index 2344c61..0000000 --- a/spec/models/merged/section_style_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'rails_helper' - -module Merged - RSpec.describe SectionStyle, type: :model do - let(:first_section) {SectionStyle.all.first} - - it "finds stye" do - spacer = SectionStyle.find_by_template("section_spacer") - expect(spacer).not_to be nil - end - - it "has Style.sections" do - expect(SectionStyle.all.length).to be 7 - end - it "Finds section by template" do - spacer = SectionStyle.find_by_template("section_spacer") - expect(spacer).not_to eq nil - end - it "Spacer has no fields" do - spacer = SectionStyle.find_by_template("section_spacer") - expect(spacer.fields).to be nil - end - it "Spacer has no fields" do - spacer = SectionStyle.find_by_template("section_spacer") - expect(spacer.fields).to be nil - end - end -end diff --git a/test/helpers/card_helper.rb b/test/helpers/card_helper.rb new file mode 100644 index 0000000..8b403fd --- /dev/null +++ b/test/helpers/card_helper.rb @@ -0,0 +1,11 @@ +module Merged + module CardHelper + def first + Card.first + end + def last + Card.last + end + + end +end diff --git a/test/helpers/cleanup.rb b/test/helpers/cleanup.rb new file mode 100644 index 0000000..70c4dcd --- /dev/null +++ b/test/helpers/cleanup.rb @@ -0,0 +1,11 @@ +require "git" + +module Merged + module Cleanup + def teardown + git = Git.open(Engine.root) + git.checkout_file("HEAD" , "test/dummy/merged") + [Page, Section, Card].each { |m| m.reload(true) } + end + end +end diff --git a/test/helpers/section_helper.rb b/test/helpers/section_helper.rb new file mode 100644 index 0000000..6e35da2 --- /dev/null +++ b/test/helpers/section_helper.rb @@ -0,0 +1,11 @@ +module Merged + module SectionHelper + def first + Section.first + end + def last + Section.last + end + + end +end diff --git a/test/models/merged/card_style_test.rb b/test/models/merged/card_style_test.rb new file mode 100644 index 0000000..b6cb4d5 --- /dev/null +++ b/test/models/merged/card_style_test.rb @@ -0,0 +1,19 @@ +require "test_helper" + +module Merged + class PageTest < ActiveSupport::TestCase + def first_card + CardStyle.all.first + end + + def test_has_all + assert_equal CardStyle.all.length , 5 + end + + def test_has_fields + assert_equal first_card.fields.class , Array + assert_equal first_card.fields.length , 2 + assert_equal first_card.fields.first , "header" + end + end +end diff --git a/test/models/merged/card_test.rb b/test/models/merged/card_test.rb new file mode 100644 index 0000000..b302b2a --- /dev/null +++ b/test/models/merged/card_test.rb @@ -0,0 +1,33 @@ +require "git" +require "test_helper" + +module Merged + class CardTest < ActiveSupport::TestCase + include CardHelper + + def test_has_all + assert_equal 20 , Card.all.length + end + def test_has_cards + assert_equal first.class , Card + end + def test_has_index + assert_equal first.index , 1 + end + + def test_first_has_no_previous + assert_equal first.index , 1 + assert_nil first.previous_card + end + def test_first_has_next + assert_equal first.next_card.index , 2 + end + + def test_create_new + card = Card.new_card("card_normal_square" , 1 , 1) + assert_equal card.index , 1 + end + + + end +end diff --git a/test/models/merged/card_write_test.rb b/test/models/merged/card_write_test.rb new file mode 100644 index 0000000..aaae0f3 --- /dev/null +++ b/test/models/merged/card_write_test.rb @@ -0,0 +1,23 @@ +require "git" +require "test_helper" + +module Merged + class CardWriteTest < ActiveSupport::TestCase + include CardHelper + include Cleanup + + def test_deletes + id = first.id + first.delete + assert_raises(ActiveHash::RecordNotFound) {Card.find(id) } + end + + def test_destroys + id = first.id + first.delete + Card.reload + assert_raises(ActiveHash::RecordNotFound) {Card.find(id) } + end + + end +end diff --git a/test/models/merged/image_test.rb b/test/models/merged/image_test.rb new file mode 100644 index 0000000..e7c3fed --- /dev/null +++ b/test/models/merged/image_test.rb @@ -0,0 +1,28 @@ +require "test_helper" + +module Merged + class ImageTest < ActiveSupport::TestCase + + def first + Image.first + end + + def test_has_all + assert_equal Image.all.length , 17 + end + def test_has_image + assert_equal first.class , Image + end + def test_has_name + assert_equal first.name , "Common spaces" + end + def test_has_height_and_width + assert_equal first.height , 300 + assert_equal first.width , 560 + end + def test_aspect + assert_equal first.aspect_ratio , [13,7] + end + + end +end diff --git a/test/models/merged/option_definition_test.rb b/test/models/merged/option_definition_test.rb new file mode 100644 index 0000000..349d106 --- /dev/null +++ b/test/models/merged/option_definition_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +module Merged + class OptionDefinitionTest < ActiveSupport::TestCase + + def first + OptionDefinition.first + end + + def test_has_first + assert_equal OptionDefinition.first.class , OptionDefinition + end + def test_there_options + assert_equal OptionDefinition.all.length , 18 + end + def test_has_option_objects + assert_equal first.class , OptionDefinition + end + def test_has_values + assert_equal first.values.class , Array + end + end +end diff --git a/test/models/merged/page_style_test.rb b/test/models/merged/page_style_test.rb new file mode 100644 index 0000000..fdd5be9 --- /dev/null +++ b/test/models/merged/page_style_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +module Merged + class PageTest < ActiveSupport::TestCase + def first + PageStyle.all.first + end + + def test_finds_stye + spacer = PageStyle.find_by_type("page") + assert spacer + end + + def test_has_sections + assert_equal PageStyle.all.length , 1 + end + def test_Spacer_has_no_fields + assert first.description + end + end +end diff --git a/test/models/merged/page_test.rb b/test/models/merged/page_test.rb new file mode 100644 index 0000000..56062b2 --- /dev/null +++ b/test/models/merged/page_test.rb @@ -0,0 +1,38 @@ +require "test_helper" + +module Merged + class PageTest < ActiveSupport::TestCase + + def index + Page.find_by_name('index') + end + + def test_all + assert_equal 2 , Page.all.length + end + def test_creates_page + name = "randomname" + page = Page.new_page( name) + assert_equal page.name , name + end + + def test_has_type + assert_equal index.type , "page" + end + + def test_has_index_page + assert_equal index.class , Page + end + def test_has_sections + assert_equal index.sections.length , 10 + end + def test_has_section_array + assert_equal index.sections.first.class , Section + end + def test_has_section_indexes + index.sections.each_with_index do |section, index| + assert_equal section.index , index + 1 # because we have human index + end + end + end +end diff --git a/test/models/merged/page_write_test.rb b/test/models/merged/page_write_test.rb new file mode 100644 index 0000000..2a930fe --- /dev/null +++ b/test/models/merged/page_write_test.rb @@ -0,0 +1,40 @@ +require "test_helper" + +module Merged + class PageWriteTests < ActiveSupport::TestCase + include Cleanup + + def index + Page.find_by_name('index') + end + + def test_deletes + id = index.id + index.delete + assert_raises(ActiveHash::RecordNotFound){Page.find(id) } + end + def test_destroys + id = index.id + index.destroy + Section.reload + assert_raises(ActiveHash::RecordNotFound){Page.find(id) } + end + def test_destroys_sections + id = index.sections.first.id + index.destroy + Section.reload + assert_raises(ActiveHash::RecordNotFound){Page.find(id) } + end + def test_creates_simple_section + s = index.new_section("section_spacer") + assert s + assert_equal s.template , "section_spacer" + end + def test_creates_section_with_right_index + should_be = index.sections.last.index + 1 + s = index.new_section("section_spacer") + assert_equal s.index , should_be + end + + end +end diff --git a/test/models/merged/section_style_test.rb b/test/models/merged/section_style_test.rb new file mode 100644 index 0000000..15d46e0 --- /dev/null +++ b/test/models/merged/section_style_test.rb @@ -0,0 +1,26 @@ +require "test_helper" + +module Merged + class SectionStyleTest < ActiveSupport::TestCase + + def first_section + SectionStyle.all.first + end + + def test_finds_stye + spacer = SectionStyle.find_by_template("section_spacer") + assert spacer + end + def test_has_sections + assert_equal SectionStyle.all.length , 7 + end + def test_finds_section_by_template + spacer = SectionStyle.find_by_template("section_spacer") + assert spacer + end + def test_Spacer_has_no_fields + spacer = SectionStyle.find_by_template("section_spacer") + assert_nil spacer.fields + end + end +end diff --git a/test/models/merged/section_test.rb b/test/models/merged/section_test.rb new file mode 100644 index 0000000..3c657f6 --- /dev/null +++ b/test/models/merged/section_test.rb @@ -0,0 +1,46 @@ +require "test_helper" + +module Merged + class SectionTest < ActiveSupport::TestCase + include SectionHelper + + def test_has_all + assert_equal Section.all.length , 14 + end + def test_has_index_page + assert_equal last.class , Section + end + def test_has_index + assert_equal last.index , 10 + end + def test_has_cards + assert_equal last.cards.length , 5 + end + def test_has_cards_array + assert_equal last.cards.class , ActiveHash::Relation + end + def test_has_options + assert_equal last.options.class , Hash + assert_equal first.options.length , 4 + end + def test_has_option_definitions + assert_equal last.option_definitions.class , Array + assert_equal last.option_definitions.length , 4 + assert_equal last.option_definitions.second.class , OptionDefinition + assert_equal last.option_definitions.second.name , "handler" + end + def test_last_has_previous + assert_equal last.previous_section.index , 9 + end + def test_first_has_no_previous + assert_equal first.index , 1 + assert_nil first.previous_section + end + def test_first_has_next + assert_equal first.next_section.index , 2 + end + def test_last_has_no_next + assert_nil last.next_section + end + end +end diff --git a/test/models/merged/section_write_test.rb b/test/models/merged/section_write_test.rb new file mode 100644 index 0000000..39a88f3 --- /dev/null +++ b/test/models/merged/section_write_test.rb @@ -0,0 +1,40 @@ +require "test_helper" + +module Merged + class SectionTest < ActiveSupport::TestCase + include SectionHelper + include Cleanup + + def test_creates_new_spacer_section + s = Section.new_section("section_spacer" , 1 , 1) + assert_equal s.template , "section_spacer" + end + + def test_creates_card_with_right_index + s = Section.find_by_template("section_cards") + length = s.cards.length + c = s.new_card + assert_equal c.index , length + 1 + end + + def test_deletes + last_id = last.id + last.delete + assert_raises(ActiveHash::RecordNotFound){Section.find(last_id) } + end + + def test_destroys + last_id = last.id + last.destroy + Section.reload + assert_raises(ActiveHash::RecordNotFound){Section.find(last_id) } + end + def test_destroys_cards + card_id = last.cards.first.id + last.destroy + Section.reload + assert_raises(ActiveHash::RecordNotFound){Card.find(card_id) } + end + + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4f0da57..3cdb072 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,8 @@ require 'capybara/minitest' require "devise" require "devise/test/integration_helpers" +Dir[Merged::Engine.root.join("test/helpers/**/*.rb")].each { |f| require f } + class ActionDispatch::IntegrationTest #include Devise::Test::IntegrationHelpers #include FactoryBot::Syntax::Methods