adsabs / biblib-service

ADS library service
https://ui.adsabs.harvard.edu
MIT License
4 stars 8 forks source link

Stub data factory #15

Closed jonnybazookatone closed 9 years ago

jonnybazookatone commented 9 years ago

Can use factories to generate stub data for tests using: https://factoryboy.readthedocs.org/en/latest/index.html

Can get predefined faked stub data using: https://github.com/joke2k/faker

Example usage for this library

import factory
from faker import Faker
from models import User, Library 

faker = Faker()

def fake_biblist():
    return ['2015MNRAS...28....E']

class UserFactory(factory.Factory):
    class Meta:
        model = User

    id = factory.Sequence(lambda n: n)
    absolute_uid = factory.LazyAttribute(lambda x: faker.random_int())

class LibraryFactory(factory.Factory):
    class Meta:
        model = Library
    name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3))
    description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=5))
    public = factory.LazyAttribute(lambda x: faker.boolean())
    bibcode = factory.LazyAttribute(lambda x: fake_biblist())

if __name__ == '__main__':

    user = UserFactory.stub()
    print(user.id, user.absolute_uid)

    user = UserFactory.stub()
    print(user.id, user.absolute_uid)

    library = LibraryFactory.stub()
    print(library.name, library.description, library.public)