def setUp(self): self.gtin = '01234567890005' self.url = self.reverse('api:product-subproducts-list', gtin=self.gtin) self.product = ProductFactory(gtin=self.gtin) self.sub_product = ProductFactory() self.query = { # 'base': '', # 'pack': '', # 'case': '', # 'pallet': '', # 'display_shipper': '', # 'brand': True, # 'gtin': True, # 'description': True, # 'sku': True, # 'mark': True, # 'target_market': True, } self.data = {'gtin': self.gtin} self.member_organisation = MemberOrganisationFactory() self.profile = ProfileFactory() self.user = UserFactory(profile=self.profile) # string token self.token = AuthTokenFactory(user=self.user) self.api_client = APIClient() self.api_client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')
def setUp(self): self.superuser = get_user_model().objects.create_superuser( username='******', email='*****@*****.**', password='******') self.user_1 = UserFactory() self.user_2 = UserFactory() self.seller_1 = SellerFactory(owner=self.user_1) self.seller_2 = SellerFactory(owner=self.user_2) self.product_1 = ProductFactory(seller=self.seller_1) self.product_2 = ProductFactory(seller=self.seller_2)
def setUp(self): self.user = UserFactory(is_staff=True) self.mo_group = Group.objects.create(name='MO Admins') self.user.groups.add(self.mo_group) mo_user = MemberOrganisationUserFactory(user=self.user) self.user.profile.member_organisation = mo_user.organization self.user.profile.save() self.token = AuthTokenFactory(user=self.user) self.headers = dict(HTTP_AUTHORIZATION=f'Token {self.token}', ) self.client = APIClient() self.client.credentials(**self.headers) gtin = '01234567890005' self.language = LanguageFactory(slug='en') data = { 'gtin': gtin, 'member_organisation': self.user.profile.member_organisation, 'label_description_i18n': json.dumps({'en': 'label_description'}), 'description_i18n': json.dumps({'en': 'description'}), 'brand_i18n': json.dumps({'en': 'brand'}), 'functional_name_i18n': json.dumps({'en': 'functional_name'}), 'gs1_cloud_state': 'ACTIVE', 'language': self.language } self.product = ProductFactory(**data) reverse_url_name = 'api:product-detail' self.url = self.reverse(reverse_url_name, gtin=gtin)
def test_post_add_to_cart_attribute_error_bad_request(self): product_obj = ProductFactory() data = { 'wrong_product': product_obj.id, 'quantity': factories.FuzzyInteger(1, 10).fuzz() } response = self.post_json(data=data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.has_attribute_errors(response)
def test_without_quantity(self): sub_product_gtin = '012345678915' ProductFactory(gtin=sub_product_gtin) response = self.api_client.post(self.url, data={'subproduct': sub_product_gtin}) assert response.status_code == 400, response.status_code assert response.data == { 'quantity': 'This field required' }, response.data
def setUp(self): self.gtin = '01234567890005' self.url = self.reverse('api:product-subproducts-list', gtin=self.gtin) self.product = ProductFactory(gtin=self.gtin) self.member_organisation = MemberOrganisationFactory() self.profile = ProfileFactory() self.user = UserFactory(profile=self.profile) # string token self.token = AuthTokenFactory(user=self.user) self.api_client = APIClient() self.api_client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')
def test_list_product(self): products = ProductFactory.create_batch(2) request = MediaHttpRequest() response = self.client.get(reverse('products:product-list')) self.assertEqual(response.status_code, 200) self.assertListEqual( response.json(), ProductSerializer(products, many=True, context=dict(request=request)).data)
def _test_add_product_to_cart(self) -> None: """ Helper method to test adding product to cart. """ product_obj = ProductFactory() data = { 'product': product_obj.id, 'quantity': factories.FuzzyInteger(1, 10).fuzz() } resp = self.post_json(data=data) resp_data = resp.data self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(resp_data['success'], True)
def test_gift_can_be_updated(self): updated_data = { 'membership': MembershipFactory(), 'product': ProductFactory(), } gift_obj = Gift.objects.first() for key, value in updated_data.items(): setattr(gift_obj, key, value) gift_obj.save() self.assertEqual(gift_obj.membership.id, updated_data['membership'].id) self.assertEqual(gift_obj.product.id, updated_data['product'].id)
class ProductViewSetDestroyTest(APITestCase): def setUp(self): self.product = ProductFactory() def test_admin_can_delete_product(self): user = UserFactory(is_admin=True, is_active=True) self.client.force_login(user) response = self.client.delete( reverse('products:product-detail', kwargs=dict(uuid=str(self.product.uuid)))) self.assertEqual(response.status_code, 204) with self.assertRaises(Product.DoesNotExist): self.product.refresh_from_db() def test_unauthorized_user_cannot_delete_product(self): user = UserFactory(is_admin=False, is_active=True) self.client.force_login(user) response = self.client.delete( reverse('products:product-detail', kwargs=dict(uuid=str(self.product.uuid)))) self.assertEqual(response.status_code, 403) self.assertDictEqual( response.json(), {'detail': 'You do not have permission to perform this action.'}) def test_unauthenticated_user_cannot_delete_product(self): response = self.client.delete( reverse('products:product-detail', kwargs=dict(uuid=str(self.product.uuid)))) self.assertEqual(response.status_code, 403) self.assertDictEqual( response.json(), {'detail': 'Authentication credentials were not provided.'})
def test_valid_data(self): sub_product_gtin = '012345678915' ProductFactory(gtin=sub_product_gtin) response = self.api_client.post(self.url, data={ 'subproduct': sub_product_gtin, 'quantity': 5 }) assert response.status_code == 200, response.status_code expected_data = { 'product': '01234567890005', 'sub_product': '012345678915', 'quantity': '5' } assert response.data == expected_data, response.data
def setUp(self): self.user = UserFactory() self.seller = SellerFactory(owner=self.user) self.product = ProductFactory(seller=self.seller)
def setUp(self): self.product = ProductFactory()
def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.context = dict(request=MediaHttpRequest()) self.set_up_basket_session(str(self.basket.uuid))
def setUp(self): super().setUp() # Create a Product for testing ProductFactory()
def test_get_available_gtins(self): gtin = '08339355934961' product = ProductFactory(gtin=gtin) answer = self.prefix.get_available_gtins(products=[product]) assert gtin not in answer
def setUp(self): self.basket = BasketFactory(updated=False) self.product = ProductFactory() self.basket_product = BasketProductRelationFactory(basket=self.basket, product=self.product, quantity=3) self.set_up_basket_session(str(self.basket.uuid))
class ProductModelTests(TestCase): def setUp(self): self.superuser = get_user_model().objects.create_superuser( username='******', email='*****@*****.**', password='******') self.user_1 = UserFactory() self.user_2 = UserFactory() self.seller_1 = SellerFactory(owner=self.user_1) self.seller_2 = SellerFactory(owner=self.user_2) self.product_1 = ProductFactory(seller=self.seller_1) self.product_2 = ProductFactory(seller=self.seller_2) def test_product_model_listing(self): self.assertEqual(Product.objects.count(), 2) self.assertEqual(self.product_1.seller, self.seller_1) self.assertEqual(self.product_2.seller, self.seller_2) self.assertEqual(self.product_1.seller.owner, self.user_1) self.assertEqual(self.product_2.seller.owner, self.user_2) def test_only_user_who_created_the_product_can_update_it(self): self.assertTrue(self.product_1.can_update(self.user_1)) self.assertTrue(self.product_2.can_update(self.user_2)) self.assertFalse(self.product_1.can_update(self.user_2)) self.assertFalse(self.product_2.can_update(self.user_1)) def test_only_user_who_created_the_product_can_delete_it(self): self.assertTrue(self.product_1.can_delete(self.user_1)) self.assertTrue(self.product_2.can_delete(self.user_2)) self.assertFalse(self.product_1.can_delete(self.user_2)) self.assertFalse(self.product_2.can_delete(self.user_1)) def test_superuser_can_update_any_product(self): self.assertTrue(self.product_1.can_update(self.superuser)) self.assertTrue(self.product_2.can_update(self.superuser)) def test_superuser_can_delete_any_product(self): self.assertTrue(self.product_1.can_delete(self.superuser)) self.assertTrue(self.product_2.can_delete(self.superuser)) def test_most_recent_products_are_listed_first(self): yesterday = timezone.now() + timezone.timedelta(days=-1) last_week = timezone.now() + timezone.timedelta(days=-7) self.product_1.date_updated = last_week self.product_2.date_updated = yesterday self.product_1.save() self.product_2.save() latest_product = Product.objects.all()[0] self.assertEqual(latest_product, self.product_2)
def _seed_products(cls) -> None: """ Seeding products from CSV file to database. """ # Read products data from csv file. dir_path = os.path.dirname(os.path.realpath(__file__)) raw_data = pd.read_csv(f'{dir_path}/styles.csv', error_bad_lines=False) # Seeding parent categories [ CategoryFactory(name=category) for category in raw_data['masterCategory'].unique() ] # Seeding gender [GenderFactory(name=name) for name in raw_data['gender'].unique()] # Seeding season [SeasonFactory(name=name) for name in raw_data['season'].unique()] # Seeding article type [ ArticleTypeFactory(name=name) for name in raw_data['articleType'].unique() ] # Seeding colour [ BaseColourFactory(name=name) for name in raw_data['baseColour'].unique() ] for index in range(200): name = str(raw_data.at[index, 'productDisplayName']) gender = str(raw_data.at[index, 'gender']) base_color = str(raw_data.at[index, 'baseColour']) usage = str(raw_data.at[index, 'usage']) season = str(raw_data.at[index, 'season']) article_type = str(raw_data.at[index, 'articleType']) sub_cat_name = str(raw_data.at[index, 'subCategory']) master_cat_name = str(raw_data.at[index, 'masterCategory']) # Find or create category if not existed try: sub_category_obj = Category.objects.get(name=sub_cat_name) except: sub_category_obj = None try: master_category_obj = Category.objects.get( name=master_cat_name) except: master_category_obj = None if not master_category_obj: master_category_obj = CategoryFactory(name=master_cat_name) if not sub_category_obj: sub_category_obj = CategoryFactory(name=sub_cat_name, parent=master_category_obj) try: gender_obj = Gender.objects.get(name=gender) except: gender_obj = None try: usage_obj = Usage.objects.get(name=usage) except: usage_obj = None try: base_color_obj = BaseColour.objects.get(name=base_color) except: base_color_obj = None try: season_obj = Season.objects.get(name=season) except: season_obj = None try: article_type_obj = ArticleType.objects.get(name=article_type) except: article_type_obj = None product_data = { 'name': name, 'price': FuzzyFloat(10, 200), 'category': sub_category_obj, 'gender': gender_obj, 'base_colour': base_color_obj, 'season': season_obj, 'usage': usage_obj, 'article_type': article_type_obj, } cls.products.append(ProductFactory(**product_data)) print(f'Inserted {index} products')