faketory 0.1.2
A very simple Factory library. Generic, but compatible with Django. Let's build together!
Requires Python: >=3.7,<4.0

# Faketory

A simple implementation of the Factory pattern, build any number of elements with random values generated by [Faker].


## Requirements

- [Python] >= 3.7
> IMPORTANT: This library depends on [Faker]. If by any chance you have any errors, make sure you have it installed. If you're using Python < 3.7, you can adapt this library by downgrading to a version of Faker that's compatible with it, you'll have to follow `installation for devs/contributors` and either build the library or just copy `faketory` folder to your Python project's root.


## Installation

```sh
pip install faketory
```

## Installation for devs/contributors

1. Clone this repository.
2. Run `./install.sh`.
> Note: You can build the library for distribution by simply running `poetry build`.

## Quick Start

1. Create `my_file.py`, then copy and paste this code:

```
from faketory.factory import Faketory
from faketory.gens import Fake


class MyModel:
    def __init__(self, age, name, email):
        self.age = age
        self.name = name
        self.email = email


class MyFactory(Faketory):
    age = Fake('pyint', min_value=18, max_value=120)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyModel

# () -> Builds an instance with autogenerated values
my_model = MyFactory()
print(f'My name is {my_model.name}\n')

# (age=20) -> Custom value
my_model_custom = MyFactory(age=20)
print(f'My age is {my_model_custom.age}\n')  # 20

# (_qty=9) -> Builds a list of 9 objects
my_models_list = MyFactory(_qty=9)
for index, my_mod in enumerate(my_models_list):
    print(f'{index + 1} {my_mod.email}')

```

2. Run the file using `python my_file.py`.


## Faketory

Builds 1 or more fake model instances of a Model class.
- `_qty`: Number of fake model instances to return. Minimum value is 1.
- `_resolver`: Function to resolve once the model instance is created. (Check Django ORM example below)
- `_type`: Define the type of the model instance you want to return. Default: 'list', but you can have 'generator' or 'set' instead.
- `**custom_fields`: These fields will override the Fake generated values of the Factory.


## Tutorial

1. Import Fake and Faketory:

```
from faketory.factory import Faketory
from faketory.gens import Fake
```

2. Import your Model(s):

```
from my_poject.models import MyModel
```

3. Create your Factory by passing Fake generators to each field you want to pass values in your Model constructor. Also add your Model to the model attribute of the Meta class:

```
class MyFactory(Faketory):
    my_field = Fake('name')
    my_other_field = Fake('text')

    class Meta:
        model = MyModel
```
> Note: You can pass any value to any field to your Model constructor instead of a Fake generator.

4. Create your single Model instance:

```
my_model_instance = MyFactory()
```

Now the attributes of `my_model_instance` will have randomly generated values by faker. For example, `my_field` could be 'Jon' and `my_other_field` could be 'Some text here'.

## Inheritance

```
class SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChild

```

This piece of code is equivalent to:

```
class BaseSampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')

    class Meta:
        model = SampleChild


class SampleFactory(BaseSampleFactory):
    name = Fake('name')

```


## FaketoryGen

A Faketory can have other Faketories... But if you pass a Faketory instance, it'll be treated as a fixed value. If you want to have different values, you'll need to use a FaketoryGen.

```
from faketory.factory import Faketory
from faketory.gens import FaketoryGen


class SampleChild(Sample):
    def __init__(self, age, email, name):
        self.age = age
        self.email = email
        self.name = name


class SampleHasChild:
    def __init__(self, child, email):
        self.child = child
        self.email = email


class SampleFactory(Faketory):
    age = Fake('pyint', min_value=18)
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = SampleChild


class SampleHasChildFactory(Faketory):
    child = FaketoryGen(SampleFactory)
    email = Fake('email')

    class Meta:
        model = SampleHasChild

```


## Django ORM example

You can pass any Django Model to `Faketory.Meta.model`, but if you want to save it to your database, make sure you pass `_resolver='save'` to your Faketory instance.

```
class SampleDjangoFactory(Faketory):
    email = Fake('email')
    name = Fake('name')

    class Meta:
        model = MyDjangoModel

my_django_instance = Factory(_resolver='save')

```

## Fake

This class simply calls the functions you get from Faker object, so you should check their docs. Here a quick example of how you can generate a fake name:

#### Faker

```
from faker import Faker


fake = Faker()

# Generate a name
first_name = fake.name()

```

#### Fake
```
from faketory.gens import Fake


first_name = Fake('name').generate()

```

#### Generate a random number

```
# Faker
fake.pyint(min_value=18, max_value=21)

# Fake
Fake('pyint', min_value=18, max_value=21).generate()
```

#### Generate unique random numbers

```
# Faker
numbers = set(
    fake.unique.random_int(min=1, max=10) for i in range(10)
)

# Fake
numbers = set(
    Fake('unique.random_int', min=1, max=10).generate() for i in range(10)
)
```


## Creator

Wαя€vเℓ - <jg@warevil.dev>

[Faker]: <https://pypi.org/project/Faker/>
[Python]: <https://www.python.org/downloads/>