Skip to content

Commit

Permalink
Add extra result specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabby committed Oct 11, 2024
1 parent 3322126 commit a351d01
Showing 1 changed file with 116 additions and 30 deletions.
146 changes: 116 additions & 30 deletions spec/anony/result_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "spec_helper"
require_relative "helpers/database"

RSpec.describe Anony::Result do
let(:field_values) do
Expand All @@ -10,69 +11,154 @@
}
end

shared_context "with model instance" do
let(:klass) do
Class.new(ActiveRecord::Base) do
include Anony::Anonymisable

def self.name
"Employee"
end

self.table_name = :employees
end
end

let(:model) { klass.new }
end

context "anonymised" do
let(:result) { described_class.overwritten(field_values) }
shared_examples_for "anonymised result" do
it "has enumbeable state" do
expect(result.status).to eq("overwritten")
end

it "has enumbeable state" do
expect(result.status).to eq("overwritten")
it "responds to .overwritten?" do
expect(result).to be_overwritten
end
end

it "responds to .overwritten?" do
expect(result).to be_overwritten
context "without record" do
let(:result) { described_class.overwritten(field_values) }

it_behaves_like "anonymised result"
end

context "with record" do
include_context "with model instance"
let(:result) { described_class.overwritten(field_values, model) }

it_behaves_like "anonymised result"

it "contains the model" do
expect(result.record).to be model
end
end
end

context "deleted" do
let(:result) { described_class.destroyed }
shared_examples_for "destroyed result" do
it "has enumbeable state" do
expect(result.status).to eq("destroyed")
end

it "has enumbeable state" do
expect(result.status).to eq("destroyed")
it "responds to .destroyed?" do
expect(result).to be_destroyed
end

it "has no fields" do
expect(result.fields).to be_empty
end
end

it "responds to .destroyed?" do
expect(result).to be_destroyed
context "without record" do
let(:result) { described_class.destroyed }

it_behaves_like "destroyed result"
end

it "has no fields" do
expect(result.fields).to be_empty
context "with record" do
include_context "with model instance"
let(:result) { described_class.destroyed(model) }

it_behaves_like "destroyed result"

it "contains the model" do
expect(result.record).to be model
end
end
end

context "skipped" do
let(:result) { described_class.skipped }
shared_examples_for "skipped result" do
it "has enumbeable state" do
expect(result.status).to eq("skipped")
end

it "responds to .skipped?" do
expect(result).to be_skipped
end

it "has enumbeable state" do
expect(result.status).to eq("skipped")
it "has no fields" do
expect(result.fields).to be_empty
end
end

it "responds to .skipped?" do
expect(result).to be_skipped
context "without record" do
let(:result) { described_class.skipped }

it_behaves_like "skipped result"
end

it "has no fields" do
expect(result.fields).to be_empty
context "with record" do
include_context "with model instance"
let(:result) { described_class.skipped(model) }

it_behaves_like "skipped result"

it "contains the model" do
expect(result.record).to be model
end
end
end

context "failed" do
let(:error) { anything }
let(:result) { described_class.failed(error) }

it "has an error" do
expect(result.error).to eq(error)
end
shared_examples_for "failed result" do
it "has an error" do
expect(result.error).to eq(error)
end

it "has enumbeable state" do
expect(result.status).to eq("failed")
end

it "responds to .failed?" do
expect(result).to be_failed
end

it "has enumbeable state" do
expect(result.status).to eq("failed")
context "without an error" do
it "raises an exception" do
expect { described_class.failed(nil) }.to raise_error(ArgumentError)
end
end
end

it "responds to .failed?" do
expect(result).to be_failed
context "without record" do
let(:result) { described_class.failed(error) }

it_behaves_like "failed result"
end

context "without an error" do
it "raises an exception" do
expect { described_class.failed(nil) }.to raise_error(ArgumentError)
context "with record" do
include_context "with model instance"
let(:result) { described_class.failed(error, model) }

it_behaves_like "failed result"

it "contains the model" do
expect(result.record).to be model
end
end
end
Expand Down

0 comments on commit a351d01

Please sign in to comment.