Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 495 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isn't this alternative to the deprecated Factory.set_creation_function working with nosetests?

#1
Factory Boy deprecated set_creation_function (see [ChangeLog 2.6.1][1]) and recommends that developers

> Replace factory.set_creation_function(SomeFactory, creation_function)
> with an override of the _create() method of SomeFactory

I have i) a number of derivative factory classes and ii) my db session instantiated in another module so I tried replacing the working example from

[To see links please register here]

with the second code block below. PyCharm is warning me that the "db" import is not being used, so I suspect it might not be being dereferenced properly when I set [sqlalchemy_session][2]?

### Working with nosetests 1.3.7 (but FactoryBoy's set_creation_function is now deprecated):
from myapp.core import db

def create_sqlalchemy_model_function(class_to_create, *args, **kwargs):
entity = class_to_create(**kwargs)
db.session.add(entity)
db.session.commit()
return entity

Factory.set_creation_function(create_sqlalchemy_model_function)


### Not working with nosetests 2.x (looking like db is not being referenced properly?)
from factory.alchemy import SQLAlchemyModelFactory as Factory
from myapp.core import db

class Factory():
class Meta:
sqlalchemy_session = db.session

def _create(cls, model_class, *args, **kwargs):
entity = model_class(*args, **kwargs)
db.session.add(entity)
db.session.commit()
return entity


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#2
Two major issues with your sample not-working code:

* the class should be derived from `SQLAlchemyModelFactory` class
* the `_create()` method should be defined as `classmethod`

Fixed version:

from factory.alchemy import SQLAlchemyModelFactory as Factory
from myapp.core import db

class MyFactory(Factory):
class Meta:
sqlalchemy_session = db.session

@classmethod
def _create(cls, model_class, *args, **kwargs):
entity = model_class(*args, **kwargs)
db.session.add(entity)
db.session.commit()
return entity

Here is also a [sample][1] of the model factory overriding the `_create()` method.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through