def create_and_populate_household(user_count=2): household = Household.create(name=fake.street_name()) print(f"Created household '{household.name}'") for i in range(user_count): profile = fake.simple_profile() first_name: str = (fake.first_name_male() if profile["sex"] == "M" else fake.first_name_female()) last_name: str = profile["name"].split(" ").pop() username: str = f"{first_name[0].lower()}{last_name.lower()}" height = fake.pyint(min_value=59, max_value=76) bmi = fake.pyfloat(min_value=19, max_value=32) weight = (bmi / 703) * height * height user = User.create( username=username, email=f"{username}@{fake.free_email_domain()}", gender="male" if profile["sex"] == "M" else "female", birthday=fake.date_between(start_date="-60y", end_date="-21y"), first_name=first_name, last_name=last_name, password=fake.password(), household_id=household.id, height_inches=height, weight_lbs=weight, time_created=fake.date_between(start_date="-2y", end_date="-30d"), ) print( f"\tAdded user {user.first_name} {user.last_name} ({user.age}, {user.gender}): {user.username} / {user.email} / {user.birthday} / {user.height_inches}in / {user.weight_lbs}lbs" )
def test_all_house_users_can_see_meals(db): household = Household.create(name="test") user1 = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) user2 = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) recipe = Recipe.create(name="test") meal = Meal.create(date=dt.date.today(), household_id=household.id) meal.recipes.append(recipe) # make sure household -> meals -> recipes are all tied together assert len(meal.recipes) == 1 assert len(household.meals) == 1 # make sure both users can see the meal assert len(user1.household.meals) == 1 assert len(user2.household.meals) == 1 assert user1.household.meals[0].recipes[0] == user2.household.meals[ 0].recipes[0]
def create_base_users(): household = Household.create(name=fake.street_name()) test_user = User.create( username="******", email="*****@*****.**", password="******", gender="male", birthday=fake.date_between(start_date="-40y", end_date="-21y"), first_name="test", last_name="user", household_id=household.id, ) admin_user = User.create( username="******", password="******", email="*****@*****.**", gender="male", birthday=fake.date_between(start_date="-40y", end_date="-21y"), first_name="admin", last_name="user", household_id=household.id, ) admin_role = Role.query.filter(Role.name == "Admin").one_or_none() if admin_role is not None: admin_user.roles.append(admin_role) admin_user.save() else: print(f"Warning - no Admin role detected! Admin user only has the User role") print(f"Successfully created 'testuser' and 'admin' in house '{household.name}'")
def test_create_meal_without_recipes(db, household_factory): household = Household.create(name="test") meal = Meal.create(date=dt.date.today(), recipes=[], household_id=household.id) assert meal.id is not None assert meal.household_id == household.id
def create_test_user(): household = Household.create(name="Test Household") user = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) click.echo(f"Created used 'testuser' with password 'testuser' successfully!")
def test_user_creation_with_household(db): household = Household.create(name="test house") user = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) assert user.active == True assert user.household.name == "test house"
def setup(self): self.household = Household.create(name="TestHousehold") self.user = User.create( username="******", email="*****@*****.**", password="******", household_id=self.household.id, ) self.access_token = create_access_token(identity=self.user) self.headers = { "Authorization": f"Bearer {self.access_token}", }
def test_user(db): household = Household.create(name="test") user = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) # user_role = Role.create(name="User") # user.roles.append(user_role) # user.save() return user
def admin_user(db): household = Household.create(name="AdminHouse") user = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) # admin_role = Role.create(name="Admin") # user_admin_role = UserRole(user_id=user.id, role_id=admin_role.id) # user.roles.append(admin_role) # user.save() return user
def create_meal_history_for_household(household_id: int, days: int = 7): household = Household.get_by_id(household_id) create_meal_for_date = lambda dt: Meal.create(date=dt, household_id=household_id) # create one meal per day for X days for (dt, meal) in fake.time_series( start_date=f"-{days}d", end_date="now", precision=timedelta(days=1), distrib=create_meal_for_date, ): print(f"Created meal {meal.id} for date {dt} in household {household.name}") recipe = find_unique_recipe(household) meal.recipes.append(recipe) meal.save()
def test_create_meal_with_recipes(db, household_factory): household = Household.create(name="test") recipe = Recipe.create(name="test") meal = Meal.create(date=dt.date.today(), household_id=household.id) # test adding via association proxy meal.recipes.append(recipe) meal.save() assert len(meal.recipes) == 1 # test adding MealRecipe directly recipe = Recipe.create(name="meal_recipe_test") meal.meal_recipes.append(MealRecipe(recipe, meal)) meal.save() assert len(meal.recipes) == 2
def gen_test_data(): fake = Faker() db.create_all() # generate common chefs to pull from chefs = _generate_chefs(5, fake) for i in range(3): household = Household.create(name=fake.street_name()) click.echo(f'\nCreated household #{household.id} "{household.name}"') # create users for each house user_count = 2 if fake.boolean(chance_of_getting_true=80) is True else 1 for u in range(user_count): user = User.create( username=fake.simple_profile().get("username"), email=fake.email(), password=fake.password(length=12), household_id=household.id, ) click.echo(f'Created user "{user.username}"') # create a couple recipes for each user recipe_count = fake.pyint(max_value=10) for r in range(recipe_count): try: recipe = Recipe.create( name=_generate_recipe_name(fake), recipe_url=f"{fake.url()}recipes/recipe.html", image_url=fake.image_url(), thumbnail_url=fake.image_url(), author=fake.random_element(elements=chefs), rating=fake.pyint(min_value=1, max_value=5), servings=fake.pyint(min_value=1, max_value=10), prep_time=fake.pyint(min_value=5, max_value=60), cook_time=fake.pyint(min_value=5, max_value=360), user_id=user.id, ) # potentially add a note to the recipe if fake.boolean(): note = Note.create(text=fake.text(), recipe_id=recipe.id) except IntegrityError as ie: db.session.rollback() household.save()
def test_get_refresh_token(client, db): household = Household.create(name="house") user = User.create( username="******", email="*****@*****.**", password="******", household_id=household.id, ) refresh_token = create_refresh_token(identity=user) resp = client.post( url_for("api_v1.auth_refresh"), headers={ "Authorization": f"Bearer {refresh_token}", "Content-Type": "application/json", }, ) assert resp.status_code == 200 assert "access_token" in resp.json