def setUp(self):
    self.client = APIClient()
    'Creates sample data for database'
    cwd = Path(__file__).parent.absolute()
    with open(f'{cwd}/test.json', 'r') as f:
        default = json.load(f)
    for data in default['results']:
        set_id = data.pop('set_id')
        products_data = data.pop('products')
        set_obj = Set.objects.create(id=set_id)
        spl_obj = set_obj.spls.create(**data)
        for product_data in products_data:
            product_data.pop('name')
            packages_data = product_data.pop('packages')
            if ('inactive_ingredients' in product_data):
                inactive_ingredients_data = product_data.pop('inactive_ingredients')
                inactive_ingredients_list = []
                for inactive_ingredient_data in inactive_ingredients_data:
                    try:
                        ingredient = InactiveIngredient.objects.get(**inactive_ingredient_data)
                        inactive_ingredients_list.append(ingredient)
                    except Exception:
                        ingredient = InactiveIngredient.objects.create(**inactive_ingredient_data)
                        inactive_ingredients_list.append(ingredient)
            product_obj = spl_obj.products.create(**product_data)
            product_obj.inactive_ingredients.add(*inactive_ingredients_list)
            for package_data in packages_data:
                product_obj.packages.create(**package_data)
def setUp(self):
    self.client = APIClient()
    'Creates sample data for database'
    cwd = Path(__file__).parent.absolute()
    with open(f'{cwd}/test.json', 'r') as f:
        default = json.load(f)
    for data in default['results']:
        set_id = data.pop('set_id')
        products_data = data.pop('products')
        set_obj = Set.objects.create(id=set_id)
        spl_obj = set_obj.spls.create(**data)
        for product_data in products_data:
            product_data.pop('name')
            packages_data = product_data.pop('packages')
            if ('inactive_ingredients' in product_data):
                inactive_ingredients_data = product_data.pop('inactive_ingredients')
                inactive_ingredients_list = []
                for inactive_ingredient_data in inactive_ingredients_data:
                    try:
                        ingredient = InactiveIngredient.objects.get(**inactive_ingredient_data)
                        inactive_ingredients_list.append(ingredient)
                    except Exception:
                        ingredient = InactiveIngredient.objects.create(**inactive_ingredient_data)
                        inactive_ingredients_list.append(ingredient)
            product_obj = spl_obj.products.create(**product_data)
            product_obj.inactive_ingredients.add(*inactive_ingredients_list)
            for package_data in packages_data:
                product_obj.packages.create(**package_data)