require 'spec_helper' require 'geoff/node_dsl' require 'geoff' describe NodeDsl do 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(: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 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 specify do node.to_geoff.should == expected_geoff 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,"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', binding: binding) do children do branch 'starbucks_branch_1', type: 'owns', some: 'property' do delay_time 15 hours ->{@hours} end end end node.to_s.should == expected_geoff end end describe 'with non-tree graphs' 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 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 specify do geoff = node.to_geoff geoff.should == expected_geoff end context "with relation type at the children level" 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 end