Skip to content

Commit 84deb52

Browse files
authored
Add missing parameters to Avram::Database.truncate method (#984)
* 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`
1 parent f2a0975 commit 84deb52

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

spec/avram/database_cleaner_spec.cr

+42
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,46 @@ describe "DatabaseCleaner" do
99
UserQuery.new.select_count.should eq 0
1010
end
1111
end
12+
13+
describe "#truncate" do
14+
it "restarts identity", tags: Avram::SpecHelper::TRUNCATE do
15+
count = 3
16+
17+
count.times do
18+
UserFactory.create
19+
ArticleFactory.create
20+
end
21+
22+
UserQuery.new.select_count.should eq(count)
23+
ArticleQuery.new.select_count.should eq(count)
24+
25+
TestDatabase.truncate(restart_identity: true)
26+
27+
UserQuery.new.select_count.should eq(0)
28+
ArticleQuery.new.select_count.should eq(0)
29+
30+
UserFactory.create.id.should eq(1)
31+
ArticleFactory.create.id.should eq(1)
32+
end
33+
34+
it "does not restart identity", tags: Avram::SpecHelper::TRUNCATE do
35+
count = 3
36+
37+
count.times do
38+
UserFactory.create
39+
ArticleFactory.create
40+
end
41+
42+
UserQuery.new.select_count.should eq(count)
43+
ArticleQuery.new.select_count.should eq(count)
44+
45+
TestDatabase.truncate(restart_identity: false)
46+
47+
UserQuery.new.select_count.should eq(0)
48+
ArticleQuery.new.select_count.should eq(0)
49+
50+
UserFactory.create.id.should eq(count + 1)
51+
ArticleFactory.create.id.should eq(count + 1)
52+
end
53+
end
1254
end

src/avram/database.cr

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ abstract class Avram::Database
4141
end
4242

4343
# Run a SQL `TRUNCATE` on all tables in the database
44-
def self.truncate
45-
new.truncate
44+
def self.truncate(**named_args)
45+
new.truncate(**named_args)
4646
end
4747

4848
# Run a SQL `DELETE` on all tables in the database
@@ -211,8 +211,8 @@ abstract class Avram::Database
211211
connection._avram_stack.last?
212212
end
213213

214-
protected def truncate
215-
DatabaseCleaner.new(self).truncate
214+
protected def truncate(**named_args)
215+
DatabaseCleaner.new(self).truncate(**named_args)
216216
end
217217

218218
protected def delete

src/avram/spec_helper.cr

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Avram::SpecHelper
2-
TRUNCATE = "truncate"
2+
TRUNCATE = "truncate"
3+
NO_CASCADE = "no_cascade"
4+
NO_RESTART_IDENTITY = "no_restart_identity"
35

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

1012
def self.wrap_spec_in_transaction(spec : Spec::Example::Procsy, *databases)
11-
if use_truncation?(spec)
13+
if named_args = use_truncation?(spec)
1214
spec.run
13-
databases.each(&.truncate)
14-
return
15+
return databases.each(&.truncate **named_args)
1516
end
1617

1718
tracked_transactions = [] of DB::Transaction
@@ -42,14 +43,23 @@ module Avram::SpecHelper
4243
end
4344
end
4445

45-
private def self.use_truncation?(spec : Spec::Example::Procsy) : Bool
46+
# TODO: <https://github.com/luckyframework/avram/pull/984#issuecomment-1821577487>
47+
# See <https://github.com/luckyframework/avram/pull/984#issuecomment-1826000231>
48+
private def self.use_truncation?(spec : Spec::Example::Procsy)
4649
current = spec.example
50+
4751
while !current.is_a?(Spec::RootContext)
4852
temp = current.as(Spec::Item)
49-
return true if temp.tags.try(&.includes?(TRUNCATE))
53+
54+
temp.tags.try do |tags|
55+
truncate = tags.includes?(TRUNCATE)
56+
cascade = !tags.includes?(NO_CASCADE)
57+
restart_id = !tags.includes?(NO_RESTART_IDENTITY)
58+
59+
return {cascade: cascade, restart_identity: restart_id} if truncate
60+
end
61+
5062
current = temp.parent
5163
end
52-
53-
false
5464
end
5565
end

0 commit comments

Comments
 (0)