Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhelle committed Jul 18, 2023
2 parents b9cb0c6 + ffbbf53 commit 3f177a0
Show file tree
Hide file tree
Showing 41 changed files with 804 additions and 420 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ jobs:
with:
python-version: ${{ matrix.python_version }}

- name: Sleep for 60 seconds (Bash)
if: ${{ inputs.os != 'windows-latest' }}
run: sleep 60s
shell: bash

- name: Sleep for 60 seconds (Powershell)
if: ${{ inputs.os == 'windows-latest' }}
run: Start-Sleep -s 60
shell: powershell

- name: Install Requirements
run: |
pip install pytest
Expand Down
26 changes: 13 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import setuptools

with open('docs/pypi.md', 'r', encoding='utf-8') as fh:
with open("docs/pypi.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name='autofaker',
version='0.1.0',
url='https://github.com/christianhelle/autofaker',
license='MIT License',
name="autofaker",
version="0.1.0",
url="https://github.com/christianhelle/autofaker",
license="MIT License",
license_files=["LICENSE"],
author='Christian Helle',
author_email='christian.helle@outlook.com',
description='Python library designed to minimize the setup/arrange phase of your unit tests',
author="Christian Helle",
author_email="christian.helle@outlook.com",
description="Python library designed to minimize the setup/arrange phase of your unit tests",
long_description=long_description,
long_description_content_type='text/markdown',
package_dir={'': 'src'},
packages=setuptools.find_packages(where='src'),
python_requires='>=3.7',
install_requires=['pandas', 'faker', 'typing_inspect'],
long_description_content_type="text/markdown",
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
python_requires=">=3.7",
install_requires=["pandas", "faker", "typing_inspect"],
)
3 changes: 2 additions & 1 deletion src/autofaker/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ def __init__(self, instance):

def get_members(self):
return [
attr for attr in dir(self.instance)
attr
for attr in dir(self.instance)
if not callable(getattr(self.instance, attr)) and not attr.startswith("__")
]

Expand Down
7 changes: 5 additions & 2 deletions src/autofaker/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def __init__(self, t, rows: int = 3, use_fake_data: bool = False):

def generate(self):
members = [
attr for attr in dir(self.data[0])
if dataclasses.is_dataclass(getattr(self.data[0], attr)) or not callable(getattr(self.data[0], attr)) and not attr.startswith("__")
attr
for attr in dir(self.data[0])
if dataclasses.is_dataclass(getattr(self.data[0], attr))
or not callable(getattr(self.data[0], attr))
and not attr.startswith("__")
]
rows = []
for d in self.data:
Expand Down
29 changes: 15 additions & 14 deletions src/autofaker/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@


def is_date_type(type_name) -> bool:
return type_name in [
'datetime',
'date'
]
return type_name in ["datetime", "date"]


class DatetimeGenerator(TypeDataGeneratorBase):
def generate(self):
year = datetime.date.today().year
return datetime.datetime(random.randint(year - 10, year + 10),
random.randint(1, 12),
random.randint(1, 28),
random.randint(0, 23),
random.randint(0, 59),
random.randint(0, 59),
random.randint(0, 999))
return datetime.datetime(
random.randint(year - 10, year + 10),
random.randint(1, 12),
random.randint(1, 28),
random.randint(0, 23),
random.randint(0, 59),
random.randint(0, 59),
random.randint(0, 999),
)


