<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2016 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.
import string
from datetime import datetime

from factory import SubFactory, LazyAttribute
from factory.alchemy import SQLAlchemyModelFactory as Factory
from factory.faker import Faker
from factory.fuzzy import FuzzyChoice, FuzzyDateTime, FuzzyInteger, FuzzyText

from pycroft.model.facilities import Site, Building, Room
from pycroft.model.finance import Account
from pycroft.model.session import session
from pycroft.model.user import User
from tests.factories.dummy.utc import utc


class BaseFactory(Factory):
    class Meta:
        sqlalchemy_session = session


class SiteFactory(BaseFactory):
    class Meta:
        model = Site

    name = FuzzyText()


class BuildingFactory(BaseFactory):
    class Meta:
        model = Building

    site = SubFactory(SiteFactory)
    site_id = LazyAttribute(lambda self: self.site.id)

    number = FuzzyText(length=3)
    short_name = FuzzyText(length=8)
    street = FuzzyText(length=20)


class RoomFactory(BaseFactory):
    class Meta:
        model = Room

    number = FuzzyText(length=36)
    level = FuzzyInteger(0)
    inhabitable = FuzzyChoice([True, False])

    # many to one from Room to Building
    building = SubFactory(BuildingFactory)
    building_id = LazyAttribute(lambda self: self.building.id)


class AccountFactory(BaseFactory):
    class Meta:
        model = Account

    name = FuzzyText(length=127)
    type = FuzzyChoice([
        "ASSET",       # Aktivkonto
        "USER_ASSET",  # Aktivkonto for users
        "BANK_ASSET",  # Aktivkonto for bank accounts
        "LIABILITY",   # Passivkonto
        "EXPENSE",     # Aufwandskonto
        "REVENUE",     # Ertragskonto
    ])


class UserFactory(BaseFactory):
    class Meta:
        model = User

    login = FuzzyText(length=22, chars=string.ascii_lowercase)
    name = FuzzyText(length=255)
    registered_at = FuzzyDateTime(datetime(2001, 9, 11, tzinfo=utc))
    passwd_hash = FuzzyText()
    email = Faker('email')
    # one to one from User to Account
    account = SubFactory(AccountFactory)
    account_id = LazyAttribute(lambda self: self.account.id)

    # many to one from User to Room
    room = SubFactory(RoomFactory)
    room_id = LazyAttribute(lambda self: self.room.id)
    # TODO: create subclasses for `dummy` and `privileged` user<commit_msg>Use Faker instead of deprecated Fuzzy attributes<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2016 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.
from factory import SubFactory, LazyAttribute
from factory.alchemy import SQLAlchemyModelFactory as Factory
from factory.faker import Faker

from pycroft.model.facilities import Site, Building, Room
from pycroft.model.finance import Account
from pycroft.model.session import session
from pycroft.model.user import User


class BaseFactory(Factory):
    class Meta:
        sqlalchemy_session = session


class SiteFactory(BaseFactory):
    class Meta:
        model = Site

    name = Faker('street_name')


class BuildingFactory(BaseFactory):
    class Meta:
        model = Building

    site = SubFactory(SiteFactory)

    number = Faker('building_number')
    street = LazyAttribute(lambda b: b.site.name)
    short_name = LazyAttribute(lambda b: "{}{}".format(b.street[:3], b.number))


class RoomFactory(BaseFactory):
    class Meta:
        model = Room

    number = Faker('numerify', text='## #')
    level = Faker('random_int', min=0, max=16)
    inhabitable = Faker('boolean')

    building = SubFactory(BuildingFactory)


class AccountFactory(BaseFactory):
    class Meta:
        model = Account

    name = Faker('word')
    type = Faker('random_element', elements=(
        "ASSET",       # Aktivkonto
        "USER_ASSET",  # Aktivkonto for users
        "BANK_ASSET",  # Aktivkonto for bank accounts
        "LIABILITY",   # Passivkonto
        "EXPENSE",     # Aufwandskonto
        "REVENUE",     # Ertragskonto
    ))


class UserFactory(BaseFactory):
    class Meta:
        model = User

    login = Faker('user_name')
    name = Faker('name')
    registered_at = Faker('date_time')
    password = Faker('password')
    email = Faker('email')
    account = SubFactory(AccountFactory, type="USER_ASSET")
    room = SubFactory(RoomFactory)
    # TODO: create subclasses for `dummy` and `privileged` user
{'fraction_non_alphanumeric': 0.0899914089347079, 'fraction_numerical': 0.00859106529209622, 'mean_word_length': 3.410037878787879, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '751300', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1357, 'n_words': 410}