test/unit/graphviz_test.rb in rails-erd-0.4.3 vs test/unit/graphviz_test.rb in rails-erd-0.4.4
- old
+ new
@@ -4,22 +4,22 @@
def setup
RailsERD.options.filetype = :png
RailsERD.options.warn = false
load "rails_erd/diagram/graphviz.rb"
end
-
+
def teardown
FileUtils.rm Dir["ERD.*"] rescue nil
RailsERD::Diagram.send :remove_const, :Graphviz rescue nil
end
-
+
def diagram(options = {})
@diagram ||= Diagram::Graphviz.new(Domain.generate(options), options).tap do |diagram|
diagram.generate
end
end
-
+
def find_dot_nodes(diagram)
[].tap do |nodes|
diagram.graph.each_node do |name, node|
nodes << node
end
@@ -35,19 +35,19 @@
diagram.graph.each_edge do |edge|
edges << [edge.node_one, edge.node_two]
end
end
end
-
+
def find_dot_edges(diagram)
[].tap do |edges|
diagram.graph.each_edge do |edge|
edges << edge
end
end
end
-
+
def find_dot_edge_styles(diagram)
find_dot_edges(diagram).map { |e| [e[:arrowtail].to_s.tr('"', ''), e[:arrowhead].to_s.tr('"', '')] }
end
# Diagram properties =======================================================
@@ -57,21 +57,21 @@
assert_equal "ERD.svg", Diagram::Graphviz.create(:filetype => :svg)
ensure
FileUtils.rm "ERD.svg" rescue nil
end
end
-
+
test "rank direction should be lr for horizontal orientation" do
create_simple_domain
assert_equal '"LR"', diagram(:orientation => :horizontal).graph[:rankdir].to_s
end
test "rank direction should be tb for vertical orientation" do
create_simple_domain
assert_equal '"TB"', diagram(:orientation => :vertical).graph[:rankdir].to_s
end
-
+
# Diagram generation =======================================================
test "create should create output for domain with attributes" do
create_model "Foo", :bar => :references, :column => :string do
belongs_to :bar
end
@@ -83,11 +83,11 @@
test "create should create output for domain without attributes" do
create_simple_domain
Diagram::Graphviz.create
assert File.exists?("ERD.png")
end
-
+
test "create should write to file with dot extension if type is dot" do
create_simple_domain
Diagram::Graphviz.create :filetype => :dot
assert File.exists?("ERD.dot")
end
@@ -106,11 +106,11 @@
GraphViz.class_eval do
alias_method :output_and_errors_from_command, :old_output_and_errors_from_command
end
end
end
-
+
test "create should create output for domain with attributes if orientation is vertical" do
create_model "Foo", :bar => :references, :column => :string do
belongs_to :bar
end
create_model "Bar", :column => :string
@@ -136,21 +136,21 @@
rescue => e
message = e.message
end
assert_match /No entities found/, message
end
-
+
test "create should write to given file name plus extension if present" do
begin
create_simple_domain
Diagram::Graphviz.create :filename => "foobar"
assert File.exists?("foobar.png")
ensure
FileUtils.rm "foobar.png" rescue nil
end
end
-
+
test "create should abort and complain if output directory does not exist" do
message = nil
begin
create_simple_domain
Diagram::Graphviz.create :filename => "does_not_exist/foo"
@@ -158,16 +158,26 @@
message = e.message
end
assert_match /Output directory 'does_not_exist' does not exist/, message
end
+ test "create should not fail when reserved words are used as node names" do
+ create_model "Node", :name => :string
+ create_model "Edge", :node => :references do
+ belongs_to :node
+ end
+ assert_nothing_raised do
+ Diagram::Graphviz.create
+ end
+ end
+
# Graphviz output ==========================================================
test "generate should create directed graph" do
create_simple_domain
assert_equal "digraph", diagram.graph.type
end
-
+
test "generate should add title to graph" do
create_simple_domain
assert_equal '"Domain model\n\n"', diagram.graph.graph[:label].to_s
end
@@ -187,80 +197,120 @@
test "generate should omit title if set to false" do
create_simple_domain
assert_equal "", diagram(:title => false).graph.graph[:label].to_s
end
-
+
test "generate should create node for each entity" do
create_model "Foo", :bar => :references do
belongs_to :bar
end
create_model "Bar"
- assert_equal ["Bar", "Foo"], find_dot_nodes(diagram).map(&:id).sort
+ assert_equal ["m_Bar", "m_Foo"], find_dot_nodes(diagram).map(&:id).sort
end
-
- test "generate should add label for entities" do
+
+ test "generate should add html label for entities" do
+ RailsERD.options.markup = true
create_model "Foo", :bar => :references do
belongs_to :bar
end
create_model "Bar"
- assert_match %r{<\w+.*?>Bar</\w+>}, find_dot_node(diagram, "Bar")[:label].to_gv
+ assert_match %r{<\w+.*?>Bar</\w+>}, find_dot_node(diagram, "m_Bar")[:label].to_gv
end
- test "generate should add attributes to entity labels" do
+ test "generate should add record label for entities" do
+ RailsERD.options.markup = false
create_model "Foo", :bar => :references do
belongs_to :bar
end
+ create_model "Bar"
+ assert_equal %Q("Bar"), find_dot_node(diagram, "m_Bar")[:label].to_gv
+ end
+
+ test "generate should add attributes to entity html labels" do
+ RailsERD.options.markup = true
+ create_model "Foo", :bar => :references do
+ belongs_to :bar
+ end
create_model "Bar", :column => :string
- assert_match %r{<\w+.*?>column <\w+.*?>string</\w+.*?>}, find_dot_node(diagram, "Bar")[:label].to_gv
+ assert_match %r{<\w+.*?>column <\w+.*?>string</\w+.*?>}, find_dot_node(diagram, "m_Bar")[:label].to_gv
end
+ test "generate should add attributes to entity record labels" do
+ RailsERD.options.markup = false
+ create_model "Foo", :bar => :references do
+ belongs_to :bar
+ end
+ create_model "Bar", :column => :string, :column_two => :boolean
+ assert_equal %Q("Bar|column (string)\\ncolumn_two (boolean)\\n"), find_dot_node(diagram, "m_Bar")[:label].to_gv
+ end
+
test "generate should not add any attributes to entity labels if attributes is set to false" do
create_model "Jar", :contents => :string
create_model "Lid", :jar => :references do
belongs_to :jar
end
- assert_no_match %r{contents}, find_dot_node(diagram(:attributes => false), "Jar")[:label].to_gv
+ assert_no_match %r{contents}, find_dot_node(diagram(:attributes => false), "m_Jar")[:label].to_gv
end
- test "generate should create edge for each relationship" do
- create_model "Foo", :bar => :references do
- belongs_to :bar
+ test "node html labels should have direction reversing braces for vertical orientation" do
+ RailsERD.options.markup = true
+ create_model "Book", :author => :references do
+ belongs_to :author
end
- create_model "Bar", :foo => :references do
- belongs_to :foo
+ create_model "Author", :name => :string
+ assert_match %r(\A<\{\s*<.*\|.*>\s*\}>\Z)m, find_dot_node(diagram(:orientation => :vertical), "m_Author")[:label].to_gv
+ end
+
+ test "node html labels should not have direction reversing braces for horizontal orientation" do
+ RailsERD.options.markup = true
+ create_model "Book", :author => :references do
+ belongs_to :author
end
- assert_equal [["Bar", "Foo"], ["Foo", "Bar"]], find_dot_node_pairs(diagram).sort
+ create_model "Author", :name => :string
+ assert_match %r(\A<\s*<.*\|.*>\s*>\Z)m, find_dot_node(diagram(:orientation => :horizontal), "m_Author")[:label].to_gv
end
-
- test "node records should have direction reversing braces for vertical orientation" do
+
+ test "node record labels should have direction reversing braces for vertical orientation" do
+ RailsERD.options.markup = false
create_model "Book", :author => :references do
belongs_to :author
end
create_model "Author", :name => :string
- assert_match %r(\A<\{\s*<.*\|.*>\s*\}>\Z)m, find_dot_node(diagram(:orientation => :vertical), "Author")[:label].to_gv
+ assert_match %r(\A"\{\w+|.*\}"\Z)m, find_dot_node(diagram(:orientation => :vertical), "m_Author")[:label].to_gv
end
- test "node records should not have direction reversing braces for horizontal orientation" do
+ test "node record labels should not have direction reversing braces for horizontal orientation" do
+ RailsERD.options.markup = false
create_model "Book", :author => :references do
belongs_to :author
end
create_model "Author", :name => :string
- assert_match %r(\A<\s*<.*\|.*>\s*>\Z)m, find_dot_node(diagram(:orientation => :horizontal), "Author")[:label].to_gv
+ assert_match %r(\A"\w+|.*"\Z)m, find_dot_node(diagram(:orientation => :horizontal), "m_Author")[:label].to_gv
end
-
+
+ test "generate should create edge for each relationship" do
+ create_model "Foo", :bar => :references do
+ belongs_to :bar
+ end
+ create_model "Bar", :foo => :references do
+ belongs_to :foo
+ end
+ assert_equal [["m_Bar", "m_Foo"], ["m_Foo", "m_Bar"]], find_dot_node_pairs(diagram).sort
+ end
+
test "generate should create edge to generalized entity if polymorphism is true" do
create_model "Cannon", :defensible => :references do
belongs_to :defensible, :polymorphic => true
end
create_model "Stronghold" do
has_many :cannons, :as => :defensible
end
create_model "Galleon" do
has_many :cannons, :as => :defensible
end
- assert_equal [["Defensible", "Cannon"], ["Defensible", "Galleon"], ["Defensible", "Stronghold"]],
+ assert_equal [["m_Defensible", "m_Cannon"], ["m_Defensible", "m_Galleon"], ["m_Defensible", "m_Stronghold"]],
find_dot_node_pairs(diagram(:polymorphism => true)).sort
end
test "generate should create edge to each child of generalized entity if polymorphism is false" do
create_model "Cannon", :defensible => :references do
@@ -270,12 +320,12 @@
has_many :cannons, :as => :defensible
end
create_model "Galleon" do
has_many :cannons, :as => :defensible
end
- assert_equal [["Galleon", "Cannon"], ["Stronghold", "Cannon"]], find_dot_node_pairs(diagram).sort
+ assert_equal [["m_Galleon", "m_Cannon"], ["m_Stronghold", "m_Cannon"]], find_dot_node_pairs(diagram).sort
end
-
+
# Simple notation style ====================================================
test "generate should use no style for one to one cardinalities with simple notation" do
create_one_to_one_assoc_domain
assert_equal [["none", "none"]], find_dot_edge_styles(diagram(:notation => :simple))
end