Sha256: dd302b84ac7b216333ee2a0e58a4ecc56d8d4ceee1c54a18b4c892fa08013dac

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

require 'test_helper'

class Superstore::AttributeMethodsTest < Superstore::TestCase
  test 'read and write attributes' do
    issue = Issue.new
    assert_nil issue.read_attribute(:description)

    issue.write_attribute(:description, nil)
    assert_nil issue.read_attribute(:description)

    issue.write_attribute(:description, 'foo')
    assert_equal 'foo', issue.read_attribute(:description)
  end

  test 'read primary_key' do
    refute_nil Issue.new[:id]
  end

  test 'hash accessor aliases' do
    issue = Issue.new

    issue[:description] = 'bar'

    assert_equal 'bar', issue[:description]
  end

  test 'attributes setter' do
    issue = Issue.new

    issue.attributes = {
      description: 'foo'
    }

    assert_equal 'foo', issue.description
  end

  class ChildIssue < Issue
    def title=(val)
      self[:title] = val + ' lol'
    end
  end

  test 'override' do
    issue = ChildIssue.new(title: 'hey')

    assert_equal 'hey lol', issue.title
  end

  class ReservedWord < Superstore::Base
    self.table_name = 'issues'
    string :system
  end

  test 'reserved words' do
    r = ReservedWord.new(system: 'hello')
    assert_equal 'hello', r.system
  end

  test 'has_attribute?' do
    refute Issue.new.attribute_exists?(:description)
    assert Issue.new(description: nil).has_attribute?(:description)
    assert Issue.new(description: false).has_attribute?(:description)
    assert Issue.new(description: 'hey').has_attribute?(:description)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
superstore-2.4.4 test/unit/attribute_methods_test.rb
superstore-2.4.3 test/unit/attribute_methods_test.rb
superstore-2.4.2 test/unit/attribute_methods_test.rb
superstore-2.4.1 test/unit/attribute_methods_test.rb
superstore-2.4.0 test/unit/attribute_methods_test.rb