Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.3 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Libraries

    PyFactory 0.4.0

    Download button

    No screenshots available
    Downloads: 124  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Mitchell Hashimoto | More programs
    MIT/X Consortium Lic... / FREE
    September 1st, 2011, 18:44 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    PyFactory description

    Generic model factory framework

    PyFactory is a Python library for writing generic model factories useful for unit testing.

    Example

    Basic Example

    The example below shows a very simple example. Say somewhere in our tests we need various instances of User objects. In our tests we would simply write the following to create a user:

    user = UserFactory().create("basic")

    The PyFactory code necessary to make this happen is shown below. Note that the ModelBuilder created would only have to be done once.

    from pyfactory import Factory, schema

    import models

    class ModelBuilder(object):
     """
     The model builder is responsible for knowing how to build
     and create models based on their attributes. This is what
     allows PyFactory to be completely model-agnostic.
     """

     @classmethod
     def build(cls, model_cls, attrs):
     return model_cls(attributes)

     @classmethod
     def create(cls, model_cls, attrs):
     result = cls.build(model_cls, attrs)
     result.save()
     return result

    class UserFactory(Factory):
     """
     This shows a simple factory which creates a type of User.
     """


     _model = models.user.User
     _model_builder = ModelBuilder

     @schema()
     def basic(self):
     return {
     "first_name": "Test",
     "last_name": "User",
     }

    Associations

    In any application, models typically have associations. Let's look at the case where we have a Post model which is written by a User. If we want a valid Post object in our tests, once again we only need to do the following:

    post = PostFactory().create("basic")

    And the PyFactory factories to make this possible are equally simple:

    class PostFactory(Factory):
     @schema()
     def basic(self):
     return {
     "title": "Fake Title",
     "body": "Lorem ipsum...",
     "author_id": association(UserFactory(), "basic", "id")
     }


    Attribute Overrides

    Given the above examples, what if you already had a User that you wanted as a post author? Well, its simple to override attributes by just passing the override attributes as additional keyword arguments to the factory method:

    author = # Pretend we got an author somewhere
    post = PostFactory().create("basic", author_id=author.id)



    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    model factories | unit testing | model | factory | framework

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM