# Path: chamber/forms/fields.py
# class DecimalField(forms.DecimalField):
# 
#     def __init__(self, *args, **kwargs):
#         self.step = kwargs.pop('step', 'any')
#         self.min = kwargs.pop('min', None)
#         self.max = kwargs.pop('max', None)
#         super().__init__(*args, **kwargs)
# 
#     def widget_attrs(self, widget):
#         attrs = super().widget_attrs(widget)
#         attrs['step'] = self.step
#         if self.min is not None:
#             attrs['min'] = self.min
#         if self.max is not None:
#             attrs['max'] = self.max
#         return attrs
# 
# class RestrictedFileField(forms.FileField):
# 
#     def __init__(self, *args, **kwargs):
#         max_upload_size = kwargs.pop('max_upload_size', settings.MAX_FILE_UPLOAD_SIZE) * 1024 * 1024
#         allowed_content_types = kwargs.pop('allowed_content_types', None)
# 
#         validators = tuple(kwargs.pop('validators', [])) + (
#             RestrictedFileValidator(max_upload_size),
#         )
#         if allowed_content_types:
#             validators += (
#                 AllowedContentTypesByFilenameFileValidator(allowed_content_types),
#                 AllowedContentTypesByContentFileValidator(allowed_content_types),
#             )
#         super().__init__(validators=validators, *args, **kwargs)


import os
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import TextInput, ValidationError
from django.test import TransactionTestCase
from germanium.tools import assert_equal, assert_true, assert_raises, assert_not_raises  # pylint: disable=E0401
from chamber.forms.fields import DecimalField, RestrictedFileField





__all__ = (
    'FormFieldsTestCase',
)


class FormFieldsTestCase(TransactionTestCase):

    def test_decimal_field_should_return_correct_widget_attrs(self):
        kwargs = {
            'step': 0.5,
            'min': 1.0,
            'max': 10.0,
        }
        field = DecimalField(**kwargs)
        widget_attrs = field.widget_attrs(TextInput())
        assert_true(len(widget_attrs.keys()) > 0)
        for attr, value in kwargs.items():
            assert_equal(value, widget_attrs[attr])

    def test_decimal_field_should_return_default_attrs(self):
        field = DecimalField()
        widget_attrs = field.widget_attrs(TextInput())
        assert_equal({'step': 'any'}, widget_attrs)

    def test_restricted_file_field_should_raise_validation_error_for_invalid_files(self):

# Path: chamber/forms/fields.py
# class DecimalField(forms.DecimalField):
# 
#     def __init__(self, *args, **kwargs):
#         self.step = kwargs.pop('step', 'any')
#         self.min = kwargs.pop('min', None)
#         self.max = kwargs.pop('max', None)
#         super().__init__(*args, **kwargs)
# 
#     def widget_attrs(self, widget):
#         attrs = super().widget_attrs(widget)
#         attrs['step'] = self.step
#         if self.min is not None:
#             attrs['min'] = self.min
#         if self.max is not None:
#             attrs['max'] = self.max
#         return attrs
# 
# class RestrictedFileField(forms.FileField):
# 
#     def __init__(self, *args, **kwargs):
#         max_upload_size = kwargs.pop('max_upload_size', settings.MAX_FILE_UPLOAD_SIZE) * 1024 * 1024
#         allowed_content_types = kwargs.pop('allowed_content_types', None)
# 
#         validators = tuple(kwargs.pop('validators', [])) + (
#             RestrictedFileValidator(max_upload_size),
#         )
#         if allowed_content_types:
#             validators += (
#                 AllowedContentTypesByFilenameFileValidator(allowed_content_types),
#                 AllowedContentTypesByContentFileValidator(allowed_content_types),
#             )
#         super().__init__(validators=validators, *args, **kwargs)


# Path: example/dj/apps/test_chamber/tests/forms/fields.py
import os
from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import TextInput, ValidationError
from django.test import TransactionTestCase
from germanium.tools import assert_equal, assert_true, assert_raises, assert_not_raises  # pylint: disable=E0401
from chamber.forms.fields import DecimalField, RestrictedFileField






__all__ = (
    'FormFieldsTestCase',
)


class FormFieldsTestCase(TransactionTestCase):

    def test_decimal_field_should_return_correct_widget_attrs(self):
        kwargs = {
            'step': 0.5,
            'min': 1.0,
            'max': 10.0,
        }
        field = DecimalField(**kwargs)
        widget_attrs = field.widget_attrs(TextInput())
        assert_true(len(widget_attrs.keys()) > 0)
        for attr, value in kwargs.items():
            assert_equal(value, widget_attrs[attr])

    def test_decimal_field_should_return_default_attrs(self):
        field = DecimalField()
        widget_attrs = field.widget_attrs(TextInput())
        assert_equal({'step': 'any'}, widget_attrs)

    def test_restricted_file_field_should_raise_validation_error_for_invalid_files(self):

        field = RestrictedFileField(allowed_content_types=('image/jpeg', 'application/pdf'), max_upload_size=1)