Skip to content

Commit

Permalink
All 158 RSpecs works.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasronge committed Sep 20, 2010
1 parent e3902a5 commit 924ca6c
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 199 deletions.
1 change: 0 additions & 1 deletion lib/neo4j/event_handler.rb
Expand Up @@ -72,7 +72,6 @@ def relationship_deleted(relationship)
end

def property_changed(node, key, old_value, new_value)
puts "property_changed #{node.neo_id} #{key}"
@listeners.each {|li| li.on_property_changed(node, key, old_value, new_value) if li.respond_to?(:on_property_changed)}
end

Expand Down
71 changes: 36 additions & 35 deletions lib/neo4j/index.rb
@@ -1,43 +1,32 @@
module Neo4j

module Index
def add_index(field, value=self[field], indexer = self.class.indexer)
indexer.add_index(wrapped_entity, field.to_s, value)
def add_index(field, value=self[field])
self.class.add_index(wrapped_entity, field.to_s, value)
end

def rm_index(field, value=self[field], indexer = self.class.indexer)
indexer.rm_index(wrapped_entity, field.to_s, value)
def rm_index(field, value=self[field])
self.class.rm_index(wrapped_entity, field.to_s, value)
end

module ClassMethods
def indexer=(indexer)
@indexer = indexer
end

def indexer
@indexer ||= Indexer.new(Neo4j::Node)
end

def index(field, conf = {})
indexer.index(field, conf[:type] || :exact)
end
extend Forwardable

def find(query, type = :exact)
indexer.find(query, type)
end

# clear the index of given type. if type == nil then clear all types of indexes
def clear_index(type = nil)
indexer.clear(type)
end
def_delegators :@indexer, :index, :find, :index?, :index_type?, :clear_index_type, :rm_index_type, :add_index, :rm_index

def unregister_index(type = nil)
indexer.unregister_index(type)
def indexer(clazz)
@@indexers ||= {}
if @@indexers.include?(clazz)
# we want to reuse an existing index
@indexer = @@indexers[clazz]
else
@indexer = Indexer.new(clazz)
@@indexers[clazz] = @indexer
end
end
end

class Indexer
DEFAULT_INDEX_NAME = 'Neo4j::Node' # if a node does not have a _classname property use this index
attr_reader :index_name

def initialize(clazz)
Expand All @@ -51,11 +40,20 @@ def initialize(clazz)
end

# add an index on a field that will be automatically updated by events.
def index(field, type)
def index(field, conf = {})
type = conf[:type] || :exact
@field_types[field.to_s] = type
Neo4j.default_db.event_handler.add(self)
end

def index?(field)
@field_types.include?(field.to_s)
end

def index_type?(type)
@field_types.values.include?(type)
end

def add_index(entity, field, value)
index_for_field(field.to_s).add(entity, field, value)
end
Expand All @@ -64,27 +62,30 @@ def rm_index(entity, field, value)
index_for_field(field).remove(entity, field, value)
end

def find(query, type)
def find(query, type = :exact)
index = index_for_type(type)
raise "no index #{@index_name} of type #{type} defined ('#{@indexes.inspect}')" if index.nil?
index.query(query)
end

# clears the index, if no type is provided clear all types of indexes
def clear(type)
def clear_index_type(type=nil)
if type
index_for_type(type).clear
raise "can't clear index of type '#{type}' since it does not exist ([#{@field_types.values.join(',')}] exists)" unless index_type?(type)
@indexes[type] && @indexes[type].clear
else
@indexes.each_value{|index| index.clear}
end
end

def unregister_index(type)
def rm_index_type(type=nil)
if type
@indexes.delete type
else
raise "can't remove index of type '#{type}' since it does not exist ([#{@field_types.values.join(',')}] exists)" unless index_type?(type)
@field_types.delete_if{|k,v| v == type}
@indexes[type] && @indexes[type].clear
@field_types.delete_if {|k,v| v == type}
else
@indexes.each_value{|index| index.clear}
@field_types.clear
end
end

Expand Down Expand Up @@ -123,7 +124,7 @@ def trigger?(classname)

def on_node_created(node)
return unless trigger?(node['_classname'])
@field_types.keys.each {|field| add_index(node, field, node[field])}
@field_types.keys.each {|field| add_index(node, field, node[field]) if node.property?(field)}
end

def on_node_deleted(node, old_props)
Expand Down
5 changes: 2 additions & 3 deletions lib/neo4j/mapping/node_mixin.rb
Expand Up @@ -58,11 +58,10 @@ class << self
c.extend ClassMethods::Aggregate
c.extend Neo4j::Index::ClassMethods
def c.inherited(subclass)
puts "new subclass #{subclass} !!!!!!!"
subclass.indexer = Neo4j::Index::Indexer.new(subclass)
subclass.indexer subclass
super
end
c.indexer = Neo4j::Index::Indexer.new(c)
c.indexer c

end

Expand Down
13 changes: 11 additions & 2 deletions lib/neo4j/node.rb
Expand Up @@ -24,17 +24,26 @@ def wrapped_entity
self
end

def self.indexer
Neo4j::Node.indexer
# Returns which ruby class wraps this node instance
# Returns the _classname property if
def wrapped_class
# TODO
end

def class
Neo4j::Node
end
end


class Node
extend Neo4j::Index::ClassMethods

self.indexer self

class << self


