-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert ComplianceMixin to has_one/virtual_delegates
This converts the functionality defined in the virtual_has_one, virtual_columns, and associated methods to a has_one and a pair of virtual_delegates. This allows these columns to also be used in SQL, and removes some of the boilerplate code necessary for the methods that are defined.
- Loading branch information
1 parent
3b7f61a
commit f6a75b0
Showing
2 changed files
with
63 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
describe ComplianceMixin do | ||
include Spec::Support::ArelHelper | ||
|
||
let(:host) { FactoryGirl.create(:host) } | ||
let(:new_timestamp) { 2.months.ago.change(:usec => 0) } | ||
let(:old_timestamp) { 4.months.ago.change(:usec => 0) } | ||
let(:new_compliance) { FactoryGirl.create(:compliance, :resource => host, :timestamp => new_timestamp, :compliant => false) } | ||
let(:old_compliance) { FactoryGirl.create(:compliance, :resource => host, :timestamp => old_timestamp) } | ||
let(:compliances) { [old_compliance, new_compliance] } | ||
|
||
describe "#last_compliance" do | ||
it "uses the most recent value" do | ||
compliances | ||
expect(host.last_compliance.timestamp).to eq(new_timestamp) | ||
end | ||
end | ||
|
||
describe "#last_compliance_status" do | ||
context "with no compliances" do | ||
it "is nil with sql" do | ||
host | ||
expect(virtual_column_sql_value(Host, "last_compliance_status")).to be_nil | ||
end | ||
|
||
it "is nil with ruby" do | ||
expect(host.last_compliance_status).to be_nil | ||
end | ||
end | ||
|
||
context "with compliances" do | ||
before { compliances } | ||
|
||
it "has the most recent timestamp with sql" do | ||
h = Host.select(:id, :last_compliance_status).first | ||
|
||
expect do | ||
expect(h.last_compliance_status).to eq(false) | ||
end.to match_query_limit_of(0) | ||
expect(h.association(:last_compliance)).not_to be_loaded | ||
end | ||
|
||
it "has the most recent timestamp with ruby" do | ||
h = Host.first # clean host record | ||
|
||
expect(h.last_compliance_status).to eq(false) | ||
expect(h.association(:last_compliance)).to be_loaded | ||
end | ||
end | ||
end | ||
end |