Added simplecov
[erd-marker.git] / spec / erd_handler / erd_spec.rb
1 require 'spec_helper'
2
3 module ErdHandler
4 describe Erd do
5 let(:input) { double('input').as_null_object }
6 let(:output) { double('output').as_null_object }
7 let(:erd) { Erd.new(input, output) }
8
9 describe "#initialize" do
10 it "creates an empty ERD" do
11 erd = Erd.new
12 erd.mark.should be_nil
13 erd.should have(0).vertices
14 erd.should have(0).edges
15 end
16
17 it "reads and creates a single box" do
18 erd = Erd.new(File.new("spec/fixtures/single_box_erd.xml"))
19 erd.mark.should == 6.5
20 erd.should have(1).vertices
21 erd.should have(0).edges
22 end
23 end # #initialize
24
25 describe "#read" do
26 it "reads and creates a single box" do
27 erd = Erd.new
28 erd.read(File.new("spec/fixtures/single_box_erd.xml"))
29 erd.mark.should == 6.5
30 erd.should have(1).vertices
31 erd.should have(0).edges
32 end
33
34 it "reads and creates two boxes with an edge joining them" do
35 erd = Erd.new
36 erd.read(File.new("spec/fixtures/two_boxes_one_link_erd.xml"))
37 erd.mark.should == 4.5
38 erd.should have(2).vertices
39 erd.should have(1).edges
40
41 erd.vertices[0].should have(1).neighbours
42 erd.vertices[1].should have(1).neighbours
43 erd.vertices[0].neighbours.should include(erd.vertices[1])
44 erd.vertices[1].neighbours.should include(erd.vertices[0])
45
46 link = erd.edges[0]
47 b0 = erd.vertices.find {|v| v.id == 0}
48 b1 = erd.vertices.find {|v| v.id == 1}
49 link.connection_at(b0).blob.should be(:closed)
50 link.connection_at(b0).crowsfoot.should be(:yes)
51 link.connection_at(b1).blob.should be(:open)
52 link.connection_at(b1).crowsfoot.should be(:no)
53 end
54
55 it "reads and creates a box with a self-loop" do
56 erd = Erd.new
57 erd.read(File.new("spec/fixtures/single_box_self_loop_erd.xml"))
58 erd.mark.should == 4.5
59 erd.should have(1).vertices
60 erd.should have(1).edges
61
62 erd.vertices[0].should have(1).neighbours
63 erd.vertices[0].neighbours.should include(erd.vertices[0])
64
65 link = erd.edges[0]
66 box = erd.vertices[0]
67 c1 = link.connections.find {|c| c.blob == :closed}
68 c1.crowsfoot.should be(:yes)
69 c2 = link.connections.find {|c| c.blob == :open}
70 c2.crowsfoot.should be(:no)
71 c1.should_not == c2
72 end
73
74 it "reads and creates an ERD with subclassing" do
75 erd = Erd.new
76 erd.read(File.new("spec/fixtures/two_boxes_one_contained_erd.xml"))
77 erd.mark.should == 4.5
78 erd.should have(2).vertices
79 erd.should have(0).edges
80
81 erd.vertices[0].should have(0).neighbours
82 erd.vertices[1].should have(0).neighbours
83
84 erd.vertices[0].within?(erd.vertices[1]).should be(true)
85 erd.vertices[1].should be_contains(erd.vertices[0])
86 end
87
88 it "reads and creates full diagram" do
89 erd = Erd.new
90 erd.read(File.new("spec/fixtures/complex_erd.xml"))
91 erd.mark.should == 4.0
92 erd.should have(5).vertices
93 erd.should have(6).edges
94
95 b0 = erd.vertices.find {|b| b.id == 0}
96 b0.name.original.should == "Unit"
97 b0.should have(2).neighbours
98
99 b1 = erd.vertices.find {|b| b.id == 1}
100 b1.name.original.should == "Employee"
101 b1.should have(2).neighbours
102
103 b2 = erd.vertices.find {|b| b.id == 2}
104 b2.name.original.should == "Course"
105 b2.should have(3).neighbours
106
107 b3 = erd.vertices.find {|b| b.id == 3}
108 b3.name.original.should == "Presentation"
109 b3.should have(3).neighbours
110
111 b4 = erd.vertices.find {|b| b.id == 4}
112 b4.name.original.should == "Client"
113 b4.should have(1).neighbours
114
115 l0 = erd.edges.find {|e| e.id == 0}
116 l0.name.original.should == "ConsistsOf"
117 l0.connections.find {|c| c.end == b0}.blob.should be :closed
118 l0.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
119 l0.connections.find {|c| c.end == b2}.blob.should be :closed
120 l0.connections.find {|c| c.end == b2}.crowsfoot.should be :no
121
122 l1 = erd.edges.find {|e| e.id == 1}
123 l1.name.original.should == "Prepares"
124 l1.connections.find {|c| c.end == b0}.blob.should be :open
125 l1.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
126 l1.connections.find {|c| c.end == b1}.blob.should be :closed
127 l1.connections.find {|c| c.end == b1}.crowsfoot.should be :no
128
129 l2 = erd.edges.find {|e| e.id == 2}
130 l2.name.original.should == "Presents"
131 l2.connections.find {|c| c.end == b1}.blob.should be :closed
132 l2.connections.find {|c| c.end == b1}.crowsfoot.should be :no
133 l2.connections.find {|c| c.end == b3}.blob.should be :closed
134 l2.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
135
136 l3 = erd.edges.find {|e| e.id == 3}
137 l3.name.original.should == "Presented"
138 l3.connections.find {|c| c.end == b2}.blob.should be :open
139 l3.connections.find {|c| c.end == b2}.crowsfoot.should be :no
140 l3.connections.find {|c| c.end == b3}.blob.should be :closed
141 l3.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
142
143 l4 = erd.edges.find {|e| e.id == 4}
144 l4.name.original.should == "Recieves"
145 l4.connections.find {|c| c.end == b3}.blob.should be :closed
146 l4.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
147 l4.connections.find {|c| c.end == b4}.blob.should be :closed
148 l4.connections.find {|c| c.end == b4}.crowsfoot.should be :no
149
150 l5 = erd.edges.find {|e| e.id == 5}
151 l5.name.original.should == "IsPrerequisiteOf"
152 l5.connections.find {|c| c.crowsfoot == :yes}.blob.should be :open
153 l5.connections.find {|c| c.crowsfoot == :no}.blob.should be :open
154 l5.connections.find {|c| c.crowsfoot == :yes}.end.should be b2
155 l5.connections.find {|c| c.crowsfoot == :no}.end.should be b2
156 end
157 end # #read
158
159 describe "#mmus" do
160 it "finds three MMUs in a simple ERD" do
161 erd = Erd.new
162 erd.read(File.new("spec/fixtures/two_boxes_one_link_erd.xml"))
163 mmus = erd.mmus
164
165 mmus.should have(3).items
166 single_box_mmus = mmus.select {|m| m.vertices.length == 1}
167 single_link_mmus = mmus.select {|m| m.edges.length == 1}
168
169 single_box_mmus.should have(2).items
170 single_box_mmus.each do |m|
171 m.should have(1).vertices
172 m.should have(0).edges
173 end
174
175 single_link_mmus.should have(1).items
176 single_link_mmus.each do |m|
177 m.should have(2).vertices
178 m.should have(1).edges
179 m.edges.first.should have(2).connections
180 m.vertices.each do |v|
181 m.vertices.should include(v)
182 end
183 end
184 end
185
186 it "finds many MMUs in a complex ERD" do
187 erd = Erd.new
188 erd.read(File.new("spec/fixtures/complex_erd.xml"))
189 mmus = erd.mmus
190
191 mmus.should have(11).items
192 single_box_mmus = mmus.select {|m| m.vertices.length == 1}
193 single_link_mmus = mmus.select {|m| m.edges.length == 1}
194
195 single_box_mmus.should have(5).items
196 single_box_mmus.each do |m|
197 m.should have(1).vertices
198 m.should have(0).edges
199 end
200 single_box_mmus.map {|m| m.vertices.first.name.original}.uniq.should have(5).items
201
202 single_link_mmus.should have(6).items
203 single_link_mmus.each do |m|
204 m.should have(2).vertices
205 m.should have(1).edges
206 m.edges.first.should have(2).connections
207 m.vertices.each do |v|
208 m.vertices.should include(v)
209 end
210 end
211 single_link_mmus.map {|m| m.edges.first.name.original}.uniq.should have(6).items
212 end
213
214 end # #mmus
215 end
216 end