6b33dd8e3dbfc92238b3c76aadcb5b5201103079
[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 full diagram" do
75 erd = Erd.new
76 erd.read(File.new("spec/fixtures/complex_erd.xml"))
77 erd.mark.should == 4.0
78 erd.should have(5).vertices
79 erd.should have(6).edges
80
81 b0 = erd.vertices.find {|b| b.id == 0}
82 b0.name.original.should == "Unit"
83 b0.should have(2).neighbours
84
85 b1 = erd.vertices.find {|b| b.id == 1}
86 b1.name.original.should == "Employee"
87 b1.should have(2).neighbours
88
89 b2 = erd.vertices.find {|b| b.id == 2}
90 b2.name.original.should == "Course"
91 b2.should have(3).neighbours
92
93 b3 = erd.vertices.find {|b| b.id == 3}
94 b3.name.original.should == "Presentation"
95 b3.should have(3).neighbours
96
97 b4 = erd.vertices.find {|b| b.id == 4}
98 b4.name.original.should == "Client"
99 b4.should have(1).neighbours
100
101 l0 = erd.edges.find {|e| e.id == 0}
102 l0.name.original.should == "ConsistsOf"
103 l0.connections.find {|c| c.end == b0}.blob.should be :closed
104 l0.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
105 l0.connections.find {|c| c.end == b2}.blob.should be :closed
106 l0.connections.find {|c| c.end == b2}.crowsfoot.should be :no
107
108 l1 = erd.edges.find {|e| e.id == 1}
109 l1.name.original.should == "Prepares"
110 l1.connections.find {|c| c.end == b0}.blob.should be :open
111 l1.connections.find {|c| c.end == b0}.crowsfoot.should be :yes
112 l1.connections.find {|c| c.end == b1}.blob.should be :closed
113 l1.connections.find {|c| c.end == b1}.crowsfoot.should be :no
114
115 l2 = erd.edges.find {|e| e.id == 2}
116 l2.name.original.should == "Presents"
117 l2.connections.find {|c| c.end == b1}.blob.should be :closed
118 l2.connections.find {|c| c.end == b1}.crowsfoot.should be :no
119 l2.connections.find {|c| c.end == b3}.blob.should be :closed
120 l2.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
121
122 l3 = erd.edges.find {|e| e.id == 3}
123 l3.name.original.should == "Presented"
124 l3.connections.find {|c| c.end == b2}.blob.should be :open
125 l3.connections.find {|c| c.end == b2}.crowsfoot.should be :no
126 l3.connections.find {|c| c.end == b3}.blob.should be :closed
127 l3.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
128
129 l4 = erd.edges.find {|e| e.id == 4}
130 l4.name.original.should == "Recieves"
131 l4.connections.find {|c| c.end == b3}.blob.should be :closed
132 l4.connections.find {|c| c.end == b3}.crowsfoot.should be :yes
133 l4.connections.find {|c| c.end == b4}.blob.should be :closed
134 l4.connections.find {|c| c.end == b4}.crowsfoot.should be :no
135
136 l5 = erd.edges.find {|e| e.id == 5}
137 l5.name.original.should == "IsPrerequisiteOf"
138 l5.connections.find {|c| c.crowsfoot == :yes}.blob.should be :open
139 l5.connections.find {|c| c.crowsfoot == :no}.blob.should be :open
140 l5.connections.find {|c| c.crowsfoot == :yes}.end.should be b2
141 l5.connections.find {|c| c.crowsfoot == :no}.end.should be b2
142 end
143 end # #read
144
145 describe "#mmus" do
146 it "finds three MMUs in a simple ERD" do
147 erd = Erd.new
148 erd.read(File.new("spec/fixtures/two_boxes_one_link_erd.xml"))
149 mmus = erd.mmus
150
151 mmus.should have(3).items
152 single_box_mmus = mmus.select {|m| m.vertices.length == 1}
153 single_link_mmus = mmus.select {|m| m.edges.length == 1}
154
155 single_box_mmus.should have(2).items
156 single_box_mmus.each do |m|
157 m.should have(1).vertices
158 m.should have(0).edges
159 end
160
161 single_link_mmus.should have(1).items
162 single_link_mmus.each do |m|
163 m.should have(2).vertices
164 m.should have(1).edges
165 m.edges.first.should have(2).connections
166 m.vertices.each do |v|
167 m.vertices.should include(v)
168 end
169 end
170 end
171
172 it "finds many MMUs in a complex ERD" do
173 erd = Erd.new
174 erd.read(File.new("spec/fixtures/complex_erd.xml"))
175 mmus = erd.mmus
176
177 mmus.should have(11).items
178 single_box_mmus = mmus.select {|m| m.vertices.length == 1}
179 single_link_mmus = mmus.select {|m| m.edges.length == 1}
180
181 single_box_mmus.should have(5).items
182 single_box_mmus.each do |m|
183 m.should have(1).vertices
184 m.should have(0).edges
185 end
186 single_box_mmus.map {|m| m.vertices.first.name.original}.uniq.should have(5).items
187
188 single_link_mmus.should have(6).items
189 single_link_mmus.each do |m|
190 m.should have(2).vertices
191 m.should have(1).edges
192 m.edges.first.should have(2).connections
193 m.vertices.each do |v|
194 m.vertices.should include(v)
195 end
196 end
197 single_link_mmus.map {|m| m.edges.first.name.original}.uniq.should have(6).items
198 end
199
200 end # #mmus
201 end
202 end