require File.expand_path('spec/spec_helper') require 'neo4j' require 'geoff' describe Geoff do class Branch; include Neo4j::NodeMixin; end class CoffeeMachine; include Neo4j::NodeMixin; end class Grinder; include Neo4j::NodeMixin; end class Table; include Neo4j::NodeMixin; end describe 'injecting builders' do context 'with object_id ignored' do before { stub_node_dsl_object_id } let(:builder) do Geoff( Branch, coffee_machines_builder, tables_builder ) do branch 'starbucks_luton_branch' do delay_time 15 children do b.small_coffee_machines type: 'uses', lease: '3 years' b.tables type: 'has' end end branch 'starbucks_reading_branch' do delay_time 10 children do b.large_coffee_machines type: 'uses', lease: '3 years' b.tables type: 'has' table 'octagonal_table', type: 'has' do capacity 8 end end end end end let(:coffee_machines_builder) do Geoff(CoffeeMachine) do b.large_coffee_machines = children do coffee_machine('large_illy' ) { power 2300 } coffee_machine('large_gaggia') { power 2600 } end b.small_coffee_machines = children do coffee_machine('small_illy' ) { power 600 } coffee_machine('small_gaggia') { power 750 } end end end let(:tables_builder) do Geoff(Table) do b.tables = children do table('round_table' ) { capacity 3 } table('square_table') { capacity 4 } end end end let(:expected_geoff) do strip_whitespace <<-EOS (ROOT)-[:Branch]->(Branch) (starbucks_luton_branch) {"_classname":"Branch","delay_time":15} (Branch)-[:all]->(starbucks_luton_branch) (small_illy) {"_classname":"CoffeeMachine","power":600} (CoffeeMachine)-[:all]->(small_illy) (starbucks_luton_branch)-[:uses]->(small_illy) {"lease":"3 years"} (small_gaggia) {"_classname":"CoffeeMachine","power":750} (CoffeeMachine)-[:all]->(small_gaggia) (starbucks_luton_branch)-[:uses]->(small_gaggia) {"lease":"3 years"} (round_table) {"_classname":"Table","capacity":3} (Table)-[:all]->(round_table) (starbucks_luton_branch)-[:has]->(round_table) (square_table) {"_classname":"Table","capacity":4} (Table)-[:all]->(square_table) (starbucks_luton_branch)-[:has]->(square_table) (starbucks_reading_branch) {"_classname":"Branch","delay_time":10} (Branch)-[:all]->(starbucks_reading_branch) (large_illy) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_illy) (starbucks_reading_branch)-[:uses]->(large_illy) {"lease":"3 years"} (large_gaggia) {"_classname":"CoffeeMachine","power":2600} (CoffeeMachine)-[:all]->(large_gaggia) (starbucks_reading_branch)-[:uses]->(large_gaggia) {"lease":"3 years"} (round_table) {"_classname":"Table","capacity":3} (Table)-[:all]->(round_table) (starbucks_reading_branch)-[:has]->(round_table) (square_table) {"_classname":"Table","capacity":4} (Table)-[:all]->(square_table) (starbucks_reading_branch)-[:has]->(square_table) (octagonal_table) {"_classname":"Table","capacity":8} (Table)-[:all]->(octagonal_table) (starbucks_reading_branch)-[:has]->(octagonal_table) EOS end specify do geoff = builder.to_geoff geoff.should == expected_geoff end end context 'with object_id sequence' do class NodeDsl def self.object_id node_dsl @object_ids ||= {} @object_id ||= 0 @object_id += 1 @object_ids[node_dsl] ||= @object_id end def object_id self.class.object_id self end end let(:builder) do Geoff( Branch, coffee_machines_builder ) do branch 'starbucks_luton_branch' do children do b.coffee_machine type: 'uses', lease: '3 years' end end end end let(:coffee_machines_builder) do Geoff(CoffeeMachine) do b.coffee_machine = coffee_machine('illy') do power 2300 children 'connected_to' do grinder 'fine_grinder' end end end end let(:expected_geoff) do strip_whitespace <<-EOS (ROOT)-[:Branch]->(Branch) (starbucks_luton_branch_1) {"_classname":"Branch"} (Branch)-[:all]->(starbucks_luton_branch_1) (illy_3) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(illy_3) (fine_grinder_5) {"_classname":"Grinder"} (Grinder)-[:all]->(fine_grinder_5) (illy_3)-[:connected_to]->(fine_grinder_5) (starbucks_luton_branch_1)-[:uses]->(illy_3) {"lease":"3 years"} EOS end specify do geoff = builder.to_geoff geoff.should == expected_geoff end end end describe 'cloning subtrees held in b variables' do before { stub_node_dsl_object_id } let(:builder) do Geoff(Branch) do b.grinder = grinder 'fine_grinder' branch 'starbucks_luton_branch' do children 'uses' do b.illy = coffee_machine('large_illy') do power 2300 children 'connected_to' do b.grinder clone: false end end # this branch uses two identical large illy machines b.illy type: 'uses', clone: true end end end end let(:expected_geoff) do strip_whitespace <<-EOS (ROOT)-[:Branch]->(Branch) (fine_grinder) {"_classname":"Grinder"} (Grinder)-[:all]->(fine_grinder) (starbucks_luton_branch) {"_classname":"Branch"} (Branch)-[:all]->(starbucks_luton_branch) (large_illy) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_illy) (large_illy)-[:connected_to]->(fine_grinder) (starbucks_luton_branch)-[:uses]->(large_illy) (large_illy) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_illy) (large_illy)-[:connected_to]->(fine_grinder) (starbucks_luton_branch)-[:uses]->(large_illy) EOS end specify do geoff = builder.to_geoff geoff.should == expected_geoff end end end