rails-erd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ require File.expand_path("../test_helper", File.dirname(__FILE__))
2
+
3
+ class EntityTest < ActiveSupport::TestCase
4
+ # Entity ===================================================================
5
+ test "model should return active record model" do
6
+ create_models "Foo"
7
+ assert_equal Foo, Entity.new(Domain.new, Foo).model
8
+ end
9
+
10
+ test "name should return model name" do
11
+ create_models "Foo"
12
+ assert_equal "Foo", Entity.new(Domain.new, Foo).name
13
+ end
14
+
15
+ test "spaceship should sort entities by name" do
16
+ create_models "Foo", "Bar"
17
+ foo, bar = Entity.new(Domain.new, Foo), Entity.new(Domain.new, Bar)
18
+ assert_equal [bar, foo], [foo, bar].sort
19
+ end
20
+
21
+ test "to_s should equal name" do
22
+ create_models "Foo"
23
+ assert_equal "Foo", Entity.new(Domain.new, Foo).to_s
24
+ end
25
+
26
+ test "inspect should show name" do
27
+ create_models "Foo"
28
+ assert_match %r{#<RailsERD::Entity:.* @model=Foo>}, Entity.new(Domain.new, Foo).inspect
29
+ end
30
+
31
+ test "relationships should return relationships for this model" do
32
+ create_model "Foo", :bar => :references do
33
+ belongs_to :bar
34
+ end
35
+ create_model "Bar", :baz => :references do
36
+ belongs_to :baz
37
+ end
38
+ create_model "Baz"
39
+
40
+ domain = Domain.generate
41
+ foo = domain.entity_for(Foo)
42
+ assert_equal domain.relationships.select { |r| r.destination == foo }, foo.relationships
43
+ end
44
+
45
+ test "relationships should return relationships that connect to this model" do
46
+ create_model "Foo", :bar => :references
47
+ create_model "Bar", :baz => :references do
48
+ belongs_to :baz
49
+ has_many :foos
50
+ end
51
+ create_model "Baz"
52
+
53
+ domain = Domain.generate
54
+ foo = domain.entity_for(Foo)
55
+ assert_equal domain.relationships.select { |r| r.destination == foo }, foo.relationships
56
+ end
57
+
58
+ # Entity properties ========================================================
59
+ test "connected should return false for unconnected entities" do
60
+ create_models "Foo", "Bar"
61
+ assert_equal [false, false], Domain.generate.entities.map(&:connected?)
62
+ end
63
+
64
+ test "connected should return true for connected entities" do
65
+ create_model "Foo", :bar => :references do
66
+ belongs_to :bar
67
+ end
68
+ create_model "Bar"
69
+ assert_equal [true, true], Domain.generate.entities.map(&:connected?)
70
+ end
71
+
72
+ # Attribute processing =====================================================
73
+ test "attributes should return list of attributes" do
74
+ create_model "Bar", :some_column => :integer, :another_column => :string
75
+ assert_equal [Attribute] * 3, Entity.new(Domain.new, Bar).attributes.collect(&:class)
76
+ end
77
+
78
+ test "attributes should return attributes sorted by name" do
79
+ create_model "Bar", :some_column => :integer, :another_column => :string
80
+ assert_equal ["another_column", "id", "some_column"], Entity.new(Domain.new, Bar).attributes.collect(&:name)
81
+ end
82
+ end
@@ -0,0 +1,308 @@
1
+ require File.expand_path("../test_helper", File.dirname(__FILE__))
2
+
3
+ class RelationshipTest < ActiveSupport::TestCase
4
+ # Relationship =============================================================
5
+ test "inspect should show source and destination" do
6
+ create_model "Foo", :bar => :references do
7
+ belongs_to :bar
8
+ end
9
+ create_model "Bar"
10
+ domain = Domain.generate
11
+ assert_match %r{#<RailsERD::Relationship:.* @source=Bar @destination=Foo>}, domain.relationships.first.inspect
12
+ end
13
+
14
+ test "source should return relationship source" do
15
+ create_model "Foo", :bar => :references do
16
+ belongs_to :bar
17
+ end
18
+ create_model "Bar"
19
+ domain = Domain.generate
20
+ assert_equal [domain.entity_for(Bar)], domain.relationships.map(&:source)
21
+ end
22
+
23
+ test "destination should return relationship destination" do
24
+ create_model "Foo", :bar => :references do
25
+ belongs_to :bar
26
+ end
27
+ create_model "Bar"
28
+ domain = Domain.generate
29
+ assert_equal [domain.entity_for(Foo)], domain.relationships.map(&:destination)
30
+ end
31
+
32
+ # Relationship properties ==================================================
33
+ test "mutual should return false for one way relationship" do
34
+ create_model "Foo", :bar => :references do
35
+ belongs_to :bar
36
+ end
37
+ create_model "Bar"
38
+ domain = Domain.generate
39
+ assert_equal [false], domain.relationships.map(&:mutual?)
40
+ end
41
+
42
+ test "mutual should return true for mutual relationship" do
43
+ create_model "Foo", :bar => :references do
44
+ belongs_to :bar
45
+ end
46
+ create_model "Bar" do
47
+ has_many :foos
48
+ end
49
+ domain = Domain.generate
50
+ assert_equal [true], domain.relationships.map(&:mutual?)
51
+ end
52
+
53
+ test "recursive should return false for ordinary relationship" do
54
+ create_model "Foo", :bar => :references do
55
+ belongs_to :bar
56
+ end
57
+ create_model "Bar" do
58
+ has_many :foos
59
+ end
60
+ domain = Domain.generate
61
+ assert_equal [false], domain.relationships.map(&:recursive?)
62
+ end
63
+
64
+ test "recursive should return true for self referencing relationship" do
65
+ create_model "Foo", :foo => :references do
66
+ belongs_to :foo
67
+ end
68
+ domain = Domain.generate
69
+ assert_equal [true], domain.relationships.map(&:recursive?)
70
+ end
71
+
72
+ test "indirect should return false for ordinary relationship" do
73
+ create_model "Foo", :bar => :references do
74
+ belongs_to :bar
75
+ end
76
+ create_model "Bar" do
77
+ has_many :foos
78
+ end
79
+ domain = Domain.generate
80
+ assert_equal [false], domain.relationships.map(&:indirect?)
81
+ end
82
+
83
+ test "indirect should return true if relationship is a through association" do
84
+ create_model "Foo", :baz => :references, :bar => :references do
85
+ belongs_to :baz
86
+ belongs_to :bar
87
+ end
88
+ create_model "Bar" do
89
+ has_many :foos
90
+ has_many :bazs, :through => :foos
91
+ end
92
+ create_model "Baz" do
93
+ has_many :foos
94
+ end
95
+ domain = Domain.generate
96
+ assert_equal true, domain.relationships.find { |rel|
97
+ rel.source.model == Bar and rel.destination.model == Baz }.indirect?
98
+ end
99
+
100
+ test "strength should return one for relationship with one association" do
101
+ create_model "Foo", :bar => :references
102
+ create_model "Bar" do
103
+ has_many :foos
104
+ end
105
+ domain = Domain.generate
106
+ assert_equal [1], domain.relationships.map(&:strength)
107
+ end
108
+
109
+ test "strength should return two for relationship with two associations" do
110
+ create_model "Foo", :bar => :references do
111
+ belongs_to :bar
112
+ end
113
+ create_model "Bar" do
114
+ has_many :foos
115
+ end
116
+ domain = Domain.generate
117
+ assert_equal [2], domain.relationships.map(&:strength)
118
+ end
119
+
120
+ test "strength should return number of associations that make up the relationship" do
121
+ create_model "Foo", :bar => :references do
122
+ belongs_to :bar
123
+ belongs_to :special_bar, :class_name => "Bar", :foreign_key => :bar_id
124
+ end
125
+ create_model "Bar" do
126
+ has_many :foos
127
+ has_many :special_foos, :class_name => "Foo", :foreign_key => :bar_id
128
+ end
129
+ domain = Domain.generate
130
+ assert_equal [4], domain.relationships.map(&:strength)
131
+ end
132
+
133
+ # Cardinality ==============================================================
134
+ test "cardinality should return one to one for has_one associations" do
135
+ create_model "Foo", :bar => :references
136
+ create_model "Bar" do
137
+ has_one :foo
138
+ end
139
+ domain = Domain.generate
140
+ assert_equal [Relationship::Cardinality::OneToOne], domain.relationships.map(&:cardinality)
141
+ end
142
+
143
+ test "cardinality should return one to many for has_many associations" do
144
+ create_model "Foo", :bar => :references
145
+ create_model "Bar" do
146
+ has_many :foos
147
+ end
148
+ domain = Domain.generate
149
+ assert_equal [Relationship::Cardinality::OneToMany], domain.relationships.map(&:cardinality)
150
+ end
151
+
152
+ test "cardinality should return many to many for has_and_belongs_to_many associations" do
153
+ create_table "bars_foos", :foo_id => :integer, :bar_id => :integer
154
+ create_model "Foo" do
155
+ has_and_belongs_to_many :bars
156
+ end
157
+ create_model "Bar" do
158
+ has_and_belongs_to_many :foos
159
+ end
160
+ domain = Domain.generate
161
+ assert_equal [Relationship::Cardinality::ManyToMany], domain.relationships.map(&:cardinality)
162
+ end
163
+
164
+ test "cardinality should return one to many for multiple associations with maximum cardinality of has_many" do
165
+ create_model "Foo", :bar => :references
166
+ create_model "Bar" do
167
+ has_one :foo
168
+ has_many :foos
169
+ end
170
+ domain = Domain.generate
171
+ assert_equal [Relationship::Cardinality::OneToMany], domain.relationships.map(&:cardinality)
172
+ end
173
+
174
+ test "cardinality should return one to many if forward association is missing" do
175
+ create_model "Foo", :bar => :references do
176
+ belongs_to :bar
177
+ end
178
+ create_model "Bar"
179
+ domain = Domain.generate
180
+ assert_equal [Relationship::Cardinality::OneToMany], domain.relationships.map(&:cardinality)
181
+ end
182
+
183
+ # test "cardinality should return zero or more for has_many association" do
184
+ # create_model "Foo", :bar => :references do
185
+ # belongs_to :bar
186
+ # end
187
+ # create_model "Bar" do
188
+ # has_many :foos
189
+ # end
190
+ # domain = Domain.generate
191
+ # assert_equal Cardinality::ZeroOrMore, domain.relationships.first.cardinality
192
+ # end
193
+ #
194
+ # test "cardinality should return one or more for validated has_many association" do
195
+ # create_model "Foo", :bar => :references do
196
+ # belongs_to :bar
197
+ # end
198
+ # create_model "Bar" do
199
+ # has_many :foos
200
+ # validates :foos, :presence => true
201
+ # end
202
+ # domain = Domain.generate
203
+ # assert_equal Cardinality::OneOrMore, domain.relationships.first.cardinality
204
+ # end
205
+ #
206
+ # test "cardinality should return zero or more for has_many association with foreign database constraint" do
207
+ # create_model "Foo" do
208
+ # belongs_to :bar
209
+ # end
210
+ # add_column :foos, :bar_id, :integer, :null => false, :default => 0
211
+ # create_model "Bar" do
212
+ # has_many :foos
213
+ # end
214
+ # domain = Domain.generate
215
+ # assert_equal Cardinality::ZeroOrMore, domain.relationships.first.cardinality
216
+ # end
217
+ #
218
+ # test "cardinality should return zero or one for has_one association" do
219
+ # create_model "Foo", :bar => :references do
220
+ # belongs_to :bar
221
+ # end
222
+ # create_model "Bar" do
223
+ # has_one :foo
224
+ # end
225
+ # domain = Domain.generate
226
+ # assert_equal Cardinality::ZeroOrOne, domain.relationships.first.cardinality
227
+ # end
228
+ #
229
+ # test "cardinality should return exactly one for validated has_one association" do
230
+ # create_model "Foo", :bar => :references do
231
+ # belongs_to :bar
232
+ # end
233
+ # create_model "Bar" do
234
+ # has_one :foo
235
+ # validates :foo, :presence => true
236
+ # end
237
+ # domain = Domain.generate
238
+ # assert_equal Cardinality::ExactlyOne, domain.relationships.first.cardinality
239
+ # end
240
+ #
241
+ # test "cardinality should return exactly one for has_one association with foreign database constraint" do
242
+ # create_model "Foo" do
243
+ # belongs_to :bar
244
+ # end
245
+ # add_column :foos, :bar_id, :integer, :null => false, :default => 0
246
+ # create_model "Bar" do
247
+ # has_one :foo
248
+ # end
249
+ # domain = Domain.generate
250
+ # assert_equal Cardinality::ZeroOrOne, domain.relationships.first.cardinality
251
+ # end
252
+ #
253
+ # # Reverse cardinality ======================================================
254
+ # test "reverse_cardinality should return nil if reverse association is missing" do
255
+ # create_model "Foo", :bar => :references
256
+ # create_model "Bar" do
257
+ # has_many :foos
258
+ # end
259
+ # domain = Domain.generate
260
+ # assert_nil domain.relationships.first.reverse_cardinality
261
+ # end
262
+ #
263
+ # test "reverse_cardinality should return zero or one for has_many association" do
264
+ # create_model "Foo", :bar => :references do
265
+ # belongs_to :bar
266
+ # end
267
+ # create_model "Bar" do
268
+ # has_many :foos
269
+ # end
270
+ # domain = Domain.generate
271
+ # assert_equal Cardinality::ZeroOrOne, domain.relationships.first.reverse_cardinality
272
+ # end
273
+ #
274
+ # test "reverse_cardinality should return exactly one for validated has_many association" do
275
+ # create_model "Foo", :bar => :references do
276
+ # belongs_to :bar
277
+ # validates :bar, :presence => true
278
+ # end
279
+ # create_model "Bar" do
280
+ # has_many :foos
281
+ # end
282
+ # domain = Domain.generate
283
+ # assert_equal Cardinality::ExactlyOne, domain.relationships.first.reverse_cardinality
284
+ # end
285
+ #
286
+ # test "reverse_cardinality should return zero or one for has_one association" do
287
+ # create_model "Foo", :bar => :references do
288
+ # belongs_to :bar
289
+ # end
290
+ # create_model "Bar" do
291
+ # has_one :foo
292
+ # end
293
+ # domain = Domain.generate
294
+ # assert_equal Cardinality::ZeroOrOne, domain.relationships.first.reverse_cardinality
295
+ # end
296
+ #
297
+ # test "reverse_cardinality should return exactly one for validated has_one association" do
298
+ # create_model "Foo", :bar => :references do
299
+ # belongs_to :bar
300
+ # validates :bar, :presence => true
301
+ # end
302
+ # create_model "Bar" do
303
+ # has_one :foo
304
+ # end
305
+ # domain = Domain.generate
306
+ # assert_equal Cardinality::ExactlyOne, domain.relationships.first.reverse_cardinality
307
+ # end
308
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-erd
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Rolf Timmermans
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-20 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: ruby-graphviz
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 9
46
+ - 17
47
+ version: 0.9.17
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Automatically generate an entity-relationship diagram (ERD) for the models in your Rails application.
51
+ email: r.timmermans@voormedia.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE
58
+ - README.rdoc
59
+ files:
60
+ - .gitignore
61
+ - CHANGES.rdoc
62
+ - LICENSE
63
+ - README.rdoc
64
+ - Rakefile
65
+ - VERSION
66
+ - lib/rails-erd.rb
67
+ - lib/rails_erd.rb
68
+ - lib/rails_erd/attribute.rb
69
+ - lib/rails_erd/diagram.rb
70
+ - lib/rails_erd/domain.rb
71
+ - lib/rails_erd/entity.rb
72
+ - lib/rails_erd/railtie.rb
73
+ - lib/rails_erd/relationship.rb
74
+ - lib/rails_erd/relationship/cardinality.rb
75
+ - lib/rails_erd/tasks.rake
76
+ - lib/rails_erd/templates/node.erb
77
+ - test/test_helper.rb
78
+ - test/unit/attribute_test.rb
79
+ - test/unit/cardinality_test.rb
80
+ - test/unit/diagram_test.rb
81
+ - test/unit/domain_test.rb
82
+ - test/unit/entity_test.rb
83
+ - test/unit/relationship_test.rb
84
+ has_rdoc: true
85
+ homepage: http://rails-erd.rubyforge.org/
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options:
90
+ - --charset=UTF-8
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project: rails-erd
112
+ rubygems_version: 1.3.7
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Entity-relationship diagram for your Rails models.
116
+ test_files:
117
+ - test/test_helper.rb
118
+ - test/unit/attribute_test.rb
119
+ - test/unit/cardinality_test.rb
120
+ - test/unit/diagram_test.rb
121
+ - test/unit/domain_test.rb
122
+ - test/unit/entity_test.rb
123
+ - test/unit/relationship_test.rb