Merge pull request #970 from airblade/convert_more_tests_to_rspec

Convert more tests to rspec
This commit is contained in:
Jared Beck 2017-05-30 01:54:30 -04:00 committed by GitHub
commit af324eb0dc
39 changed files with 197 additions and 227 deletions

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe ArticlesController, type: :controller do
describe "PaperTrail.enabled_for_controller?" do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
require "generator_spec/test_case"
require File.expand_path("../../../lib/generators/paper_trail/install_generator", __FILE__)

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Animal, type: :model, versioning: true do
it "baseline test setup" do

View File

@ -1,5 +1,5 @@
require "rails_helper"
require Rails.root.join("..", "custom_json_serializer")
require "spec_helper"
require "support/custom_json_serializer"
RSpec.describe Boolit, type: :model, versioning: true do
subject { Boolit.create! }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe CallbackModifier, type: :model, versioning: true do
describe "paper_trail_on_destroy" do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Car, type: :model do
it { is_expected.to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe CustomPrimaryKeyRecord, type: :model do
it { is_expected.to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Document, type: :model do
describe "`have_a_version_with` matcher", versioning: true do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Gadget, type: :model do
it { is_expected.to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe JoinedVersion, type: :model, versioning: true do
let(:widget) { Widget.create!(name: FFaker::Name.name) }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
# The `json_versions` table tests postgres' `json` data type. So, that
# table is only created when testing against postgres and ActiveRecord >= 4.

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module Kitchen
RSpec.describe Banana, type: :model do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe NotOnUpdate, type: :model do
describe "#touch_with_version", versioning: true do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe PostWithStatus, type: :model do
with_versioning do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Skipper, type: :model, versioning: true do
it { is_expected.to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Thing, type: :model do
it { is_expected.to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Vehicle, type: :model do
it { is_expected.not_to be_versioned }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
::RSpec.describe Version, type: :model do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe Widget, type: :model do
describe "`be_versioned` matcher" do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe PaperTrail, type: :module, versioning: true do
describe "#config" do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe PaperTrail::VersionConcern do
before(:all) { require "support/alt_db_init" }

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe(::PaperTrail, versioning: true) do
CHAPTER_NAMES = [

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
::RSpec.describe Cleaner, versioning: true do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
::RSpec.describe Config do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe(::PaperTrail, versioning: true) do
context "A record with defined 'only' and 'ignore' attributes" do

View File

@ -0,0 +1,85 @@
require "spec_helper"
require "support/custom_json_serializer"
RSpec.describe(PaperTrail, versioning: true) do
context "YAML serializer" do
it "saves the expected YAML in the object column" do
customer = Customer.create(name: "Some text.")
original_attributes = customer.paper_trail.attributes_before_change
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))
expect(customer.versions[0].reify).to(be_nil)
expect(customer.versions[1].reify.name).to(eq("Some text."))
expect(YAML.load(customer.versions[1].object)).to(eq(original_attributes))
expect(customer.versions[1].object).to(eq(YAML.dump(original_attributes)))
end
end
context "JSON Serializer" do
before do
PaperTrail.configure do |config|
config.serializer = PaperTrail::Serializers::JSON
end
end
after do
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
end
it "reify with JSON serializer" do
customer = Customer.create(name: "Some text.")
original_attributes = customer.paper_trail.attributes_before_change
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))
expect(customer.versions[0].reify).to(be_nil)
expect(customer.versions[1].reify.name).to(eq("Some text."))
expect(ActiveSupport::JSON.decode(customer.versions[1].object)).to(eq(original_attributes))
expect(customer.versions[1].object).to(eq(ActiveSupport::JSON.encode(original_attributes)))
end
describe "#changeset" do
it "returns the expected hash" do
customer = Customer.create(name: "Some text.")
customer.update(name: "Some more text.")
initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, customer.id] }
second_changeset = { "name" => ["Some text.", "Some more text."] }
expect(customer.versions[0].changeset).to(eq(initial_changeset))
expect(customer.versions[1].changeset).to(eq(second_changeset))
end
end
end
context "Custom Serializer" do
before do
PaperTrail.configure { |config| config.serializer = CustomJsonSerializer }
end
after do
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
end
it "reify with custom serializer" do
customer = Customer.create
original_attributes = customer.paper_trail.attributes_before_change.reject { |_k, v| v.nil? }
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))
expect(customer.versions[0].reify).to(be_nil)
expect(customer.versions[1].reify.name).to(be_nil)
expect(
ActiveSupport::JSON.decode(customer.versions[1].object)
).to eq(original_attributes)
expect(
customer.versions[1].object
).to eq(ActiveSupport::JSON.encode(original_attributes))
end
describe "#changeset" do
it "store object_changes" do
customer = Customer.create
customer.update(name: "banana")
expect(customer.versions[0].changeset).to eq("id" => [nil, customer.id])
expect(customer.versions[1].changeset).to eq("name" => [nil, "banana"])
end
end
end
end

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module CustomYamlSerializer
extend PaperTrail::Serializers::YAML

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
module Serializers

