tests for changes and simplifying api as we go

This commit is contained in:
Torsten 2022-12-26 18:09:31 +02:00
parent 367c05b086
commit ac18050586
10 changed files with 123 additions and 54 deletions

View File

@ -1,26 +1,21 @@
module Merged
class ActiveBase < ActiveYaml::Base
def save
def edit_save!
ChangeSet.current.edit(self.class.name , self.change_name)
super
save!()
self.class.save_all
end
def add_save
def add_save!
ChangeSet.current.add(self.class.name , self.change_name)
super.save()
save!()
self.class.save_all
end
def delete
def delete_save!
ChangeSet.current.delete(self.class.name , self.change_name)
self.class.delete(self.id)
end
def destroy
delete
self.class.save_all
end

View File

@ -38,11 +38,6 @@ module Merged
CardStyle.find_by_template( self.template )
end
def delete(reindex = true)
super()
section.reset_index if reindex
end
def self.new_card(card_template , section_id , index)
data = { section_id: section_id , index: index}
template = CardStyle.find_by_template( card_template )

View File

@ -8,6 +8,10 @@ module Merged
attr_reader :adds , :edits , :deletes
def initialize
zero
end
def zero
@adds = []
@edits = []
@deletes = []

View File

@ -35,7 +35,7 @@ module Merged
PageStyle.find_by_type( type )
end
def new_section(section_template)
def new_section(section_template = nil)
section_template = "section_spacer" if section_template.blank?
section = Section.new_section(section_template, self.id , sections.length + 1)
section
@ -59,21 +59,9 @@ module Merged
sections.each_with_index{|section, index| section.index = index + 1}
end
def destroy
has_sections , has_cards = delete()
Page.save_all
if has_sections > 0
Section.save_all
Card.save_all if has_cards > 0
end
end
def delete
has_sections = sections.length
has_cards = 0
sections.each {|section| has_cards += section.delete(false) }
super
[has_sections , has_cards]
sections.each {|section| section.delete }
delete_save!
end
def save(editor)

View File

@ -67,18 +67,9 @@ module Merged
cards.each_with_index{|card, index| card.index = index + 1}
end
def destroy
has_cards = delete()
Section.save_all
Card.save_all if has_cards > 0
end
def delete( reindex = true )
has_cards = cards.length
cards.each {|card| card.delete(false) }
super()
page.reset_index if reindex
has_cards
def delete
cards.each {|card| card.delete_save! }
delete_save!()
end
def self.new_section(template , page_id , index)

View File

@ -12,10 +12,16 @@ module Merged
fields :options , :updated_at , :updated_by
def save( editor )
def edit_save( editor )
self.updated_at = Time.now
self.updated_by = editor
super()
edit_save!
end
def add_save( editor )
self.updated_at = Time.now
self.updated_by = editor
add_save!
end
def has_option?(option)

View File

@ -8,16 +8,16 @@ module Merged
def test_deletes
id = first.id
first.delete
first.delete_save!
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) }
def test_adds
card = Card.first.section.new_card
card.add_save!
assert_equal "NEW" , card.header
end
end
end

View File

@ -0,0 +1,90 @@
require "test_helper"
module Merged
module Zero
def change
ChangeSet.current
end
def setup
change.zero
end
end
class ChangeSetTest < ActiveSupport::TestCase
include Zero
def test_has_add
change.add("Section" , "name")
assert_equal "name", change.added("Section").first.last
assert_equal :Section, change.added("Section").first.first
end
def test_has_edit
change.edit("Section" , "name")
assert_equal "name", change.edited("Section").first.last
assert_equal :Section, change.edited("Section").first.first
end
def test_has_delete
change.delete("Section" , "name")
assert_equal "name", change.deleted("Section").first.last
assert_equal :Section, change.deleted("Section").first.first
end
end
class ChangeSetWriteTest < ActiveSupport::TestCase
include Cleanup
include Zero
def test_page_edit
studios = Page.first
studios.edit_save("email")
assert_equal "studios" , change.edited("Page").first.last
end
def test_section_edit
studios = Section.first
studios.edit_save("email")
assert_equal "studios:Studios" , change.edited("Section").first.last
end
def test_card_edit
studios = Card.first
studios.edit_save("email")
assert_equal "studios:Standard" , change.edited("Card").first.last
end
def test_page_new
studios = Page.new_page("new")
studios.add_save("email")
assert_equal "new" , change.added("Page").first.last
assert_nil change.edited("Page").first
end
def test_section_new
studios = Page.first.new_section()
studios.add_save("email")
assert_equal "studios:" , change.added("Section").first.last
assert_nil change.edited("Section").first
end
def test_card_new
studios = Card.first.section.new_card()
studios.add_save("email")
assert_equal "studios:NEW" , change.added("Card").first.last
assert_nil change.edited("Card").first
end
def test_page_delete
studios = Page.first
studios.delete_save!
assert_equal "studios" , change.deleted("Page").first.last
assert_nil change.edited("Page").first
end
def test_section_delete
studios = Section.first
studios.delete_save!()
assert_equal "studios:Studios" , change.deleted("Section").first.last
assert_nil change.edited("Section").first
end
def test_card_delete
studios = Card.first
studios.delete_save!()
assert_equal "studios:Standard" , change.deleted("Card").first.last
assert_nil change.edited("Card").first
end
end
end

View File

@ -15,13 +15,13 @@ module Merged
end
def test_destroys
id = index.id
index.destroy
index.delete
Section.reload
assert_raises(ActiveHash::RecordNotFound){Page.find(id) }
end
def test_destroys_sections
id = index.sections.first.id
index.destroy
index.delete
Section.reload
assert_raises(ActiveHash::RecordNotFound){Page.find(id) }
end

View File

@ -25,13 +25,13 @@ module Merged
def test_destroys
last_id = last.id
last.destroy
last.delete
Section.reload
assert_raises(ActiveHash::RecordNotFound){Section.find(last_id) }
end
def test_destroys_cards
card_id = last.cards.first.id
last.destroy
last.delete
Section.reload
assert_raises(ActiveHash::RecordNotFound){Card.find(card_id) }
end