|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe Item, type: :model do |
| 4 | + describe 'factory' do |
| 5 | + it 'has a valid factory' do |
| 6 | + expect(build(:item)).to be_valid |
| 7 | + end |
| 8 | + end |
| 9 | + |
| 10 | + describe 'associations' do |
| 11 | + it { is_expected.to belong_to(:category).optional } |
| 12 | + end |
| 13 | + |
| 14 | + describe 'validations' do |
| 15 | + subject { build(:item) } |
| 16 | + |
| 17 | + describe 'name' do |
| 18 | + it { is_expected.to validate_presence_of(:name) } |
| 19 | + it { is_expected.to validate_uniqueness_of(:name).scoped_to(:category_id).with_message('Item name must be unique within category') } |
| 20 | + |
| 21 | + it 'allows same name with different categories' do |
| 22 | + category1 = create(:category) |
| 23 | + category2 = create(:category) |
| 24 | + create(:item, name: 'Test Item', category: category1) |
| 25 | + item2 = build(:item, name: 'Test Item', category: category2) |
| 26 | + expect(item2).to be_valid |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe 'category_must_be_active' do |
| 31 | + it 'is valid with an active category' do |
| 32 | + item = build(:item) |
| 33 | + expect(item).to be_valid |
| 34 | + end |
| 35 | + |
| 36 | + it 'is valid without a category' do |
| 37 | + item = build(:item, category: nil) |
| 38 | + expect(item).to be_valid |
| 39 | + end |
| 40 | + |
| 41 | + it 'is invalid with a disabled category' do |
| 42 | + item = build(:item, :with_disabled_category) |
| 43 | + expect(item).not_to be_valid |
| 44 | + expect(item.errors[:category]).to include('is disabled') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe 'calculation_formula' do |
| 49 | + context 'when is_fixed is true' do |
| 50 | + let(:item) { build(:item, :with_fixed_parameters, :without_calculation_formula) } |
| 51 | + |
| 52 | + it 'requires calculation_formula' do |
| 53 | + expect(item).not_to be_valid |
| 54 | + expect(item.errors[:calculation_formula]).to include("can't be blank") |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when is_open is true' do |
| 59 | + let(:item) { build(:item, :with_open_parameters, :without_calculation_formula) } |
| 60 | + |
| 61 | + it 'requires calculation_formula' do |
| 62 | + expect(item).not_to be_valid |
| 63 | + expect(item.errors[:calculation_formula]).to include("can't be blank") |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'when is_selectable_options is true' do |
| 68 | + let(:item) { build(:item, :with_pricing_options, :without_calculation_formula) } |
| 69 | + |
| 70 | + it 'requires calculation_formula' do |
| 71 | + expect(item).not_to be_valid |
| 72 | + expect(item.errors[:calculation_formula]).to include("can't be blank") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'when multiple flags are true' do |
| 77 | + let(:item) do |
| 78 | + build(:item, |
| 79 | + fixed_parameters: { "Aquisition" => "2500" }, |
| 80 | + pricing_options: { "Tier" => { "1-5" => "100" } }, |
| 81 | + is_fixed: true, |
| 82 | + is_selectable_options: true, |
| 83 | + calculation_formula: nil) |
| 84 | + end |
| 85 | + |
| 86 | + it 'requires calculation_formula' do |
| 87 | + expect(item).not_to be_valid |
| 88 | + expect(item.errors[:calculation_formula]).to include("can't be blank") |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + context 'when no flags are true' do |
| 93 | + it 'does not require calculation_formula' do |
| 94 | + item = build(:item) |
| 95 | + expect(item).to be_valid |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe 'fixed_parameters' do |
| 101 | + it 'is valid with empty hash' do |
| 102 | + item = build(:item, fixed_parameters: {}) |
| 103 | + expect(item).to be_valid |
| 104 | + end |
| 105 | + |
| 106 | + it 'is valid with numeric string values' do |
| 107 | + item = build(:item, :with_fixed_parameters) |
| 108 | + expect(item).to be_valid |
| 109 | + end |
| 110 | + |
| 111 | + it 'is valid with numeric values' do |
| 112 | + item = build(:item, fixed_parameters: { "Acquisition" => 2500 }) |
| 113 | + expect(item).to be_valid |
| 114 | + end |
| 115 | + |
| 116 | + it 'is invalid with non-numeric string values' do |
| 117 | + item = build(:item, :with_invalid_fixed_parameters) |
| 118 | + expect(item).not_to be_valid |
| 119 | + expect(item.errors[:fixed_parameters]).to include("value for 'Acquisition' must be a number") |
| 120 | + end |
| 121 | + |
| 122 | + it 'is invalid with non-hash values' do |
| 123 | + item = build(:item, fixed_parameters: "not_a_hash") |
| 124 | + expect(item).not_to be_valid |
| 125 | + expect(item.errors[:fixed_parameters]).to include("must be a JSON object") |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe 'pricing_options' do |
| 130 | + it 'is valid with empty hash' do |
| 131 | + item = build(:item, pricing_options: {}) |
| 132 | + expect(item).to be_valid |
| 133 | + end |
| 134 | + |
| 135 | + it 'is valid with nested numeric string values' do |
| 136 | + item = build(:item, :with_pricing_options) |
| 137 | + expect(item).to be_valid |
| 138 | + end |
| 139 | + |
| 140 | + it 'is valid with nested numeric values' do |
| 141 | + item = build(:item, pricing_options: { "Tier" => { "1-5" => 100 } }) |
| 142 | + expect(item).to be_valid |
| 143 | + end |
| 144 | + |
| 145 | + it 'is invalid with non-numeric nested string values' do |
| 146 | + item = build(:item, :with_invalid_pricing_options) |
| 147 | + expect(item).not_to be_valid |
| 148 | + expect(item.errors[:pricing_options]).to include("value for 'Tier -> 1-5' must be a number") |
| 149 | + end |
| 150 | + |
| 151 | + it 'is invalid with non-hash values' do |
| 152 | + item = build(:item, pricing_options: "not_a_hash") |
| 153 | + expect(item).not_to be_valid |
| 154 | + expect(item.errors[:pricing_options]).to include("must be a JSON object") |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments