from copy import deepcopy
from unittest import mock

from django.test import TestCase
from freezegun import freeze_time

from oscar.core.loading import get_class

import ocyan.core.fender
from ocyan.core.utils import merge_dicts

OrderNumberGenerator = get_class("order.utils", "OrderNumberGenerator")


def create_order_number_pattern(pattern):
    return merge_dicts(
        deepcopy(ocyan.core.fender.config.settings),
        {"oscar sequential order numbers": {"order_number_pattern": pattern}},
        True,
    )


@freeze_time("2012-01-14")
class SequenceTestCase(TestCase):
    @mock.patch.object(
        ocyan.core.fender.config, "settings", create_order_number_pattern("{number}")
    )
    def test_default(self):
        gen = OrderNumberGenerator()
        self.assertEqual(gen.order_number(None), "1")

    @mock.patch.object(
        ocyan.core.fender.config, "settings", create_order_number_pattern("%Y-{number}")
    )
    def test_year(self):
        gen = OrderNumberGenerator()
        self.assertEqual(gen.order_number(None), "2012-1")

    @mock.patch.object(
        ocyan.core.fender.config,
        "settings",
        create_order_number_pattern("%Y-%m-{number:012d}"),
    )
    def test_padded_year_month(self):
        gen = OrderNumberGenerator()
        self.assertEqual(gen.order_number(None), "2012-01-000000000001")

    @mock.patch.object(
        ocyan.core.fender.config,
        "settings",
        create_order_number_pattern("harrie%Y-{number}"),
    )
    def test_random_crap(self):
        gen = OrderNumberGenerator()
        self.assertEqual(gen.order_number(None), "harrie2012-1")
