Commit 5c0b3a27 authored by Mark Chao's avatar Mark Chao

Add with_hosted_plan scope to filter by plan

Refactor spec to cache created plan records
parent 94974e2c
...@@ -13,8 +13,12 @@ class GitlabSubscription < ApplicationRecord ...@@ -13,8 +13,12 @@ class GitlabSubscription < ApplicationRecord
delegate :name, :title, to: :hosted_plan, prefix: :plan, allow_nil: true delegate :name, :title, to: :hosted_plan, prefix: :plan, allow_nil: true
scope :with_hosted_plan, -> (plan_name) do
joins(:hosted_plan).where(trial: false, 'plans.name' => plan_name)
end
scope :with_a_paid_hosted_plan, -> do scope :with_a_paid_hosted_plan, -> do
joins(:hosted_plan).where(trial: false, 'plans.name' => Plan::PAID_HOSTED_PLANS) with_hosted_plan(Plan::PAID_HOSTED_PLANS)
end end
def seats_in_use def seats_in_use
......
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
require 'spec_helper' require 'spec_helper'
describe GitlabSubscription do describe GitlabSubscription do
%i[free_plan bronze_plan silver_plan gold_plan early_adopter_plan].each do |plan|
let_it_be(plan) { create(plan) }
end
describe 'default values' do describe 'default values' do
it do it do
Timecop.freeze(Date.today + 30) do Timecop.freeze(Date.today + 30) do
...@@ -26,6 +30,23 @@ describe GitlabSubscription do ...@@ -26,6 +30,23 @@ describe GitlabSubscription do
it { is_expected.to belong_to(:hosted_plan) } it { is_expected.to belong_to(:hosted_plan) }
end end
describe 'scopes' do
describe '.with_hosted_plan' do
let!(:gold_subscription) { create(:gitlab_subscription, hosted_plan: gold_plan) }
let!(:silver_subscription) { create(:gitlab_subscription, hosted_plan: silver_plan) }
let!(:early_adopter_subscription) { create(:gitlab_subscription, hosted_plan: early_adopter_plan) }
let!(:trial_subscription) { create(:gitlab_subscription, hosted_plan: gold_plan, trial: true) }
it 'scopes to the plan' do
expect(described_class.with_hosted_plan('gold')).to contain_exactly(gold_subscription)
expect(described_class.with_hosted_plan('silver')).to contain_exactly(silver_subscription)
expect(described_class.with_hosted_plan('early_adopter')).to contain_exactly(early_adopter_subscription)
expect(described_class.with_hosted_plan('bronze')).to be_empty
end
end
end
describe '#seats_in_use' do describe '#seats_in_use' do
let!(:user_1) { create(:user) } let!(:user_1) { create(:user) }
let!(:user_2) { create(:user) } let!(:user_2) { create(:user) }
...@@ -38,12 +59,6 @@ describe GitlabSubscription do ...@@ -38,12 +59,6 @@ describe GitlabSubscription do
let!(:subgroup_2) { create(:group, parent: group) } let!(:subgroup_2) { create(:group, parent: group) }
let!(:gitlab_subscription) { create(:gitlab_subscription, namespace: group) } let!(:gitlab_subscription) { create(:gitlab_subscription, namespace: group) }
before do
%i[free_plan bronze_plan silver_plan gold_plan].each do |plan|
create(plan)
end
end
it 'returns count of members' do it 'returns count of members' do
group.add_developer(user_1) group.add_developer(user_1)
...@@ -116,8 +131,6 @@ describe GitlabSubscription do ...@@ -116,8 +131,6 @@ describe GitlabSubscription do
end end
describe '#seats_owed' do describe '#seats_owed' do
let!(:bronze_plan) { create(:bronze_plan) }
let!(:early_adopter_plan) { create(:early_adopter_plan) }
let!(:gitlab_subscription) { create(:gitlab_subscription, subscription_attrs) } let!(:gitlab_subscription) { create(:gitlab_subscription, subscription_attrs) }
before do before do
...@@ -262,8 +275,7 @@ describe GitlabSubscription do ...@@ -262,8 +275,7 @@ describe GitlabSubscription do
context 'after_destroy_commit' do context 'after_destroy_commit' do
it 'logs previous state to gitlab subscription history' do it 'logs previous state to gitlab subscription history' do
group = create(:group) group = create(:group)
plan = create(:bronze_plan) subject.update! max_seats_used: 37, seats: 11, namespace: group, hosted_plan: bronze_plan
subject.update! max_seats_used: 37, seats: 11, namespace: group, hosted_plan: plan
db_created_at = described_class.last.created_at db_created_at = described_class.last.created_at
subject.destroy! subject.destroy!
...@@ -275,7 +287,7 @@ describe GitlabSubscription do ...@@ -275,7 +287,7 @@ describe GitlabSubscription do
'max_seats_used' => 37, 'max_seats_used' => 37,
'seats' => 11, 'seats' => 11,
'namespace_id' => group.id, 'namespace_id' => group.id,
'hosted_plan_id' => plan.id, 'hosted_plan_id' => bronze_plan.id,
'gitlab_subscription_created_at' => db_created_at 'gitlab_subscription_created_at' => db_created_at
) )
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment