Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add spec for callbacks #154

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions spec/granite_orm/callbacks/callbacks_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
module {{adapter.capitalize.id}}
describe "{{ adapter.id }} (callback feature)" do
describe "#save (new record)" do
it "runs before_save, before_create, after_create, after_save" do
callback = Callback.new(name: "foo")
callback.save

callback.history.to_s.strip.should eq <<-EOF
before_save
before_create
after_create
after_save
EOF
end
end

describe "#save" do
it "runs before_save, before_update, after_update, after_save" do
Callback.new(name: "foo").save
callback = Callback.first
callback.save

callback.history.to_s.strip.should eq <<-EOF
before_save
before_update
after_update
after_save
EOF
end
end

describe "#destroy" do
it "runs before_destroy, after_destroy" do
Callback.new(name: "foo").save
callback = Callback.first
callback.destroy

callback.history.to_s.strip.should eq <<-EOF
before_destroy
after_destroy
EOF
end
end

describe "an exception thrown in a hook" do
it "should not get swallowed" do
callback = Callback.new(name: "foo")
# close IO in order to raise IO::Error in callback blocks
callback.history.close

expect_raises(IO::Error, "Closed stream") do
callback.save
end
end
end
end
end
{% end %}
28 changes: 28 additions & 0 deletions spec/spec_models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ end
end
end

class Callback < Granite::ORM::Base
adapter {{ adapter_literal }}
table_name callbacks
primary id : Int64
field name : String

property history : IO::Memory = IO::Memory.new

{% for name in Granite::ORM::Callbacks::CALLBACK_NAMES %}
{{name.id}} _{{name.id}}
private def _{{name.id}}
history << "{{name.id}}\n"
end
{% end %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, this is clever


def self.drop_and_create
exec "DROP TABLE IF EXISTS #{ quoted_table_name }"
exec <<-SQL
CREATE TABLE #{ quoted_table_name } (
id {{ primary_key_sql }},
name VARCHAR(100) NOT NULL
)
SQL
end
end

Parent.drop_and_create
Teacher.drop_and_create
Student.drop_and_create
Expand All @@ -245,6 +271,7 @@ end
Review.drop_and_create
Empty.drop_and_create
ReservedWord.drop_and_create
Callback.drop_and_create

Spec.before_each do
Parent.clear
Expand All @@ -257,6 +284,7 @@ end
Review.clear
Empty.clear
ReservedWord.clear
Callback.clear
end
end
{% end %}