# Creates a new node using the default db instance when given no args
# Same as Neo4j::Node#create
def new(*args)
Expand Down
4 changes: 2 additions & 2 deletions spec/api/node_spec.rb
Expand Up @@ -30,7 +30,7 @@
graph_db.should_receive(:create_node).and_return(fixtures[:new_node])
db = double("Database")
db.should_receive(:graph).and_return(graph_db)
Neo4j.stub!(:db).and_return(db)
Neo4j.stub!(:started_db).and_return(db)
end
Return do
it ("a new node"){ should == fixtures[:new_node]}
Expand All @@ -46,7 +46,7 @@
graph_db.should_receive(:create_node).and_return(fixtures[:new_node])
db = double("Database")
db.should_receive(:graph).and_return(graph_db)
Neo4j.stub!(:db).and_return(db)
Neo4j.stub!(:started_db).and_return(db)
arg.hash = {:name => 'andreas', :colour => 'blue'}
end
Return do
Expand Down
2 changes: 0 additions & 2 deletions spec/fixture/employee.rb
@@ -1,4 +1,3 @@
puts "EMPLOYEE"
class Employee < Person
property :employee_id

Expand All @@ -9,4 +8,3 @@ def indexer
Neo4j::Index::Indexer.new(Person)
end
end
puts "EMPLOYEE DONE"
9 changes: 1 addition & 8 deletions spec/integration/aggregate_spec.rb
Expand Up @@ -10,7 +10,7 @@ class NewsStory
end


describe "Neo4j::Node#aggregate" do
describe "Neo4j::Node#aggregate", :type => :transactional do


before(:all) do
Expand All @@ -28,15 +28,8 @@ class NewsStory
new_tx
User.delete_aggregates
NewsStory.delete_aggregates
finish_tx
Neo4j.shutdown
rm_db_storage
end

before(:each) { new_tx }

after(:each) { finish_tx }

it "generate accessor methods for traversing the aggregate group" do
User.should respond_to(:all)
User.should respond_to(:old)
Expand Down
42 changes: 40 additions & 2 deletions spec/integration/index_spec.rb
Expand Up @@ -2,12 +2,33 @@


describe Neo4j::Node, "index", :type => :transactional do
class Vehicle
include Neo4j::NodeMixin
index :wheels
end

class Car < Vehicle
indexer Vehicle # use the same indexer as Vehicle, get index on wheels
index :brand
end

after(:each) do
# make sure we clean up after each test
Vehicle.clear_index_type
end

it "can index and search on two properties" do
c = Company.new(:name => 'jayway', :revenue => 1234)
new_tx
Company.find('name:"jayway" AND revenue: "1234"').should include(c)
end

it "can use the same index for a subclass" do
pending
volvo = Car.new(:brand => 'volvo', :wheels => 4)
new_tx
Car.find('brand: volvo').first.should == volvo
end
end

describe Neo4j::Node, "index", :type => :transactional do
Expand All @@ -17,8 +38,7 @@

after(:each) do
# make sure we clean up after each test
Neo4j::Node.clear_index
Neo4j::Node.unregister_index
Neo4j::Node.rm_index_type(:exact)
end

it "create index on a node" do
Expand All @@ -32,6 +52,24 @@
Neo4j::Node.find("name: andreas").get_single.should == new_node
end


it "#rm_index_type unregisters the index from the eventhandler and clear the index" do
new_node = Neo4j::Node.new :name => 'andreas'
new_node.add_index(:name)

# when
Neo4j::Node.rm_index_type(:exact)

# then
Neo4j::Node.find("name: andreas").first.should_not == new_node
Neo4j::Node.index_type?(:exact).should be_false
Neo4j::Node.index?(:name).should be_false

# clean up
Neo4j::Node.index(:name)
end


it "does not remove old index when a property is reindexed" do
new_node = Neo4j::Node.new
new_node[:name] = 'Kalle Kula'
Expand Down
16 changes: 8 additions & 8 deletions spec/integration/node_mixin_spec.rb
Expand Up @@ -8,13 +8,13 @@
end

after(:each) do
Employee.clear_index
Employee.unregister_index
Person.clear_index
Person.unregister_index
Employee.clear_index_type
Employee.rm_index_type
Person.clear_index_type
Person.rm_index_type
end

it "#new" do
it "#new creates node and set properties with given hash" do
empl = Employee.new(:name => 'andreas', :employee_id => 123)
empl[:name].should == 'andreas'
end
Expand Down Expand Up @@ -48,13 +48,13 @@
end

after(:each) do
SimpleNode.clear_index
SimpleNode.unregister_index # TODO
SimpleNode.clear_index_type
SimpleNode.rm_index_type # TODO
end



it "#new :name => 'foo' ..." do
it "#new :name => 'foo' initialize the node with given property hash" do
n = SimpleNode.new :name => 'foo', :bar => 'bar'
n.name.should == 'foo'
n[:bar].should == 'bar'
Expand Down
1 change: 1 addition & 0 deletions spec/integration/node_spec.rb
Expand Up @@ -34,6 +34,7 @@
new_node = Neo4j::Node.new
new_node.del
expect { new_node[:foo] = 'bar'}.to raise_error
expect { finish_tx }.to raise_error
end

it "update and then delete the same node in one transaction is okey" do
Expand Down
2 changes: 0 additions & 2 deletions spec/integration/transaction_spec.rb
Expand Up @@ -31,10 +31,8 @@
Neo4j::Node.new
end
id = a.neo_id
puts "delete id #{id}"
Neo4j::Transaction.run do
x = Neo4j::Node.load(id)
puts "got #{x} id:#{x.neo_id}"
x.del
end
end
Expand Down

0 comments on commit 924ca6c

Please sign in to comment.