Created
September 28, 2011 14:45
-
-
Save nmk/1248123 to your computer and use it in GitHub Desktop.
If a cyclic model is namespaced, root and parent relationships are lost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mongoid' | |
require 'minitest/autorun' | |
Mongoid.configure do |config| | |
config.master = Mongo::Connection.new.db('nodes_test') | |
end | |
module Tree | |
class Node | |
include Mongoid::Document | |
recursively_embeds_many | |
field :name | |
def is_root? | |
parent_node.nil? | |
end | |
end | |
end | |
class NodeTest < MiniTest::Unit::TestCase | |
include Tree | |
def setup | |
@root = Node.new(name: 'root') | |
@first_child = @root.child_nodes.build(name: 'first_child') | |
@second_child = @root.child_nodes.build(name: 'second_child') | |
@root.save! | |
end | |
def test_a_node_knows_if_it_is_a_root | |
assert @root.is_root? | |
end | |
def test_a_node_knows_if_it_not_a_root | |
refute @root.child_nodes.first.is_root? | |
end | |
def test_a_node_knows_its_parent | |
assert_equal @root, @root.child_nodes.first.parent_node | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mongoid' | |
require 'minitest/autorun' | |
Mongoid.configure do |config| | |
config.master = Mongo::Connection.new.db('nodes_test') | |
end | |
class Node | |
include Mongoid::Document | |
recursively_embeds_many | |
field :name | |
def is_root? | |
parent_node.nil? | |
end | |
end | |
class NodeTest < MiniTest::Unit::TestCase | |
def setup | |
@root = Node.new(name: 'root') | |
@first_child = @root.child_nodes.build(name: 'first_child') | |
@second_child = @root.child_nodes.build(name: 'second_child') | |
@root.save! | |
end | |
def test_a_node_knows_if_it_is_a_root | |
assert @root.is_root? | |
end | |
def test_a_node_knows_if_it_not_a_root | |
refute @root.child_nodes.first.is_root? | |
end | |
def test_a_node_knows_its_parent | |
assert_equal @root, @root.child_nodes.first.parent_node | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment