Skip to content

Commit

Permalink
Merge pull request #118 from kbrock/cleaning
Browse files Browse the repository at this point in the history
Use database_cleaner to clear out tables
  • Loading branch information
jrafanie authored May 1, 2023
2 parents ee11022 + 96c5719 commit 36eee53
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 43 deletions.
1 change: 1 addition & 0 deletions activerecord-virtual_attributes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "activerecord", "~> 6.1.0"

spec.add_development_dependency "byebug"
spec.add_development_dependency "database_cleaner-active_record", "~> 2.1"
spec.add_development_dependency "db-query-matchers"
spec.add_development_dependency "manageiq-style"
spec.add_development_dependency "rake", "~> 13.0"
Expand Down
3 changes: 3 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ set -vx

bundle install

psql -c 'create database virtual_attributes;' || echo 'db exists'
mysql -u root -e 'CREATE SCHEMA IF NOT EXISTS 'virtual_attributes';'

# Do any other automated setup that you need to do here
29 changes: 13 additions & 16 deletions spec/preload_values_spec.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
RSpec.describe "preloads_values" do
before do
Author.destroy_all
Book.destroy_all
Author.create_with_books(3).books.first.create_bookmarks(2)
Author.create_with_books(2)
end

let(:author_name) { "foo" }
let(:book_name) { "bar" }

it "detects preloaded values" do
expect(Book.select(:author_name)).to preload_values(:author_name, [author_name, author_name, author_name])
it "detects values preloaded with a value" do
expect(Book.select(:author_name)).to preload_values(:author_name, [author_name, author_name])
end

it "detects preloaded values converts to array" do
it "detects values preloaded (auto converts value to an array)" do
expect(Book.select(:author_name)).to preload_values(:author_name, author_name)
end

it "detects not preloaded" do
it "detects values preloading failure" do
expect do
expect(Book).to preload_values(:author_name, author_name)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "Expected to preload author_name but executed 3 queries instead")
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "Expected to preload author_name but executed 2 queries instead")
end

it "detects incorrect values" do
it "detects values matching failure" do
expect do
expect(Book.select(:author_name)).to preload_values(:author_name, "bogus")
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected:/)
end

it "detects not preloading values" do
it "detects values not preloaded" do
expect(Book).not_to preload_values(:author_name, author_name)
end

# NOTE: was unsure if incorrect values met or failed the expectation
# went with the core of the expectation is preloading. but matching values trumps all
it "detects not preloading values (bad value)" do
# even though we said not to preload, still expecting values to match
it "detects values not preloaded but matching failures" do
expect do
expect(Book).not_to preload_values(:author_name, "bogus")
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected:/)
end

it "detects not preloading values failure" do
it "detects values not preloaded failure (expecting not preloaded but they were)" do
expect do
expect(Book.select(:author_name)).not_to preload_values(:author_name, author_name)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "Unexpectedly preloaded author_name")
Expand Down
14 changes: 14 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "bundler/setup"
require "active_record/virtual_attributes"
require "active_record/virtual_attributes/rspec"
require "database_cleaner/active_record"
require "db-query-matchers"

Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
Expand All @@ -23,4 +24,17 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.before(:suite) do
# truncate at startup
DatabaseCleaner.clean_with :truncation
# transaction between examples (mysql requires truncation)
DatabaseCleaner.strategy = ENV["DB"].to_s.include?("mysql") ? :truncation : :transaction
end

config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
14 changes: 1 addition & 13 deletions spec/virtual_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,6 @@ def lc
end

describe ".select" do
before do
Author.delete_all
end
it "supports virtual attributes" do
Author.create(:name => "abc")
expect(Author.select(:id, :nick_or_name).first.nick_or_name).to eq("abc")
Expand All @@ -795,10 +792,6 @@ def lc
end

describe ".where" do
before do
Author.delete_all
end

it "supports virtual attributes hash syntax a" do
author = Author.create(:name => "name")
Author.create(:name => "other")
Expand Down Expand Up @@ -832,12 +825,7 @@ def lc
end

describe ".order" do
before do
Author.delete_all
authors
end

let(:authors) do
let!(:authors) do
[
Author.create(:name => "aaa"),
Author.create(:nickname => "bbb"),
Expand Down
6 changes: 0 additions & 6 deletions spec/virtual_delegates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ def self.connection
end

it "respects type" do
Author.delete_all
Book.delete_all
author = Author.create(:name => "no one of consequence")
book = author.books.create(:name => "nothing of consequence", :id => author.id)
book.photos.create(:description => 'bad')
Expand All @@ -256,8 +254,6 @@ def self.connection
end

it "handles polymorphic in" do
Author.delete_all
Book.delete_all
author = Author.create(:name => "no one of consequence")
author.books.create(:name => "nothing of consequence", :id => author.id)
author.photos.create(:description => 'good')
Expand All @@ -267,8 +263,6 @@ def self.connection
end

it "handles polymorphic or" do
Author.delete_all
Book.delete_all
author = Author.create(:name => "no one of consequence")
author.books.create(:name => "nothing of consequence", :id => author.id)
author.photos.create(:description => 'good')
Expand Down
2 changes: 0 additions & 2 deletions spec/virtual_includes_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
RSpec.describe ActiveRecord::VirtualAttributes::VirtualIncludes do
before do
Author.destroy_all
Book.destroy_all
Author.create_with_books(3).books.first.create_bookmarks(2)
end

Expand Down
6 changes: 0 additions & 6 deletions spec/virtual_total_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
RSpec.describe VirtualAttributes::VirtualTotal do
before do
Author.delete_all
Book.delete_all
Bookmark.delete_all
end

describe ".select" do
it "supports virtual_totals" do
Author.select(:id, :total_books).first
Expand Down

0 comments on commit 36eee53

Please sign in to comment.