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 module Merged
class ActiveBase < ActiveYaml::Base class ActiveBase < ActiveYaml::Base
def save def edit_save!
ChangeSet.current.edit(self.class.name , self.change_name) ChangeSet.current.edit(self.class.name , self.change_name)
super save!()
self.class.save_all self.class.save_all
end end
def add_save def add_save!
ChangeSet.current.add(self.class.name , self.change_name) ChangeSet.current.add(self.class.name , self.change_name)
super.save() save!()
self.class.save_all self.class.save_all
end end
def delete_save!
def delete
ChangeSet.current.delete(self.class.name , self.change_name) ChangeSet.current.delete(self.class.name , self.change_name)
self.class.delete(self.id) self.class.delete(self.id)
end
def destroy
delete
self.class.save_all self.class.save_all
end end

View File

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

View File

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

View File

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

View File

@ -67,18 +67,9 @@ module Merged
cards.each_with_index{|card, index| card.index = index + 1} cards.each_with_index{|card, index| card.index = index + 1}
end end
def destroy def delete
has_cards = delete() cards.each {|card| card.delete_save! }
Section.save_all delete_save!()
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
end end
def self.new_section(template , page_id , index) def self.new_section(template , page_id , index)

View File

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

View File

@ -8,16 +8,16 @@ module Merged
def test_deletes def test_deletes
id = first.id id = first.id
first.delete first.delete_save!
assert_raises(ActiveHash::RecordNotFound) {Card.find(id) } assert_raises(ActiveHash::RecordNotFound) {Card.find(id) }
end end
def test_destroys def test_adds
id = first.id card = Card.first.section.new_card
first.delete card.add_save!
Card.reload assert_equal "NEW" , card.header
assert_raises(ActiveHash::RecordNotFound) {Card.find(id) }
end end
end 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 end
def test_destroys def test_destroys
id = index.id id = index.id
index.destroy index.delete
Section.reload Section.reload
assert_raises(ActiveHash::RecordNotFound){Page.find(id) } assert_raises(ActiveHash::RecordNotFound){Page.find(id) }
end end
def test_destroys_sections def test_destroys_sections
id = index.sections.first.id id = index.sections.first.id
index.destroy index.delete
Section.reload Section.reload
assert_raises(ActiveHash::RecordNotFound){Page.find(id) } assert_raises(ActiveHash::RecordNotFound){Page.find(id) }
end end

View File

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