def create_random_user(): username = internet.user_name() email = internet.email_address() signup_date = date.date(past=True) password = lorem_ipsum.word() return User(username=username, email=email, signup_date=signup_date, password=password)
def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed, randint from forgery_py import date users = User.query.all() # The method seed() sets the integer starting value used in generating # random numbers. Call this function before calling any other random # module function. seed() for i in range(count): int_follower = randint(0, len(users) - 1) int_followed = randint(0, len(users) - 1) if int_follower != int_followed: f = Follow( follower=users[int_follower], followed=users[int_followed], timestamp=date.date(past=True) ) db.session.add(f) # relationship might not be random, in which case rollback try: db.session.commit() except IntegrityError: db.session.rollback()
def generate_comment(func_author, func_post): return Comment( body=lorem_ipsum.paragraphs(), created=date.date(past=True), authors=func_author(), post=func_post() )
def generate_post(func_author): return Post( title=lorem_ipsum.title(), body=lorem_ipsum.paragraphs(), created=date.date(), author=func_author(), )
def gene_users(count=100): from random import seed, randint from sqlalchemy.exc import IntegrityError gender = ('男', '女', '不告诉你') seed() for i in range(count): fake_user = User() fake_user.username = internet.user_name(True) fake_user.email = fake_user.username + '@test.com' fake_user.password = '******' fake_user.name = name.full_name() fake_user.about_me = lorem_ipsum.sentence()[0:99] fake_user.timestamp = date.date(past=True) fake_user.birthday = date.date(past=True) fake_user.gender = gender[randint(0, 2)] fake_user.phone = address.phone() fake_user.address = address.street_address() fake_user.id_number = ''.join([str(randint(0, 9)) for i in range(18)]) db.session.add(fake_user) try: db.session.commit() except IntegrityError: db.session.rollback()
def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed from forgery_py import name, date, internet, address, personal seed() for i in range(count): p = Person(name.first_name(), name.last_name(), date.date(), personal.gender(), address.phone(), internet.email_address(), address.street_address(), address.city(), address.state_abbrev(), address.zip_code()) db.session.add(p) try: db.session.commit() except IntegrityError: db.session.rollback()
def gene_teams(count=100): from random import seed, randint, sample from sqlalchemy.exc import IntegrityError seed() user_count = User.query.count() types = OutdoorType.show_list() for i in range(count): fake_team = Team() fake_team.name = name.company_name() + '的户外团队' leader = User.query.offset(randint(0, count - 1)).first() fake_team.created_by = leader.id fake_team.leader_id = leader.id fake_team.description = lorem_ipsum.sentence()[:99] fake_team.timestamp = date.date(past=True) fake_team.types = sample(types, randint(1, 7)) db.session.add(fake_team) try: db.session.commit() except IntegrityError: db.session.rollback()
def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed, randint from forgery_py import date users = User.query.all() # The method seed() sets the integer starting value used in generating # random numbers. Call this function before calling any other random # module function. seed() for i in range(count): int_follower = randint(0, len(users) - 1) int_followed = randint(0, len(users) - 1) if int_follower != int_followed: f = Follow(follower=users[int_follower], followed=users[int_followed], timestamp=date.date(past=True)) db.session.add(f) # relationship might not be random, in which case rollback try: db.session.commit() except IntegrityError: db.session.rollback()
def generate_article(func_author): return Article(title=lorem_ipsum.title(), body=lorem_ipsum.paragraphs(), created_time=date.date(past=True), author=func_author())
def generate_comment(func_author, func_article): return Comment(body=lorem_ipsum.paragraphs(), created_time=date.date(past=True), author=func_author(), article=func_article())
from os import path, mkdir from helper import test_app_path, clean_pages from forgery_py import lorem_ipsum as lorem, date import random _title = lambda: 'title: "%s"' % lorem.words(random.randint(2, 5)) _file_name = lambda: '%s_%s' % (lorem.word(), lorem.word()) _summary = lambda: 'summary: "%s"' % lorem.sentence() _body = lambda: lorem.paragraphs(random.randint(5, 20)) _tags = lambda: 'tags: "%s"' % ','.join(lorem.words(random.randint(2, 5), as_list=True)) _pub = lambda: 'date: %s 00:00:00' % date.date(past=True) class DataMocks(object): def page(self, file_name): return self.write_page(file_name, [_title(), _summary(), '', _body()]) def post(self, file_name): return self.write_page(file_name, [_title(), _summary(), _tags(), _pub(), '', _body()], root='pages/posts') def write_page(self, file_name, lines, root='pages'): page_folder = path.join(test_app_path, root, file_name) if not path.exists(page_folder): mkdir(page_folder) with open(path.join(test_app_path, root, file_name + '.md'), 'w') as file: file.write('\n'.join(lines)) return self def index(self):
def generate_post(func_author): return Post(title=lorem_ipsum.title(), body=lorem_ipsum.paragraphs(), created=date.date(), author=func_author())
def generate_comment(func_author, func_post): return Comment(body=lorem_ipsum.paragraphs(), created=date.date(past=True), author=func_author(), post=func_post())
def generate_todo(func_boolean, func_author): return Todo(content=lorem_ipsum.sentences(), created=date.date(past=True), isdone=func_boolean(), author=func_author())