Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silug committed Nov 5, 2024
1 parent 4eac1a7 commit 0c1b559
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/compliance_engine/data_loader/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def initialize(file, fileclass: ::File, key: file)
end

def refresh
size = @fileclass.size(@filename)
mtime = @fileclass.mtime(@filename)
return if size == @size && mtime == @mtime
newsize = @fileclass.size(@filename)
newmtime = @fileclass.mtime(@filename)
return if newsize == @size && newmtime == @mtime

@size = size
@mtime = mtime
@size = newsize
@mtime = newmtime
self.data = parse(@fileclass.read(@filename))
end

Expand Down
23 changes: 23 additions & 0 deletions spec/classes/compliance_engine/data_loader/file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'spec_helper'
require 'compliance_engine/data_loader/file'

RSpec.describe ComplianceEngine::DataLoader::File do
let(:filename) { '/path/to/file' }

before(:each) do
allow(File).to receive(:size).and_call_original
allow(File).to receive(:size).with(filename).and_return(0)

allow(File).to receive(:mtime).and_call_original
allow(File).to receive(:mtime).with(filename).and_return(Time.now)

allow(File).to receive(:read).and_call_original
allow(File).to receive(:read).with(filename).and_return("\n")
end

it 'fails to initialize' do
expect { described_class.new(filename) }.to raise_error(NoMethodError)
end
end
27 changes: 27 additions & 0 deletions spec/classes/compliance_engine/data_loader/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

RSpec.describe ComplianceEngine::DataLoader::Json do
before(:each) do
allow(File).to receive(:size).and_call_original
allow(File).to receive(:mtime).and_call_original
allow(File).to receive(:read).and_call_original
end

Expand All @@ -23,6 +25,9 @@

shared_examples 'an observable' do
it 'updates the data' do
data_loader.instance_variable_set(:@size, 0)
data_loader.instance_variable_set(:@mtime, Time.now)
data_loader.data = initial_data
data_loader.refresh
expect(data_loader.data).to eq(updated_data)
end
Expand All @@ -40,6 +45,10 @@ def update(data)
attr_accessor :data
end

data_loader.instance_variable_set(:@size, 0)
data_loader.instance_variable_set(:@mtime, Time.now)
data_loader.data = initial_data

observer = ExampleObserver.new(data_loader)
data_loader.refresh
expect(observer.data).to be_instance_of(described_class)
Expand All @@ -60,13 +69,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_json)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_json)
end

Expand All @@ -87,13 +100,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_json)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_json)
end

Expand All @@ -114,13 +131,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_json)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_json)
end

Expand All @@ -133,6 +154,8 @@ def update(data)
let(:data) { 'invalid data' }

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(data.to_json)
end

Expand All @@ -147,12 +170,16 @@ def update(data)
let(:updated_data) { 'invalid data' }

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_json)
end

it 'does not update the data' do
loader = described_class.new(filename)

allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_json)

expect { loader.refresh }.to raise_error(ComplianceEngine::Error, 'Data must be a hash')
Expand Down
27 changes: 27 additions & 0 deletions spec/classes/compliance_engine/data_loader/yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

RSpec.describe ComplianceEngine::DataLoader::Yaml do
before(:each) do
allow(File).to receive(:size).and_call_original
allow(File).to receive(:mtime).and_call_original
allow(File).to receive(:read).and_call_original
end

Expand All @@ -23,6 +25,9 @@

shared_examples 'an observable' do
it 'updates the data' do
data_loader.instance_variable_set(:@size, 0)
data_loader.instance_variable_set(:@mtime, Time.now)
data_loader.data = initial_data
data_loader.refresh
expect(data_loader.data).to eq(updated_data)
end
Expand All @@ -40,6 +45,10 @@ def update(data)
attr_accessor :data
end

data_loader.instance_variable_set(:@size, 0)
data_loader.instance_variable_set(:@mtime, Time.now)
data_loader.data = initial_data

observer = ExampleObserver.new(data_loader)
data_loader.refresh
expect(observer.data).to be_instance_of(described_class)
Expand All @@ -60,13 +69,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml)
end

Expand All @@ -87,13 +100,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml)
end

Expand All @@ -114,13 +131,17 @@ def update(data)
end

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml)
end

it_behaves_like 'a data loader'

context 'with updated data' do
before(:each) do
allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml)
end

Expand All @@ -133,6 +154,8 @@ def update(data)
let(:data) { 'invalid data' }

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(data.to_yaml)
end

Expand All @@ -147,12 +170,16 @@ def update(data)
let(:updated_data) { 'invalid data' }

before(:each) do
allow(File).to receive(:size).with(filename).and_return(0)
allow(File).to receive(:mtime).with(filename).and_return(Time.now)
allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml)
end

it 'does not update the data' do
loader = described_class.new(filename)

allow(File).to receive(:size).with(filename).and_return(1)
allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1)
allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml)

expect { loader.refresh }.to raise_error(ComplianceEngine::Error, 'Data must be a hash')
Expand Down

0 comments on commit 0c1b559

Please sign in to comment.