Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django- Factory boy failing for no apparent reason in just one factory

The code: class StockFactory(UniqueObjectsFactory): FACTORY_FOR = Stock FACTORY_DJANGO_GET_OR_CREATE = ('name', 'market')

market = factory.SubFactory(MarketFactory)
symbol = FuzzyAttribute(lambda: ''.join(random.choice(string.ascii_uppercase) for _ in xrange(4)))
name = FuzzyCompanyName()
# last_trade_price = fuzzy.FuzzyDecimal(0.0, 10000.0)


class PositionsFactory(FreezeableFactory):
    FACTORY_FOR = Position
    FACTORY_DJANGO_GET_OR_CREATE = ('stock','AnotherObject')

    id = FuzzyInteger(100000)
    stock = factory.SubFactory(Stock)

    AnotherObject = factory.SubFactory(AnotherObject)  #If I comment stock out it would fail here

    created_date = FuzzyDateTime(start_dt=datetime(2013, 1, 1, tzinfo=compat.UTC))

The error:

 File "/home/alon/Projects/stox-server/execution/tests/functional/test_positions.py", line 21, in setUp
    PositionsFactory.create( portfolio=self.portfolio)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 522, in create
    attrs = cls.attributes(create=True, extra=kwargs)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
    force_sequence=force_sequence,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 283, in build
    return stub.__fill__()
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 83, in __fill__
    res[attr] = getattr(self, attr)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
    containers=containers,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 75, in evaluate
    return self.function(obj)
  File "/home/alon/Projects/stox-server/execution/tests/common/factories.py", line 173, in <lambda>
    symbol = factory.LazyAttribute(lambda pos: pos.stock.symbol)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
    containers=containers,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 299, in evaluate
    return self.generate(sequence, obj, create, defaults)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 386, in generate
    return subfactory.simple_generate(create, **params)
AttributeError: type object 'Stock' has no attribute 'simple_generate'

Any clues? Ideas? I work with factory-boy quite a lot and most of the time it's an excellent tool. but after hours of debugging I just cant find the problem

like image 981
alonisser Avatar asked Mar 03 '14 08:03

alonisser


1 Answers

So stupid of me: Those line are wrong:

stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject) 

Should have been:

stock = factory.SubFactory(StockFactory)
AnotherObject = factory.SubFactory(AnotherObjectFactory) 

Hope it helps anyone else who bumps into this issue

like image 83
alonisser Avatar answered Oct 17 '22 01:10

alonisser