Skip to content

Commit

Permalink
Add missing parameters to Avram::Database.truncate method (#984)
Browse files Browse the repository at this point in the history
* Add missing parameters to `Database.truncate` method

Missed these in commit 97a2cd9.

* Allow customizing truncation behaviour for transactional specs

Adds support for restart identity and cascade during transactional
specs, using tags.

* Add specs for `Avram::Database::DatabaseCleaner#truncate`
  • Loading branch information
akadusei authored Dec 27, 2023
1 parent f2a0975 commit 84deb52
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
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 @@ -211,8 +211,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

0 comments on commit 84deb52

Please sign in to comment.