remodel 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +0,0 @@
1
- require 'helper'
2
-
3
- class TestEntityShortnames < Test::Unit::TestCase
4
-
5
- class Bar < Remodel::Entity
6
- property :foo, :short => 'z'
7
- end
8
-
9
- context "[short names]" do
10
- setup do
11
- @bar = Bar.create(context, :foo => 42)
12
- end
13
-
14
- should "be used when storing properties" do
15
- serialized = redis.hget(context.key, @bar.key)
16
- assert !serialized.match(/foo/)
17
- assert serialized.match(/z/)
18
- end
19
-
20
- should "work in roundtrip" do
21
- @bar.reload
22
- assert_equal 42, @bar.foo
23
- end
24
-
25
- should "not be used in as_json" do
26
- assert !@bar.as_json.has_key?(:z)
27
- assert @bar.as_json.has_key?(:foo)
28
- end
29
-
30
- should "not be used in inspect" do
31
- assert !@bar.inspect.match(/z/)
32
- assert @bar.inspect.match(/foo/)
33
- end
34
- end
35
-
36
- end
@@ -1,77 +0,0 @@
1
- require 'helper'
2
-
3
- class TestManyToMany < Test::Unit::TestCase
4
-
5
- class Person < Remodel::Entity
6
- has_many :groups, :class => 'TestManyToMany::Group', :reverse => 'members'
7
- property :name
8
- end
9
-
10
- class Group < Remodel::Entity
11
- has_many :members, :class => 'TestManyToMany::Person', :reverse => 'groups'
12
- property :name
13
- end
14
-
15
- context "both associations" do
16
- should "be empty by default" do
17
- assert_equal [], Person.new(context).groups
18
- assert_equal [], Group.new(context).members
19
- end
20
-
21
- context "create" do
22
- should "add a new group to both associations" do
23
- tim = Person.create(context, :name => 'tim')
24
- rugb = tim.groups.create :name => 'rug-b'
25
- assert_equal [tim], rugb.members
26
- end
27
-
28
- should "add a new person to both associations" do
29
- rugb = Group.create(context, :name => 'rug-b')
30
- tim = rugb.members.create :name => 'tim'
31
- assert_equal [rugb], tim.groups
32
- end
33
- end
34
-
35
- context "add" do
36
- setup do
37
- @tim = Person.create(context, :name => 'tim')
38
- @rugb = Group.create(context, :name => 'rug-b')
39
- end
40
-
41
- should "add a new group to both associations" do
42
- @tim.groups.add(@rugb)
43
- assert_equal [@tim], @rugb.members
44
- assert_equal [@rugb], @tim.groups
45
- end
46
-
47
- should "add a new person to both associations" do
48
- @rugb.members.add(@tim)
49
- assert_equal [@tim], @rugb.members
50
- assert_equal [@rugb], @tim.groups
51
- end
52
- end
53
-
54
- context "remove" do
55
- setup do
56
- @tim = Person.create(context, :name => 'tim')
57
- @rugb = @tim.groups.create(:name => 'rug-b')
58
- @erlang = @tim.groups.create(:name => 'erlang')
59
- @aws = @tim.groups.create(:name => 'aws')
60
- end
61
-
62
- should "remove a group from both associations" do
63
- @tim.groups.remove(@erlang)
64
- assert_equal [@rugb, @aws], @tim.groups
65
- assert_equal [], @erlang.members
66
- end
67
-
68
- should "remove a person from both associations" do
69
- @erlang.members.remove(@tim)
70
- assert_equal [@rugb, @aws], @tim.groups
71
- assert_equal [], @erlang.members
72
- end
73
-
74
- end
75
- end
76
-
77
- end
@@ -1,117 +0,0 @@
1
- require 'helper'
2
- require 'json'
3
-
4
- class TestManyToOne < Test::Unit::TestCase
5
-
6
- class Puzzle < Remodel::Entity
7
- has_many :pieces, :class => 'TestManyToOne::Piece', :reverse => 'puzzle'
8
- property :topic
9
- end
10
-
11
- class Piece < Remodel::Entity
12
- has_one :puzzle, :class => 'TestManyToOne::Puzzle'
13
- property :color
14
- end
15
-
16
- context "has_many" do
17
- context "association" do
18
- should "exist" do
19
- assert Puzzle.create(context).respond_to?(:pieces)
20
- end
21
-
22
- should "return an empty list by default" do
23
- assert_equal [], Puzzle.create(context).pieces
24
- end
25
-
26
- should "return any existing children" do
27
- puzzle = Puzzle.create(context)
28
- red_piece = Piece.create(context, :color => 'red')
29
- blue_piece = Piece.create(context, :color => 'blue')
30
- value = JSON.generate([red_piece.key, blue_piece.key])
31
- redis.hset(context.key, "#{puzzle.key}_pieces", value)
32
- assert_equal 2, puzzle.pieces.size
33
- assert_equal Piece, puzzle.pieces[0].class
34
- assert_equal 'red', puzzle.pieces[0].color
35
- end
36
-
37
- should "not return any child multiple times" do
38
- puzzle = Puzzle.create(context)
39
- red_piece = Piece.create(context, :color => 'red')
40
- value = JSON.generate([red_piece.key, red_piece.key])
41
- redis.hset(context.key, "#{puzzle.key}_pieces", value)
42
- assert_equal 1, puzzle.pieces.size
43
- assert_equal Piece, puzzle.pieces[0].class
44
- assert_equal 'red', puzzle.pieces[0].color
45
- end
46
-
47
- context "create" do
48
- should "have a create method" do
49
- assert Puzzle.create(context).pieces.respond_to?(:create)
50
- end
51
-
52
- should "work without attributes" do
53
- puzzle = Puzzle.create(context)
54
- piece = puzzle.pieces.create
55
- assert piece.is_a?(Piece)
56
- end
57
-
58
- should "create and store a new child" do
59
- puzzle = Puzzle.create(context)
60
- puzzle.pieces.create :color => 'green'
61
- assert_equal 1, puzzle.pieces.size
62
- puzzle.reload
63
- assert_equal 1, puzzle.pieces.size
64
- assert_equal Piece, puzzle.pieces[0].class
65
- assert_equal 'green', puzzle.pieces[0].color
66
- end
67
-
68
- should "associate the created child with self" do
69
- puzzle = Puzzle.create(context, :topic => 'provence')
70
- piece = puzzle.pieces.create :color => 'green'
71
- assert_equal 'provence', piece.puzzle.topic
72
- end
73
- end
74
-
75
- context "add" do
76
- should "add the given entity to the association" do
77
- puzzle = Puzzle.create(context)
78
- piece = Piece.create(context, :color => 'white')
79
- puzzle.pieces.add piece
80
- assert_equal 1, puzzle.pieces.size
81
- puzzle.reload
82
- assert_equal 1, puzzle.pieces.size
83
- assert_equal Piece, puzzle.pieces[0].class
84
- assert_equal 'white', puzzle.pieces[0].color
85
- end
86
- end
87
-
88
- context "find" do
89
- setup do
90
- @puzzle = Puzzle.create(context)
91
- 5.times { @puzzle.pieces.create :color => 'blue' }
92
- end
93
-
94
- should "find the element with the given id" do
95
- piece = @puzzle.pieces[2]
96
- assert_equal piece, @puzzle.pieces.find(piece.id)
97
- end
98
-
99
- should "raise an exception if no element with the given id exists" do
100
- assert_raises(Remodel::EntityNotFound) { @puzzle.pieces.find(-1) }
101
- end
102
- end
103
-
104
- end
105
- end
106
-
107
- context "reload" do
108
- should "reset has_many associations" do
109
- puzzle = Puzzle.create(context)
110
- piece = puzzle.pieces.create :color => 'black'
111
- redis.hdel(context.key, "#{puzzle.key}_pieces")
112
- puzzle.reload
113
- assert_equal [], puzzle.pieces
114
- end
115
- end
116
-
117
- end
@@ -1,96 +0,0 @@
1
- require 'helper'
2
-
3
-
4
- class TestOneToMany < Test::Unit::TestCase
5
-
6
- class Piece < Remodel::Entity
7
- has_one :puzzle, :class => 'TestOneToMany::Puzzle', :reverse => 'pieces'
8
- property :color
9
- end
10
-
11
- class Puzzle < Remodel::Entity
12
- has_many :pieces, :class => 'TestOneToMany::Piece', :reverse => 'puzzle'
13
- property :topic
14
- end
15
-
16
- context "has_one" do
17
- context "association getter" do
18
- should "exist" do
19
- assert Piece.create(context).respond_to?(:puzzle)
20
- end
21
-
22
- should "return nil by default" do
23
- assert_nil Piece.create(context).puzzle
24
- end
25
-
26
- should "return the associated entity" do
27
- puzzle = Puzzle.create(context, :topic => 'animals')
28
- piece = Piece.create(context)
29
- redis.hset(context.key, "#{piece.key}_puzzle", puzzle.key)
30
- assert_equal 'animals', piece.puzzle.topic
31
- end
32
- end
33
-
34
- context "association setter" do
35
- should "exist" do
36
- assert Piece.create(context).respond_to?(:'puzzle=')
37
- end
38
-
39
- should "store the key of the associated entity" do
40
- puzzle = Puzzle.create(context)
41
- piece = Piece.create(context)
42
- piece.puzzle = puzzle
43
- assert_equal puzzle.key, redis.hget(context.key, "#{piece.key}_puzzle")
44
- end
45
-
46
- should "add the entity to the reverse association" do
47
- puzzle = Puzzle.create(context)
48
- piece = Piece.create(context)
49
- piece.puzzle = puzzle
50
- assert_equal 1, puzzle.pieces.size
51
- assert_equal piece.id, puzzle.pieces.first.id
52
- end
53
-
54
- should "remove the entity from the old reverse association" do
55
- puzzle = Puzzle.create(context)
56
- piece = puzzle.pieces.create
57
- new_puzzle = Puzzle.create(context)
58
- piece.puzzle = new_puzzle
59
- assert_equal [], puzzle.reload.pieces
60
- end
61
-
62
- should "be settable to nil" do
63
- piece = Piece.create(context)
64
- piece.puzzle = nil
65
- assert_nil piece.puzzle
66
- end
67
-
68
- should "remove the key if set to nil" do
69
- piece = Piece.create(context)
70
- piece.puzzle = Puzzle.create(context)
71
- piece.puzzle = nil
72
- assert_nil redis.hget(piece.context, "#{piece.key}_puzzle")
73
- end
74
-
75
- should "remove the entity from the reverse association if set to nil" do
76
- puzzle = Puzzle.create(context)
77
- piece = Piece.create(context)
78
- piece.puzzle = puzzle
79
- piece.puzzle = nil
80
- puzzle.reload
81
- assert_equal 0, puzzle.pieces.size
82
- end
83
- end
84
- end
85
-
86
- context "reload" do
87
- should "reset has_one associations" do
88
- piece = Piece.create(context, :color => 'black')
89
- piece.puzzle = Puzzle.create(context)
90
- redis.hdel(context.key, "#{piece.key}_puzzle")
91
- piece.reload
92
- assert_nil piece.puzzle
93
- end
94
- end
95
-
96
- end
@@ -1,57 +0,0 @@
1
- require 'helper'
2
-
3
- class TestOneToOne < Test::Unit::TestCase
4
-
5
- class Man < Remodel::Entity
6
- has_one :wife, :class => 'TestOneToOne::Woman', :reverse => 'husband'
7
- property :name
8
- end
9
-
10
- class Woman < Remodel::Entity
11
- has_one :husband, :class => 'TestOneToOne::Man', :reverse => 'wife'
12
- property :name
13
- end
14
-
15
- context "both associations" do
16
- should "be nil by default" do
17
- assert_equal nil, Man.new(context).wife
18
- assert_equal nil, Woman.new(context).husband
19
- end
20
-
21
- context "setter" do
22
- setup do
23
- @bill = Man.create(context, :name => 'Bill')
24
- @mary = Woman.create(context, :name => 'Mary')
25
- end
26
-
27
- context "non-nil value" do
28
- should "also set husband" do
29
- @bill.wife = @mary
30
- assert_equal @bill, @mary.husband
31
- end
32
-
33
- should "also set wife" do
34
- @mary.husband = @bill
35
- assert_equal @mary, @bill.wife
36
- end
37
- end
38
-
39
- context "nil value" do
40
- setup do
41
- @bill.wife = @mary
42
- end
43
-
44
- should "also clear husband" do
45
- @bill.wife = nil
46
- assert_equal nil, @mary.husband
47
- end
48
-
49
- should "also clear wife" do
50
- @mary.husband = nil
51
- assert_equal nil, @bill.wife
52
- end
53
- end
54
- end
55
- end
56
-
57
- end