class DateGenerator(TypeDataGeneratorBase):
def generate(self):
year = datetime.date.today().year
return datetime.datetime(random.randint(year - 10, year + 10),
random.randint(1, 12),
random.randint(1, 28))
return datetime.datetime(
random.randint(year - 10, year + 10),
random.randint(1, 12),
random.randint(1, 28),
)
56 changes: 43 additions & 13 deletions src/autofaker/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ def test_create_str_argument_using_decorator(self, text, number, decimal, boolea
def decorator(function):
def wrapper(*args):
if __get_class_that_defined_method(function) is None:
return function(*tuple(__create_function_args(function, *tuple(types), use_fake_data=use_fake_data)))
return function(__get_test_class(*args),
*tuple(__create_function_args(function, *tuple(types), use_fake_data=use_fake_data)))
return function(
*tuple(
__create_function_args(
function, *tuple(types), use_fake_data=use_fake_data
)
)
)
return function(
__get_test_class(*args),
*tuple(
__create_function_args(
function, *tuple(types), use_fake_data=use_fake_data
)
)
)

return wrapper

Expand Down Expand Up @@ -65,9 +77,19 @@ def test_create_fake_arguments(self, text: str, number: int, decimal: float, boo
def decorator(function):
def wrapper(*args):
if __get_class_that_defined_method(function) is None:
return function(*tuple(__create_function_args(function, *tuple(types), use_fake_data=True)))
return function(__get_test_class(*args),
*tuple(__create_function_args(function, *tuple(types), use_fake_data=True)))
return function(
*tuple(
__create_function_args(
function, *tuple(types), use_fake_data=True
)
)
)
return function(
__get_test_class(*args),
*tuple(
__create_function_args(function, *tuple(types), use_fake_data=True)
)
)

return wrapper

Expand All @@ -85,7 +107,9 @@ def autopandas(t: object, rows: int = 3, use_fake_data: bool = False):

def decorator(function):
def wrapper(*args):
pdf = PandasDataFrameGenerator(t, rows, use_fake_data=use_fake_data).generate()
pdf = PandasDataFrameGenerator(
t, rows, use_fake_data=use_fake_data
).generate()
if __get_class_that_defined_method(function) is None:
return function(pdf)
return function(__get_test_class(*args), pdf)
Expand All @@ -108,18 +132,22 @@ def fakepandas(t, rows: int = 3):
def __get_test_class(*args):
test_class = args[0]
if issubclass(test_class.__class__, unittest.TestCase) is False:
raise NotImplementedError("This way of creating anonymous objects are only supported from unit tests")
raise NotImplementedError(
"This way of creating anonymous objects are only supported from unit tests"
)
return test_class


def __get_class_that_defined_method(meth):
if inspect.isfunction(meth):
cls = getattr(inspect.getmodule(meth),
meth.__qualname__.split('.<locals>', 1)[0].rsplit('.', 1)[0],
None)
cls = getattr(
inspect.getmodule(meth),
meth.__qualname__.split(".<locals>", 1)[0].rsplit(".", 1)[0],
None,
)
if isinstance(cls, type):
return cls
return getattr(meth, '__objclass__', None) # handle special descriptor objects
return getattr(meth, "__objclass__", None) # handle special descriptor objects


def __create_function_args(function, *types, use_fake_data: bool = False) -> List:
Expand All @@ -133,5 +161,7 @@ def __create_function_args(function, *types, use_fake_data: bool = False) -> Lis
if __get_class_that_defined_method(function) is None:
pos = 0
if len(argtpes.args) - pos != len(values):
raise ValueError("Missing argument annotations. Please declare the type of every argument")
raise ValueError(
"Missing argument annotations. Please declare the type of every argument"
)
return values
3 changes: 2 additions & 1 deletion src/autofaker/enums.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import random
from enum import Enum

from autofaker.base import TypeDataGeneratorBase


def is_enum(t) -> bool:
return t.__base__.__name__ == 'Enum'
return isinstance(t, Enum.__class__)


class EnumGenerator(TypeDataGeneratorBase):
Expand Down
60 changes: 32 additions & 28 deletions src/autofaker/factory.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
from autofaker.builtins import (
IntegerGenerator,
FloatGenerator,
BooleanGenerator,
ByteArrayGenerator,
BytesGenerator,
ComplexGenerator,
FloatGenerator,
IntegerGenerator,
MemoryViewGenerator,
RangeGenerator,
BytesGenerator,
ByteArrayGenerator,
MemoryViewGenerator
)
from autofaker.fakes import FakeStringGenerator, StringGenerator, FakeIntegerGenerator
from autofaker.fakes import FakeIntegerGenerator, FakeStringGenerator, StringGenerator


class BuiltinTypeDataGeneratorFactory:
@staticmethod
def is_supported(type_name) -> bool:
return type_name in [
'int',
'str',
'float',
'complex',
'bool',
'range',
'bytes',
'bytearray',
'memoryview'
"int",
"str",
"float",
"complex",
"bool",
"range",
"bytes",
"bytearray",
"memoryview",
]

@staticmethod
def create(type_name, field_name: str = None, use_fake_data: bool = False):
if type_name == 'int':
return FakeIntegerGenerator() \
if field_name is not None and use_fake_data is True \
if type_name == "int":
return (
FakeIntegerGenerator()
if field_name is not None and use_fake_data is True
else IntegerGenerator()
if type_name == 'str':
return FakeStringGenerator(field_name) \
if field_name is not None and use_fake_data is True \
)
if type_name == "str":
return (
FakeStringGenerator(field_name)
if field_name is not None and use_fake_data is True
else StringGenerator()
if type_name == 'float':
)
if type_name == "float":
return FloatGenerator()
if type_name == 'complex':
if type_name == "complex":
return ComplexGenerator()
if type_name == 'bool':
if type_name == "bool":
return BooleanGenerator()
if type_name == 'range':
if type_name == "range":
return RangeGenerator()
if type_name == 'bytes':
if type_name == "bytes":
return BytesGenerator()
if type_name == 'bytearray':
if type_name == "bytearray":
return ByteArrayGenerator()
if type_name == 'memoryview':
if type_name == "memoryview":
return MemoryViewGenerator()
Loading

0 comments on commit 3f177a0

Please sign in to comment.