forked from tlossen/remodel
/
test_has_many.rb
105 lines (89 loc) · 3.1 KB
/
test_has_many.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'helper'
require 'json'
class TestHasMany < Test::Unit::TestCase
class Puzzle < Remodel::Entity
has_many :pieces, :class => 'TestHasMany::Piece'
property :topic
end
class Piece < Remodel::Entity
property :color
end
context "association" do
should "exist" do
assert Puzzle.create(context).respond_to?(:pieces)
end
should "return an empty list by default" do
assert_equal [], Puzzle.create(context).pieces
end
should "return any existing children" do
puzzle = Puzzle.create(context)
red_piece = Piece.create(context, :color => 'red')
blue_piece = Piece.create(context, :color => 'blue')
redis.hset(context.key, "#{puzzle.key}_pieces", "#{red_piece.key} #{blue_piece.key}")
assert_equal 2, puzzle.pieces.size
assert_equal Piece, puzzle.pieces[0].class
assert_equal 'red', puzzle.pieces[0].color
end
should "not return any child multiple times" do
puzzle = Puzzle.create(context)
red_piece = Piece.create(context, :color => 'red')
redis.hset(context.key, "#{puzzle.key}_pieces", "#{red_piece.key} #{red_piece.key}")
assert_equal 1, puzzle.pieces.size
assert_equal Piece, puzzle.pieces[0].class
assert_equal 'red', puzzle.pieces[0].color
end
context "create" do
should "have a create method" do
assert Puzzle.create(context).pieces.respond_to?(:create)
end
should "work without attributes" do
puzzle = Puzzle.create(context)
piece = puzzle.pieces.create
assert piece.is_a?(Piece)
end
should "create and store a new child" do
puzzle = Puzzle.create(context)
puzzle.pieces.create :color => 'green'
assert_equal 1, puzzle.pieces.size
puzzle.reload
assert_equal 1, puzzle.pieces.size
assert_equal Piece, puzzle.pieces[0].class
assert_equal 'green', puzzle.pieces[0].color
end
end
context "add" do
should "add the given entity to the association" do
puzzle = Puzzle.create(context)
piece = Piece.create(context, :color => 'white')
puzzle.pieces.add piece
assert_equal 1, puzzle.pieces.size
puzzle.reload
assert_equal 1, puzzle.pieces.size
assert_equal Piece, puzzle.pieces[0].class
assert_equal 'white', puzzle.pieces[0].color
end
end
context "find" do
setup do
@puzzle = Puzzle.create(context)
5.times { @puzzle.pieces.create :color => 'blue' }
end
should "find the element with the given id" do
piece = @puzzle.pieces[2]
assert_equal piece, @puzzle.pieces.find(piece.id)
end
should "raise an exception if no element with the given id exists" do
assert_raises(Remodel::EntityNotFound) { @puzzle.pieces.find(-1) }
end
end
end
context "reload" do
should "reset has_many associations" do
puzzle = Puzzle.create(context)
piece = puzzle.pieces.create :color => 'black'
redis.hdel(context.key, "#{puzzle.key}_pieces")
puzzle.reload
assert_equal [], puzzle.pieces
end
end
end