Back to Repositories

Testing ActiveRecord Property Detection in RailsAdmin

This test suite validates the property handling functionality in RailsAdmin’s ActiveRecord adapter, focusing on field type detection, serialization, and read-only attributes. The tests ensure proper mapping and behavior of database fields within the Rails Admin interface.

Test Coverage Overview

The test suite provides comprehensive coverage of ActiveRecord property handling in RailsAdmin, specifically examining:
  • String field properties including type, length, and nullability
  • Serialized field handling and attributes
  • Read-only column detection and validation

Implementation Analysis

The testing approach utilizes RSpec’s describe/it blocks to organize test cases around specific field types and properties. The implementation leverages subject blocks for focused property testing and employs Timecop for time-dependent scenarios.

The tests use mock models (Player, User, HasReadOnlyColumn) to validate property detection and attribute handling.

Technical Details

Key technical components include:
  • RSpec test framework with active_record integration
  • Tableless model implementation for read-only testing
  • AbstractModel wrapper for ActiveRecord models
  • Property detection and attribute validation methods

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with clear separation of concerns
  • Proper use of RSpec subject blocks for DRY testing
  • Comprehensive property verification
  • Mock model usage for controlled testing environments

railsadminteam/rails_admin

spec/rails_admin/adapters/active_record/property_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'
require 'timecop'

RSpec.describe 'RailsAdmin::Adapters::ActiveRecord::Property', active_record: true do
  describe 'string field' do
    subject { RailsAdmin::AbstractModel.new('Player').properties.detect { |f| f.name == :name } }

    it 'returns correct values' do
      expect(subject.pretty_name).to eq 'Name'
      expect(subject.type).to eq :string
      expect(subject.length).to eq 100
      expect(subject.nullable?).to be_falsey
      expect(subject.serial?).to be_falsey
    end
  end

  describe 'serialized field' do
    subject { RailsAdmin::AbstractModel.new('User').properties.detect { |f| f.name == :roles } }

    it 'returns correct values' do
      expect(subject.pretty_name).to eq 'Roles'
      expect(subject.type).to eq :serialized
      expect(subject.nullable?).to be_truthy
      expect(subject.serial?).to be_falsey
    end
  end

  describe '#read_only?' do
    before do
      class HasReadOnlyColumn < Tableless
        column :name, :varchar
        attr_readonly :name
      end
    end

    it 'returns correct values' do
      expect(RailsAdmin::AbstractModel.new('Player').properties.detect { |f| f.name == :name }).not_to be_read_only
      expect(RailsAdmin::AbstractModel.new('HasReadOnlyColumn').properties.detect { |f| f.name == :name }).to be_read_only
    end
  end
end