Sha256: 20253238521aa8fa74827e44667e2052bfd268ce099ca3708dd2a110153bac2c

Contents?: true

Size: 1.85 KB

Versions: 15

Compression:

Stored size: 1.85 KB

Contents

require_relative "helper"

class Post < Ohm::Model
  list :comments, :Comment
end

class Comment < Ohm::Model
end

setup do
  post = Post.create

  post.comments.push(c1 = Comment.create)
  post.comments.push(c2 = Comment.create)
  post.comments.push(c3 = Comment.create)

  [post, c1, c2, c3]
end

test "include?" do |p, c1, c2, c3|
  assert p.comments.include?(c1)
  assert p.comments.include?(c2)
  assert p.comments.include?(c3)
end

test "first / last / size / empty?" do |p, c1, c2, c3|
  assert_equal 3, p.comments.size
  assert_equal c1, p.comments.first
  assert_equal c3, p.comments.last
  assert ! p.comments.empty?
end

test "replace" do |p, c1, c2, c3|
  c4 = Comment.create

  p.comments.replace([c4])

  assert_equal [c4], p.comments.to_a
end

test "push / unshift" do |p, c1, c2, c3|
  c4 = Comment.create
  c5 = Comment.create

  p.comments.unshift(c4)
  p.comments.push(c5)

  assert_equal c4, p.comments.first
  assert_equal c5, p.comments.last
end

test "delete" do |p, c1, c2, c3|
  p.comments.delete(c1)
  assert_equal 2, p.comments.size
  assert ! p.comments.include?(c1)

  p.comments.delete(c2)
  assert_equal 1, p.comments.size
  assert ! p.comments.include?(c2)

  p.comments.delete(c3)
  assert p.comments.empty?
end

test "deleting main model cleans up the collection" do |p, _, _, _|
  p.delete

  assert_equal 0, Ohm.redis.call("EXISTS", p.key[:comments])
end

test "#ids returns an array with the ids" do |post, *comments|
  assert_equal comments.map(&:id), post.comments.ids
end

test "range" do |p, c1, c2, c3|
  assert_equal 3, p.comments.range(0, 100).size
  assert_equal [c1, c2, c3], p.comments.range(0, 2)
  assert_equal [c1, c2], p.comments.range(0, 1)
  assert_equal [c2, c3], p.comments.range(1, 2)
  assert_equal [c1, c2, c3], p.comments.range(0, -1)
  assert_equal [c1, c2], p.comments.range(0, -2)
  assert_equal [c2, c3], p.comments.range(1, -1)
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
ohm_util-0.1 test/list.rb
ohm-3.1.1 test/list.rb
ohm-3.1.0 test/list.rb
ohm-3.0.3 test/list.rb
ohm-3.0.2 test/list.rb
ohm-3.0.1 test/list.rb
ohm-3.0.0 test/list.rb
ohm-2.3.0 test/list.rb
ohm-2.2.1 test/list.rb
sohm-0.0.1 test/list.rb
ohm-2.2.0 test/list.rb
ohm-2.1.0 test/list.rb
ohm-2.0.1 test/list.rb
ohm-2.0.0 test/list.rb
ohm-2.0.0.rc2 test/list.rb