View File

@ -0,0 +1,42 @@
require "spec_helper"
module PaperTrail
module Serializers
::RSpec.describe(YAML, versioning: true) do
let(:array) { ::Array.new(10) { ::FFaker::Lorem.word } }
let(:hash) {
{
alice: "bob",
binary: 0xdeadbeef,
octal_james_bond: 0o7,
int: 42,
float: 4.2
}
}
describe ".load" do
it "deserializes YAML to Ruby" do
expect(described_class.load(hash.to_yaml)).to eq(hash)
expect(described_class.load(array.to_yaml)).to eq(array)
end
end
describe ".dump" do
it "serializes Ruby to YAML" do
expect(described_class.dump(hash)).to eq(hash.to_yaml)
expect(described_class.dump(array)).to eq(array.to_yaml)
end
end
describe ".where_object" do
it "constructs the correct WHERE query" do
matches = described_class.where_object_condition(
::PaperTrail::Version.arel_table[:object], :arg1, "Val 1"
)
expect(matches.instance_of?(Arel::Nodes::Matches)).to(eq(true))
expect(matches.right.val).to eq("%\narg1: Val 1\n%")
end
end
end
end
end

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
::RSpec.describe Cleaner, versioning: true do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
module PaperTrail
::RSpec.describe(Version, versioning: true) do

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe PaperTrail do
describe ".gem_version" do

View File

@ -1,37 +0,0 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= "test"
ENV["DB"] ||= "sqlite"
unless File.exist?(File.expand_path("../../test/dummy/config/database.yml", __FILE__))
warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
end
require "spec_helper"
require File.expand_path("../../test/dummy/config/environment", __FILE__)
require "rspec/rails"
require "paper_trail/frameworks/rspec"
require "ffaker"
require "timecop"
# prevent Test::Unit's AutoRunner from executing during RSpec's rake task
Test::Unit.run = true if defined?(Test::Unit) && Test::Unit.respond_to?(:run=)
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if ActiveRecord::Migration.respond_to?(:check_pending!)
require "database_cleaner"
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = active_record_gem_version >= ::Gem::Version.new("5")
# In rails < 5, some tests seem to require DatabaseCleaner-truncation.
# Truncation is about three times slower than transaction rollback, so it'll
# be nice when we can drop support for rails < 5.
if active_record_gem_version < ::Gem::Version.new("5")
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
end
end

View File

@ -1,4 +1,4 @@
require "rails_helper"
require "spec_helper"
RSpec.describe "Articles management", type: :request, order: :defined do
let(:valid_params) { { article: { title: "Doh", content: FFaker::Lorem.sentence } } }

View File

@ -1,3 +1,11 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= "test"
ENV["DB"] ||= "sqlite"
unless File.exist?(File.expand_path("../../test/dummy/config/database.yml", __FILE__))
warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
end
require "pry"
RSpec.configure do |config|
@ -43,3 +51,32 @@ def params_wrapper(args)
args
end
end
require File.expand_path("../../test/dummy/config/environment", __FILE__)
require "rspec/rails"
require "paper_trail/frameworks/rspec"
require "ffaker"
require "timecop"
# prevent Test::Unit's AutoRunner from executing during RSpec's rake task
Test::Unit.run = true if defined?(Test::Unit) && Test::Unit.respond_to?(:run=)
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if ActiveRecord::Migration.respond_to?(:check_pending!)
require "database_cleaner"
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = active_record_gem_version >= ::Gem::Version.new("5")
# In rails < 5, some tests seem to require DatabaseCleaner-truncation.
# Truncation is about three times slower than transaction rollback, so it'll
# be nice when we can drop support for rails < 5.
if active_record_gem_version < ::Gem::Version.new("5")
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
end
end

View File

