versolearning / meteor-factory

Meteor package for creating test data or generating fixtures
https://atmospherejs.com/dburles/factory
MIT License
107 stars 23 forks source link

Hard to deal with repeated objects #8

Open mlanett opened 9 years ago

mlanett commented 9 years ago

I'm having some problems with objects when the relations form a graph.

Consider this:

Factory.define "author", Author, { name: "F. Scott Fitzgerald" }
Factory.define "book", Book, { title: "The Great Gatsby": author: Factory.get("author") }
Factory.define "contract", Contract, { author: Factory.get("author"), book: Factory.get("book") }

When we Factory.create("contract"), we fail because there will be two inserts of author; one by contract and one when it creates the relation for book. We should only have one author object and the same id in both places. This can be fixed by after() hooks but it's a mess.

But, for this following case we might expect that a relation is different each time.

Factory.define "person", Person, { name: Random.string }
Factory.define "dance", Pair, { leader: Factory.get("person"), follower: Factory.get("person") }

It's easy to fix the first case by putting a name:object cache in Factory.build and using it to ensure that relations created unique objects. But that would break the latter case. Is the latter case a reasonable expectation? If not then I can probably build the cache and make a PR. If so then we need to figure out the best way to accommodate it.

mlanett commented 9 years ago

Also one may have a graph after calling multiple create methods, so exposing the cache optionally may also make sense.

dburles commented 9 years ago

Hey @mlanett seems like a valid point, I wonder if such a feature has been implemented in similar packages?