Sha256: 7699ce746d118f691a54697e936ecc5850f499fc9b0f0d5068a2f68d79b9d0be

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

require_relative "helper"

class Post < Sohm::Model
  include Sohm::AutoId

  list :comments, :Comment
end

class Comment < Sohm::Model
  include Sohm::AutoId
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 "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, Sohm.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

7 entries across 7 versions & 1 rubygems

Version Path
sohm-0.10.5 test/list.rb
sohm-0.10.4 test/list.rb
sohm-0.10.3 test/list.rb
sohm-0.10.2 test/list.rb
sohm-0.10.1 test/list.rb
sohm-0.10.0 test/list.rb
sohm-0.9.0 test/list.rb