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 missing parameters to Avram::Database.truncate method #984

Merged
merged 3 commits into from
Dec 27, 2023
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
42 changes: 42 additions & 0 deletions spec/avram/database_cleaner_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,46 @@ describe "DatabaseCleaner" do
UserQuery.new.select_count.should eq 0
end
end

describe "#truncate" do
it "restarts identity", tags: Avram::SpecHelper::TRUNCATE do
count = 3

count.times do
UserFactory.create
ArticleFactory.create
end

UserQuery.new.select_count.should eq(count)
ArticleQuery.new.select_count.should eq(count)

TestDatabase.truncate(restart_identity: true)

UserQuery.new.select_count.should eq(0)
ArticleQuery.new.select_count.should eq(0)

UserFactory.create.id.should eq(1)
ArticleFactory.create.id.should eq(1)
end

it "does not restart identity", tags: Avram::SpecHelper::TRUNCATE do
count = 3

count.times do
UserFactory.create
ArticleFactory.create
end

UserQuery.new.select_count.should eq(count)
ArticleQuery.new.select_count.should eq(count)

TestDatabase.truncate(restart_identity: false)

UserQuery.new.select_count.should eq(0)
ArticleQuery.new.select_count.should eq(0)

UserFactory.create.id.should eq(count + 1)
ArticleFactory.create.id.should eq(count + 1)
end
end
end
8 changes: 4 additions & 4 deletions src/avram/database.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ abstract class Avram::Database
end

# Run a SQL `TRUNCATE` on all tables in the database
def self.truncate
new.truncate
def self.truncate(**named_args)
new.truncate(**named_args)
end

# Run a SQL `DELETE` on all tables in the database
Expand Down Expand Up @@ -208,8 +208,8 @@ abstract class Avram::Database
connection._avram_stack.last?
end

protected def truncate
DatabaseCleaner.new(self).truncate
protected def truncate(**named_args)
DatabaseCleaner.new(self).truncate(**named_args)
end

protected def delete
Expand Down
26 changes: 18 additions & 8 deletions src/avram/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Avram::SpecHelper
TRUNCATE = "truncate"
TRUNCATE = "truncate"
NO_CASCADE = "no_cascade"
NO_RESTART_IDENTITY = "no_restart_identity"

macro use_transactional_specs(*databases)
Spec.around_each do |spec|
Expand All @@ -8,10 +10,9 @@ module Avram::SpecHelper
end

def self.wrap_spec_in_transaction(spec : Spec::Example::Procsy, *databases)
if use_truncation?(spec)
if named_args = use_truncation?(spec)
spec.run
databases.each(&.truncate)
return
return databases.each(&.truncate **named_args)
end

tracked_transactions = [] of DB::Transaction
Expand Down Expand Up @@ -42,14 +43,23 @@ module Avram::SpecHelper
end
end

private def self.use_truncation?(spec : Spec::Example::Procsy) : Bool
# TODO: <https://github.com/luckyframework/avram/pull/984#issuecomment-1821577487>
# See <https://github.com/luckyframework/avram/pull/984#issuecomment-1826000231>
private def self.use_truncation?(spec : Spec::Example::Procsy)
current = spec.example

while !current.is_a?(Spec::RootContext)
temp = current.as(Spec::Item)
return true if temp.tags.try(&.includes?(TRUNCATE))

temp.tags.try do |tags|
truncate = tags.includes?(TRUNCATE)
cascade = !tags.includes?(NO_CASCADE)
restart_id = !tags.includes?(NO_RESTART_IDENTITY)

return {cascade: cascade, restart_identity: restart_id} if truncate
end

current = temp.parent
end

false
end
end