2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-23 14:33:16 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-03-03 14:00:05 -05:00
|
|
|
describe UserInteractedProject do
|
2018-02-23 14:33:16 -05:00
|
|
|
describe '.track' do
|
|
|
|
subject { described_class.track(event) }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2018-02-23 14:33:16 -05:00
|
|
|
let(:event) { build(:event) }
|
|
|
|
|
|
|
|
Event::ACTIONS.each do |action|
|
|
|
|
context "for all actions (event types)" do
|
|
|
|
let(:event) { build(:event, action: action) }
|
2019-12-18 19:08:01 -05:00
|
|
|
|
2018-02-23 14:33:16 -05:00
|
|
|
it 'creates a record' do
|
2018-02-26 06:24:02 -05:00
|
|
|
expect { subject }.to change { described_class.count }.from(0).to(1)
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets project accordingly' do
|
2018-02-23 14:56:43 -05:00
|
|
|
subject
|
2018-02-26 06:24:02 -05:00
|
|
|
expect(described_class.first.project).to eq(event.project)
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets user accordingly' do
|
2018-02-23 14:56:43 -05:00
|
|
|
subject
|
2018-02-26 06:24:02 -05:00
|
|
|
expect(described_class.first.user).to eq(event.author)
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'only creates a record once per user/project' do
|
|
|
|
expect do
|
|
|
|
subject
|
|
|
|
described_class.track(event)
|
2018-02-26 06:24:02 -05:00
|
|
|
end.to change { described_class.count }.from(0).to(1)
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
2018-02-23 15:24:11 -05:00
|
|
|
|
|
|
|
describe 'with an event without a project' do
|
|
|
|
let(:event) { build(:event, project: nil) }
|
|
|
|
|
|
|
|
it 'ignores the event' do
|
2018-02-26 06:24:02 -05:00
|
|
|
expect { subject }.not_to change { described_class.count }
|
2018-02-23 15:24:11 -05:00
|
|
|
end
|
|
|
|
end
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|
|
|
|
|
2018-02-26 10:44:35 -05:00
|
|
|
describe '.available?' do
|
|
|
|
before do
|
|
|
|
described_class.instance_variable_set('@available_flag', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks schema version and properly caches positive result' do
|
|
|
|
expect(ActiveRecord::Migrator).to receive(:current_version).and_return(described_class::REQUIRED_SCHEMA_VERSION - 1 - rand(1000))
|
|
|
|
expect(described_class.available?).to be_falsey
|
|
|
|
expect(ActiveRecord::Migrator).to receive(:current_version).and_return(described_class::REQUIRED_SCHEMA_VERSION + rand(1000))
|
|
|
|
expect(described_class.available?).to be_truthy
|
|
|
|
expect(ActiveRecord::Migrator).not_to receive(:current_version)
|
|
|
|
expect(described_class.available?).to be_truthy # cached response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-05 10:50:05 -05:00
|
|
|
it { is_expected.to validate_presence_of(:project_id) }
|
|
|
|
it { is_expected.to validate_presence_of(:user_id) }
|
2018-02-23 14:33:16 -05:00
|
|
|
end
|