<commit_before>"""Tests for easycart.cart."""
import json
from copy import deepcopy
from unittest.mock import Mock, patch

from django.test import TestCase, RequestFactory

from easycart import (
    BaseItem,
    InvalidItemQuantity,
    ItemNotInCart,
    ItemNotInDatabase,
)
from tests.common import (
    DUMMY_SESSION_DATA,
    SESSION_KEY,
    Cart,
    SessionStore,
    fill_db,
    set_up_session,
)
from tests.models import Item, Book, Magazine

request_factory = RequestFactory()


class TestCartItem(TestCase):

    def setUp(self):
        self.obj = Item(pk=1, name='dummy_item', price=100)

    def test_instance_attrs_are_ok(self):
        item = BaseItem(self.obj, 3)
        self.assertIs(item.obj, self.obj)
        self.assertEqual(item.price, 100)
        self.assertEqual(item.quantity, 3)
        self.assertEqual(item.total, 300)

    def test_constructor_cleans_quantity(self):
        with patch.object(BaseItem, 'clean_quantity') as mock_clean_quantity:
            mock_clean_quantity.return_value = 'cleaned'
            item = BaseItem(self.obj, 'raw')
        mock_clean_quantity.assert_called_once_with('raw')
        self.assertEqual(item.quantity, 'cleaned')

    def test_quantity_can_be_changed_to_valid_value(self):
        item = BaseItem(self.obj, quantity=1)

        def test(new_quantity):
            item.quantity = new_quantity
            self.assertEqual(item.quantity, int(new_quantity))

        test(10)
        test('19')
        test(123.45)

    def test_quantity_cannot_be_changed_to_invalid_value(self):
        item = BaseItem(self.obj, quantity=3)

        def test(new_quantity):
            with self.assertRaises(InvalidItemQuantity):
                item.quantity = new_quantity
            self.assertEqual(item.quantity, 3)

        test('text-that-cant-be-converted-to-int')
        test(0)
        test(-1)
        item.max_quantity = 999
        test(1000)

    def test_total_changes_with_quantity(self):
        item = BaseItem(self.obj, quantity=1)
        self.assertEqual(item.total, 100)
        item.quantity = 10
        self.assertEqual(item.total, 1000)

    def test__eq__(self):
        item_1 = BaseItem(self.obj)
        item_2 = BaseItem(self.obj)
        item_3 = BaseItem(self.obj, 2)
        self.assertEqual(item_1, item_1)
        self.assertEqual(item_1, item_2)
        self.assertNotEqual(item_1, item_3)

    def test__repr__(self):
        item = BaseItem(self.obj, 1)
        self.assertEqual(repr(item), '<CartItem: obj=dummy_item, quantity=1>')


class TestCart(TestCase):

    def setUp(self):
        fill_db()
        request = request_factory.post('')
        request.session = SessionStore()
        set_up_session(request.session)
        cart = Cart(request)
        mock_update = Mock(wraps=cart.update)
        patch.object(cart, 'update', mock_update).start()<commit_msg>Update names of cart and cart item test cases<commit_after>"""Tests for easycart.cart."""
import json
from copy import deepcopy
from unittest.mock import Mock, patch

from django.test import TestCase, RequestFactory

from easycart import (
    BaseItem,
    InvalidItemQuantity,
    ItemNotInCart,
    ItemNotInDatabase,
)
from tests.common import (
    DUMMY_SESSION_DATA,
    SESSION_KEY,
    Cart,
    SessionStore,
    fill_db,
    set_up_session,
)
from tests.models import Item, Book, Magazine

request_factory = RequestFactory()


class TestBaseItem(TestCase):

    def setUp(self):
        self.obj = Item(pk=1, name='dummy_item', price=100)

    def test_instance_attrs_are_ok(self):
        item = BaseItem(self.obj, 3)
        self.assertIs(item.obj, self.obj)
        self.assertEqual(item.price, 100)
        self.assertEqual(item.quantity, 3)
        self.assertEqual(item.total, 300)

    def test_constructor_cleans_quantity(self):
        with patch.object(BaseItem, 'clean_quantity') as mock_clean_quantity:
            mock_clean_quantity.return_value = 'cleaned'
            item = BaseItem(self.obj, 'raw')
        mock_clean_quantity.assert_called_once_with('raw')
        self.assertEqual(item.quantity, 'cleaned')

    def test_quantity_can_be_changed_to_valid_value(self):
        item = BaseItem(self.obj, quantity=1)

        def test(new_quantity):
            item.quantity = new_quantity
            self.assertEqual(item.quantity, int(new_quantity))

        test(10)
        test('19')
        test(123.45)

    def test_quantity_cannot_be_changed_to_invalid_value(self):
        item = BaseItem(self.obj, quantity=3)

        def test(new_quantity):
            with self.assertRaises(InvalidItemQuantity):
                item.quantity = new_quantity
            self.assertEqual(item.quantity, 3)

        test('text-that-cant-be-converted-to-int')
        test(0)
        test(-1)
        item.max_quantity = 999
        test(1000)

    def test_total_changes_with_quantity(self):
        item = BaseItem(self.obj, quantity=1)
        self.assertEqual(item.total, 100)
        item.quantity = 10
        self.assertEqual(item.total, 1000)

    def test__eq__(self):
        item_1 = BaseItem(self.obj)
        item_2 = BaseItem(self.obj)
        item_3 = BaseItem(self.obj, 2)
        self.assertEqual(item_1, item_1)
        self.assertEqual(item_1, item_2)
        self.assertNotEqual(item_1, item_3)

    def test__repr__(self):
        item = BaseItem(self.obj, 1)
        self.assertEqual(repr(item), '<CartItem: obj=dummy_item, quantity=1>')


class TestBaseCart(TestCase):

    def setUp(self):
        fill_db()
        request = request_factory.post('')
        request.session = SessionStore()
        set_up_session(request.session)
        cart = Cart(request)
        mock_update = Mock(wraps=cart.update)
        patch.object(cart, 'update', mock_update).start()
{'fraction_non_alphanumeric': 0.10262612301313062, 'fraction_numerical': 0.019004837595024186, 'mean_word_length': 2.8261731658955718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4289599', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1769, 'n_words': 335}