<commit_before># -*- coding: utf-8
from __future__ import unicode_literals

import json
from django.test import TestCase
from django.core.urlresolvers import reverse
from myshop.models.polymorphic.smartcard import SmartCard
from myshop.models import Cart


class CatalogTest(TestCase):
    fixtures = ['myshop-polymorphic.json']

    def setUp(self):
        super(CatalogTest, self).setUp()
        self.sample = SmartCard.objects.get(slug='sdhc-card-4gb')
        self.assertIsNotNone(self.sample)

    def skip_test_html_content(self):
        from bs4 import BeautifulSoup

        url = self.sample.get_absolute_url()
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        soup = BeautifulSoup(response.content, 'html.parser')
        print(soup.prettify())
        self.assertEqual(soup.title.string, 'SDHC Card 4GB')
        breadcrumb = soup.find('ol', class_='breadcrumb')
        list_items = breadcrumb.find_all('li')
        self.assertEqual(list_items[0].a.string, 'HOME')
        self.assertEqual(list_items[1].a.string, 'Shop')
        self.assertEqual(list_items[2].a.string, 'Smart Cards')
        active_item = breadcrumb.find('li', class_='active')
        self.assertEqual(active_item.string, 'SDHC Card 4GB')

    def test_add2cart(self):
        #
        url = self.sample.get_absolute_url() + '/add-to-cart'
        response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        payload = json.loads(response.content)
        self.assertIsInstance(payload, dict)
        self.assertDictContainsSubset(
            {'unit_price': "€ 3.99", 'product': 1, 'extra': {'product_code': '1041'}},
            payload)

        # add two items of that Smart Card
        payload['quantity'] = 2
        url = reverse('shop:cart-list')
        response = self.client.post(url, payload)
        self.assertEqual(response.status_code, 201)
        cart = json.loads(response.content)
        self.assertDictContainsSubset(
            {'id': 1, 'product_name': "SDHC Card 4GB", 'product_url': self.sample.get_absolute_url(),
             'product_type': "Smart Card", 'product_model': "smartcard", 'price': "€ 3.99"},
            cart['summary'])

        # and add another one
        payload['quantity'] = 1
        response = self.client.post(url, payload)
        self.assertEqual(response.status_code, 201)

        # now check the cart detail
        response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        print(response.content)
        cart = json.loads(response.content)
        self.assertDictContainsSubset({'num_items': 1, 'total_quantity': 3, 'subtotal': "€ 11.97",
            'extra_rows': [{'amount': "€ 1.91", 'modifier': "cartexcludedtaxmodifier",
                'label': "19% VAT incl."}], 'total': "€ 11.97"}, cart)<commit_msg>attempt to fix utf-8 encoding in Py3<commit_after># -*- coding: utf-8
from __future__ import unicode_literals

import json
from django.test import TestCase
from django.core.urlresolvers import reverse
from myshop.models.polymorphic.smartcard import SmartCard
from myshop.models import Cart


class CatalogTest(TestCase):
    fixtures = ['myshop-polymorphic.json']

    def setUp(self):
        super(CatalogTest, self).setUp()
        self.sample = SmartCard.objects.get(slug='sdhc-card-4gb')
        self.assertIsNotNone(self.sample)

    def skip_test_html_content(self):
        from bs4 import BeautifulSoup

        url = self.sample.get_absolute_url()
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        soup = BeautifulSoup(response.content, 'html.parser')
        print(soup.prettify())
        self.assertEqual(soup.title.string, 'SDHC Card 4GB')
        breadcrumb = soup.find('ol', class_='breadcrumb')
        list_items = breadcrumb.find_all('li')
        self.assertEqual(list_items[0].a.string, 'HOME')
        self.assertEqual(list_items[1].a.string, 'Shop')
        self.assertEqual(list_items[2].a.string, 'Smart Cards')
        active_item = breadcrumb.find('li', class_='active')
        self.assertEqual(active_item.string, 'SDHC Card 4GB')

    def test_add2cart(self):
        #
        url = self.sample.get_absolute_url() + '/add-to-cart'
        response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        payload = json.loads(response.content.decode('utf-8'))
        self.assertIsInstance(payload, dict)
        self.assertDictContainsSubset(
            {'unit_price': "€ 3.99", 'product': 1, 'extra': {'product_code': '1041'}},
            payload)

        # add two items of that Smart Card
        payload['quantity'] = 2
        url = reverse('shop:cart-list')
        response = self.client.post(url, payload)
        self.assertEqual(response.status_code, 201)
        cart = json.loads(response.content.decode('utf-8'))
        self.assertDictContainsSubset(
            {'id': 1, 'product_name': "SDHC Card 4GB", 'product_url': self.sample.get_absolute_url(),
             'product_type': "Smart Card", 'product_model': "smartcard", 'price': "€ 3.99"},
            cart['summary'])

        # and add another one
        payload['quantity'] = 1
        response = self.client.post(url, payload)
        self.assertEqual(response.status_code, 201)

        # now check the cart detail
        response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        print(response.content)
        cart = json.loads(response.content.decode('utf-8'))
        self.assertDictContainsSubset({'num_items': 1, 'total_quantity': 3, 'subtotal': "€ 11.97",
            'extra_rows': [{'amount': "€ 1.91", 'modifier': "cartexcludedtaxmodifier",
                'label': "19% VAT incl."}], 'total': "€ 11.97"}, cart)
{'fraction_non_alphanumeric': 0.13581960104076324, 'fraction_numerical': 0.017519514310494363, 'mean_word_length': 3.0209205020920504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3601299', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1748, 'n_words': 357}