Sha256: 28704670332e6be8439fa3601c066a4f64cd59c45df89c76c97a73667539042f

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require File.join(File.dirname(__FILE__), "test_helper")

class IndicesTest < Test::Unit::TestCase
  class User < Ohm::Model
    attribute :email

    index :email
  end

  context "A model with an indexed attribute" do
    setup do
      Ohm.flush

      @user1 = User.create(:email => "foo")
      @user2 = User.create(:email => "bar")
      @user3 = User.create(:email => "baz qux")
    end

    should "be able to find by the given attribute" do
      assert_equal @user1, User.find(:email, "foo").first
    end

    should "return nil if no results are found" do
      assert User.find(:email, "foobar").empty?
      assert_equal nil, User.find(:email, "foobar").first
    end

    should "update indices when changing attribute values" do
      @user1.email = "baz"
      @user1.save

      assert_equal [], User.find(:email, "foo")
      assert_equal [@user1], User.find(:email, "baz")
    end

    should "remove from the index after deleting" do
      @user2.delete

      assert_equal [], User.find(:email, "bar")
    end

    should "work with attributes that contain spaces" do
      assert_equal [@user3], User.find(:email, "baz qux")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ohm-0.0.13 test/indices_test.rb