@ -1,107 +0,0 @@
require "test_helper"
require "custom_json_serializer"
class SerializerTest < ActiveSupport::TestCase
context "YAML Serializer" do
setup do
@customer = Customer.create name: "Some text."
# this is exactly what PaperTrail serializes
@original_attributes = @customer.paper_trail.attributes_before_change
@customer.update_attributes name: "Some more text."
end
should "work with the default `YAML` serializer" do
# Normal behaviour
assert_equal 2, @customer.versions.length
assert_nil @customer.versions[0].reify
assert_equal "Some text.", @customer.versions[1].reify.name
# Check values are stored as `YAML`.
assert_equal @original_attributes, YAML.load(@customer.versions[1].object)
assert_equal YAML.dump(@original_attributes), @customer.versions[1].object
end
end
context "JSON Serializer" do
setup do
PaperTrail.configure do |config|
config.serializer = PaperTrail::Serializers::JSON
end
@customer = Customer.create name: "Some text."
# this is exactly what PaperTrail serializes
@original_attributes = @customer.paper_trail.attributes_before_change
@customer.update_attributes name: "Some more text."
end
teardown do
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
end
should "reify with JSON serializer" do
# Normal behaviour
assert_equal 2, @customer.versions.length
assert_nil @customer.versions[0].reify
assert_equal "Some text.", @customer.versions[1].reify.name
# Check values are stored as JSON.
assert_equal @original_attributes,
ActiveSupport::JSON.decode(@customer.versions[1].object)
assert_equal ActiveSupport::JSON.encode(@original_attributes),
@customer.versions[1].object
end
should "store object_changes" do
initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, @customer.id] }
second_changeset = { "name" => ["Some text.", "Some more text."] }
assert_equal initial_changeset, @customer.versions[0].changeset
assert_equal second_changeset, @customer.versions[1].changeset
end
end
context "Custom Serializer" do
setup do
PaperTrail.configure do |config|
config.serializer = CustomJsonSerializer
end
@customer = Customer.create
# this is exactly what PaperTrail serializes
@original_attributes = @customer.
paper_trail.
attributes_before_change.
reject { |_k, v| v.nil? }
@customer.update_attributes name: "Some more text."
end
teardown do
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
end
should "reify with custom serializer" do
# Normal behaviour
assert_equal 2, @customer.versions.length
assert_nil @customer.versions[0].reify
assert_nil @customer.versions[1].reify.name
# Check values are stored as JSON.
assert_equal @original_attributes,
ActiveSupport::JSON.decode(@customer.versions[1].object)
assert_equal ActiveSupport::JSON.encode(@original_attributes),
@customer.versions[1].object
end
should "store object_changes" do
initial_changeset = { "id" => [nil, @customer.id] }
second_changeset = { "name" => [nil, "Some more text."] }
assert_equal initial_changeset, @customer.versions[0].changeset
assert_equal second_changeset, @customer.versions[1].changeset
end
end
end

View File

@ -1,5 +1,5 @@
require "test_helper"
require "custom_json_serializer"
require_relative "../../../spec/support/custom_json_serializer"
class MixinJsonTest < ActiveSupport::TestCase
setup do

View File

@ -1,50 +0,0 @@
require "test_helper"
class YamlTest < ActiveSupport::TestCase
setup do
# Setup a hash with random values
@hash = {}
(1..4).each do |i|
@hash["key#{i}".to_sym] = FFaker::Lorem.word
end
@hash_as_yaml = @hash.to_yaml
# Setup an array of random words
@array = []
(rand(5) + 4).times { @array << FFaker::Lorem.word }
@array_as_yaml = @array.to_yaml
end
context "`load` class method" do
should "exist" do
assert PaperTrail::Serializers::YAML.respond_to?(:load)
end
should "deserialize `YAML` to Ruby" do
assert_equal @hash, PaperTrail::Serializers::YAML.load(@hash_as_yaml)
assert_equal @array, PaperTrail::Serializers::YAML.load(@array_as_yaml)
end
end
context "`dump` class method" do
should "exist" do
assert PaperTrail::Serializers::YAML.respond_to?(:dump)
end
should "serialize Ruby to `YAML`" do
assert_equal @hash_as_yaml, PaperTrail::Serializers::YAML.dump(@hash)
assert_equal @array_as_yaml, PaperTrail::Serializers::YAML.dump(@array)
end
end
context "`where_object` class method" do
should "construct correct WHERE query" do
matches = PaperTrail::Serializers::YAML.where_object_condition(
PaperTrail::Version.arel_table[:object],
:arg1,
"Val 1"
)
assert matches.instance_of?(Arel::Nodes::Matches)
assert_equal matches.right.val, "%\narg1: Val 1\n%"
end
end
end