You are a helpful AI coding assistant. Skip prose and keep explanations short. If you…
- User
You are a helpful AI coding assistant. Skip prose and keep explanations short. If you don't know the answer to a question/task, reply with "I don't know that”.
- Assistant
Understood.
- User
Please help me write a test for this ruby on rails model, I am using RSpec for testing.
class Report < ApplicationRecord PUBLIC_ID_CHARS = %w[0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z].freeze
has_many :quotes belongs_to :organization
validates_length_of :title, maximum: 250 validates_numericality_of :logotype_width, greater_than: 10, less_than_or_equal_to: 500, allow_nil: false validates :public_id, presence: true
before_validation :assign_public_id, on: :create
def align_quotes_order! quotes.ordered.each_with_index do |quote, index| quote.update_column(:position, index) end end
def active_cpm_settings @active_cpm_settings ||= cpm_setting || organization.cpm_settings.default || CpMSetting.default_for_currency(organization.currency) end
def visible_metrics_values if visible_metrics.to_set.size != visible_metrics.size || visible_metrics.any? { |m| !AVAILABLE_METRICS.include?(m) } errors.add(:visible_metrics, :invalid) end end
private
def assign_public_id loop do new_id = PUBLIC_ID_CHARS.sample(8).join # 8 characters public id self.public_id = new_id
break unless Report.exists?(public_id: new_id) end end end
- Assistant
Here's an RSpec test for your Report model:
rubyrequire 'rails_helper' RSpec.describe Report, type: :model do describe 'associations' do it { should belong_to(:organization) } it { should have_many(:quotes) } end describe 'validations' do it { should validate_length_of(:title).is_at_most(250) } it { should validate_numericality_of(:logotype_width).is_greater_than(10).is_less_than_or_equal_to(500) } it { should validate_presence_of(:public_id) } end describe '#align_quotes_order!' do let!(:report) { create(:report) } let!(:quote1) { create(:quote, report: report, position: 2) } let!(:quote2) { create(:quote, report: report, position: 1) } it 'aligns quotes order' do report.align_quotes_order! expect(quote1.reload.position).to eq(1) expect(quote2.reload.position).to eq(0) end end describe '#assign_public_id' do let(:report) { build(:report, public_id: nil) } it 'assigns public_id before
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.