1

This is my first time developing in Ruby on Rails, and I am using Neo4j for a database. I have installed a test version, created some models representing nodes, and written a basic test to make sure everything is working. The test is very simple: it fetches all users and prints their names.

When I connect to the test database directly and execute Cypher queries to insert data, the tests find the data and react to it correctly. However, when I specify data in the test/fixtures/users.yml file, it does not load. The test finds only the data inserted manually. Here is the yml file:

one:
  name: 'Dan'
two:
  name: 'Sam'

What am I doing wrong? Is there some setting I have to enable to load yml data into neo4j?

UPDATE: It appears that neo4j is not compatible with Rails fixtures, and for good reason: the language is incapable of describing relationships. I am in the process of developing new, expanded syntax and an implementation that will allow fixtures to be used to test Rails apps that use neo4j.

1 Answers1

0

Support for Rails fixtures hasn't been put into the neo4j gem. It is actually an ActiveRecord feature which we just haven't put in yet. To start with I would suggest checking out factory_girl:

https://github.com/thoughtbot/factory_girl

It provides a much more dynamic and easier to maintain system of defining factories and creating objects in your tests. Of course that means that your tests will be slower because they need to make the database calls for each test rather than setting up the objects at the beginning like fixtures do.

A lot of people get into a "one way must be better" mentality and say that you need to use either fixtures or factories. I think that there's actually room for both in a project. Fixtures are great especially for objects which you know are always going to be there. One of the biggest problems with them is that you need to refer back to your fixture files when you're working on your tests.

In your edit you mentioned that fixtures don't support relationships. That's not exactly true. ActiveRecord fixtures support defining properties in the YAML which specify values for associations. In ActiveRecord associations are managed via foreign keys/join tables, but in Neo4j they're just done through relationships. So I imagine the same structure would be fine. If you're looking to tackle this I would keep a couple of things in mind:

In ActiveRecord fixtures you can specify the foreign key column directly or you can specify the association via it's name and refer to it's identifier, like this:

### in pirates.yml

reginald:
  name: Reginald the Pirate
  monkey_id: 123
  # OR
  monkey: george

### in monkeys.yml

george:
  name: George the Monkey

With ActiveRecord fixtures, those identifiers are actually hashed to an integer and then that integer is always used for that record's ID in the database. This makes it easy to set the associations. Some similar workaround would probably need to be done for neo4j.rb. Note that ActiveNode models had a default uuid property so maybe that could be overridden, but you'd want to make sure you don't affect user's custom id_property definitions.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • I know that's rather a lot, so if you have any other questions feel free to ask in our Gitter channel: https://gitter.im/neo4jrb/neo4j – Brian Underwood Sep 13 '15 at 05:05