Code Monkey home page Code Monkey logo

Comments (8)

stevelacey avatar stevelacey commented on May 30, 2024

@olegpidsadnyi any thoughts on this? A sensible defacto way of retrieving build/unsaved models would replace a bunch of stuff in the suite I'm currently working on – and I'd rather not write a fixture for each model – is there a way to do this already or would adding a pre/suffixed fixture name be the way to go here?

from pytest-factoryboy.

stevelacey avatar stevelacey commented on May 30, 2024

A few further thoughts

  • A decorator would be passable... but I'd rather make the default not saving than making people go to more /effort/ to write db-free tests
  • Automatically toggling this based on whether the db fixture (or /a/ db fixture) is included would be neat but kind of crazy, and I have no idea how you'd do that... that'd negate the need for pre/suffixing though which would be cool
  • An alternative to the above could be a nodb fixture or something but again crazy no idea how

A suffix or a decorator to adjust how the factory works for the test in question seems like the way to go but I am wondering if we can do better

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 30, 2024

@stevelacey it is more like what kind of base factory are you using, right? What kind of ORM is it in your case. I guess there's a difference between build and create, but if you have a non-db model, what kind of difference does it make?

Could you please describe your setup?

from pytest-factoryboy.

stevelacey avatar stevelacey commented on May 30, 2024

from pytest-factoryboy.

stevelacey avatar stevelacey commented on May 30, 2024

@olegpidsadnyi ☝️

from pytest-factoryboy.

witold-gren avatar witold-gren commented on May 30, 2024

This is very good idea. I try another case, byt this is not work. Normally in FactoryBoy we have strategy in class Meta. If I set BUILD_STRATEGY and usage fixture_factory and call to them, this fixture connect with my db. This is not intuitive..

register(MyModel)

def test_something(my_model_factory):
    my_model_factory()   # <- this call db
    my_model_factory.build().   # <- this not call db

from pytest-factoryboy.

stevelacey avatar stevelacey commented on May 30, 2024

from pytest-factoryboy.

timdiels avatar timdiels commented on May 30, 2024

Wouldn't the following work? Generate these fixtures:

@pytest.fixture
def derive_factory_strategy():  # or call it factory_strategy_strategy ;)
    def derive(factory_class):
        return factory_class._meta.strategy
    return derive

@pytest.fixture
def {factory_name}__strategy(derive_factory_strategy):
    strategy = derive_factory_strategy(factory_class)
    ...

@pytest.yield_fixture
def {factory_name}({factory_name}__strategy):
    # - Setting TheFactory._meta.strategy is basically all use_strategy does.
    #   I'm not using it as I have to get the _meta.strategy so that
    #   I can reset it later and there's no public function for that that I know of.
    # - You could also use the pytest builtin monkeypatch.setattr to temporarily
    #    change the strategy.
    #   (It's necessary to change it back as the factory is the actual factory
    #   class, which isn't isolated across tests)
    default_strategy = foo_factory._meta.strategy
    foo_factory._meta.strategy = {factory_name}__strategy
    yield foo_factory
    # yield_fixture catches exceptions for us, no need for try: finally:
    foo_factory._meta.strategy = default_strategy

The user can now globally override the desired strategy, e.g. to derive from presence of a db fixture the user could write:

@pytest.fixture
def derive_factory_strategy(request):
    def derive(factory_class):
        if 'db' in request.fixturenames:
            return factory.CREATE_STRATEGY
        else:
            return factory.BUILD_STRATEGY

If a user wants to override a single FooFactory's strategy, they can override the foo_factory__strategy fixture.

from pytest-factoryboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.