require 'spec_helper' require 'geoff/node_dsl' require 'geoff' describe NodeDsl do before { stub_node_dsl_object_id } describe 'with properties and no children' do let(:node) do NodeDsl.new(node_name: 'starbucks_branch', klass_name: 'Branch') do delay_time 15 end end let(:expected_geoff) do strip_whitespace <<-EOS (starbucks_branch) {"_classname":"Branch","delay_time":15} (Branch)-[:all]->(starbucks_branch) EOS end specify do node.to_geoff.should == expected_geoff end end describe 'with children' do let(:expected_geoff) do strip_whitespace <<-EOS (starbucks) {"_classname":"Cafe"} (Cafe)-[:all]->(starbucks) (starbucks_branch_1) {"_classname":"Branch","closing_buffer":30,"delay_time":15,"postcode":"LU1 1CN","schedule":"DEFAULT_SCHEDULE"} (Branch)-[:all]->(starbucks_branch_1) (starbucks)-[:owns]->(starbucks_branch_1) {"some":"property"} EOS end context 'having properties defined at node level' do let(:node) do NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe') do children do branch 'starbucks_branch_1', type: 'owns', some: 'property' do closing_buffer 30 delay_time 15 postcode 'LU1 1CN' schedule 'DEFAULT_SCHEDULE' end end end end specify do node.to_geoff.should == expected_geoff end end context 'having properties defined at children level' do let(:node) do NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe') do children 'owns', some: 'property' do branch 'starbucks_branch_1' do closing_buffer 30 delay_time 15 postcode 'LU1 1CN' schedule 'DEFAULT_SCHEDULE' end end end end specify do pending 'properties at children level get ignored' node.to_geoff.should == expected_geoff end end end describe "#with a lambda to access the outer scope" do let(:expected_geoff) do strip_whitespace <<-EOS (starbucks) {"_classname":"Cafe"} (Cafe)-[:all]->(starbucks) (starbucks_branch_1) {"_classname":"Branch","delay_time":15,"opening_hours":3} (Branch)-[:all]->(starbucks_branch_1) (starbucks)-[:owns]->(starbucks_branch_1) {"some":"property"} EOS end specify do @hours = 3 node = NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe', target: self) do children do branch 'starbucks_branch_1', type: 'owns', some: 'property' do delay_time 15 opening_hours ->{@hours} end end end node.to_s.should == expected_geoff end end describe 'with non-tree graphs' do let(:expected_geoff) do strip_whitespace <<-EOS (starbucks) {"_classname":"Cafe"} (Cafe)-[:all]->(starbucks) (luton) {"_classname":"Area","name":"LU1"} (Area)-[:all]->(luton) (starbucks)-[:headquarters_location]->(luton) (starbucks_branch_1) {"_classname":"Branch","delay_time":15} (Branch)-[:all]->(starbucks_branch_1) (starbucks_branch_1)-[:location]->(luton) (starbucks)-[:owns]->(starbucks_branch_1) {"some":"property"} EOS end context 'and relationship type at node level' do let(:node) do NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe') do children do b.luton = area 'luton', type: 'headquarters_location' do name 'LU1' end branch 'starbucks_branch_1', type: 'owns', some: 'property' do delay_time 15 children do b.luton type: 'location' end end end end end specify do geoff = node.to_geoff geoff.should == expected_geoff end end end context "with relation type at the children level" do context 'and tree graph' do let(:expected_geoff) do strip_whitespace <<-EOS (starbucks) {"_classname":"Cafe"} (Cafe)-[:all]->(starbucks) (luton) {"_classname":"Area","name":"LU1"} (Area)-[:all]->(luton) (starbucks)-[:location]->(luton) EOS end specify do node = NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe') do children "location" do area 'luton' do name 'LU1' end end end node.to_geoff.should == expected_geoff end end end # Cloning nodes is needed for injecting same builder multiple times. describe '#clone' do let(:node) do NodeDsl.new(node_name: 'starbucks', klass_name: 'Cafe') do children do branch 'starbucks_branch_1', type: 'owns', some: 'property' do closing_buffer 30 delay_time 15 postcode 'LU1 1CN' schedule 'DEFAULT_SCHEDULE' end end end end specify do geoff = node.to_geoff geoff_of_clone = node.clone.to_geoff geoff.should == geoff_of_clone end specify do node.should_not == node.clone end end end