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 'using direction macros' do before { stub_node_dsl_object_id } let(:builder) do Geoff(Branch) do branch 'banana' do incoming :inverts do branch 'egg' end outgoing :reverts do branch 'reverted_banana' end end end end let(:expected_geoff) do strip_whitespace <<-EOS (ROOT)-[:Branch]->(Branch) (banana) {"_classname":"Branch"} (Branch)-[:all]->(banana) (egg) {"_classname":"Branch"} (Branch)-[:all]->(egg) (banana)<-[:inverts]-(egg) (reverted_banana) {"_classname":"Branch"} (Branch)-[:all]->(reverted_banana) (banana)-[:reverts]->(reverted_banana) EOS end specify do geoff = builder.to_geoff geoff.should == expected_geoff end end context 'with object_id ignored' do before { stub_node_dsl_object_id } let(:builder) do Geoff( Branch, coffee_machines_builder, tables_builder ) do branch 'acme_coffee_luton_branch' do number_of_employees 15 children do b.small_coffee_machines type: 'uses', lease: '3 years' b.tables type: 'has', clone: true end end branch 'acme_coffee_reading_branch' do number_of_employees 10 children do b.large_coffee_machines type: 'uses', lease: '3 years' b.tables type: 'has', clone: true 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_machine') { power 2300 } coffee_machine('xxl_machine' ) { power 2600 } end b.small_coffee_machines = children do coffee_machine('xs_machine' ) { power 600 } coffee_machine('small_machine') { 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) (acme_coffee_luton_branch) {"_classname":"Branch","number_of_employees":15} (Branch)-[:all]->(acme_coffee_luton_branch) (xs_machine) {"_classname":"CoffeeMachine","power":600} (CoffeeMachine)-[:all]->(xs_machine) (acme_coffee_luton_branch)-[:uses]->(xs_machine) {"lease":"3 years"} (small_machine) {"_classname":"CoffeeMachine","power":750} (CoffeeMachine)-[:all]->(small_machine) (acme_coffee_luton_branch)-[:uses]->(small_machine) {"lease":"3 years"} (round_table) {"_classname":"Table","capacity":3} (Table)-[:all]->(round_table) (acme_coffee_luton_branch)-[:has]->(round_table) (square_table) {"_classname":"Table","capacity":4} (Table)-[:all]->(square_table) (acme_coffee_luton_branch)-[:has]->(square_table) (acme_coffee_reading_branch) {"_classname":"Branch","number_of_employees":10} (Branch)-[:all]->(acme_coffee_reading_branch) (large_machine) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_machine) (acme_coffee_reading_branch)-[:uses]->(large_machine) {"lease":"3 years"} (xxl_machine) {"_classname":"CoffeeMachine","power":2600} (CoffeeMachine)-[:all]->(xxl_machine) (acme_coffee_reading_branch)-[:uses]->(xxl_machine) {"lease":"3 years"} (round_table) {"_classname":"Table","capacity":3} (Table)-[:all]->(round_table) (acme_coffee_reading_branch)-[:has]->(round_table) (square_table) {"_classname":"Table","capacity":4} (Table)-[:all]->(square_table) (acme_coffee_reading_branch)-[:has]->(square_table) (octagonal_table) {"_classname":"Table","capacity":8} (Table)-[:all]->(octagonal_table) (acme_coffee_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 'acme_coffee_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('machine') do power 2300 children 'connected_to' do grinder 'fine_grinder' end end end end let(:expected_geoff) do strip_whitespace <<-EOS (ROOT)-[:Branch]->(Branch) (acme_coffee_luton_branch_1) {"_classname":"Branch"} (Branch)-[:all]->(acme_coffee_luton_branch_1) (machine_3) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(machine_3) (fine_grinder_5) {"_classname":"Grinder"} (Grinder)-[:all]->(fine_grinder_5) (machine_3)-[:connected_to]->(fine_grinder_5) (acme_coffee_luton_branch_1)-[:uses]->(machine_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 'acme_coffee_luton_branch' do children 'uses' do b.machine = coffee_machine('large_machine') do power 2300 children 'connected_to' do b.grinder clone: false end end # this branch uses two identical large machine machines # but sharing same grinder b.machine 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) (acme_coffee_luton_branch) {"_classname":"Branch"} (Branch)-[:all]->(acme_coffee_luton_branch) (large_machine) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_machine) (large_machine)-[:connected_to]->(fine_grinder) (acme_coffee_luton_branch)-[:uses]->(large_machine) (large_machine) {"_classname":"CoffeeMachine","power":2300} (CoffeeMachine)-[:all]->(large_machine) (large_machine)-[:connected_to]->(fine_grinder) (acme_coffee_luton_branch)-[:uses]->(large_machine) EOS end specify do geoff = builder.to_geoff geoff.should == expected_geoff end end end