diff --git a/spec/avram/database_cleaner_spec.cr b/spec/avram/database_cleaner_spec.cr index 2521f79a3..7f4568811 100644 --- a/spec/avram/database_cleaner_spec.cr +++ b/spec/avram/database_cleaner_spec.cr @@ -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 diff --git a/src/avram/database.cr b/src/avram/database.cr index c843b26ea..efc3de88a 100644 --- a/src/avram/database.cr +++ b/src/avram/database.cr @@ -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 @@ -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 diff --git a/src/avram/spec_helper.cr b/src/avram/spec_helper.cr index fb61a6bd8..574f61270 100644 --- a/src/avram/spec_helper.cr +++ b/src/avram/spec_helper.cr @@ -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| @@ -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 @@ -42,14 +43,23 @@ module Avram::SpecHelper end end - private def self.use_truncation?(spec : Spec::Example::Procsy) : Bool + # TODO: + # See + 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