# Path: tests/base.py
# class BreadTestCase(TestCase):
#     url_namespace = ""
#     extra_bread_attributes = {}
# 
#     def setUp(self):
#         self.username = "joe"
#         self.password = "random"
#         User = get_user_model()
#         self.user = User.objects.create_user(username=self.username)
#         self.user.set_password(self.password)
#         self.user.save()
#         assert self.client.login(username=self.username, password=self.password)
#         self.model = BreadTestModel
#         self.model_name = self.model._meta.model_name
#         self.model_factory = BreadTestModelFactory
#         self.request_factory = RequestFactory()
# 
#         class ReadClass(ReadView):
#             columns = [
#                 ("Name", "name"),
#                 ("Text", "other__text"),
#                 (
#                     "Model1",
#                     "model1",
#                 ),
#             ]
# 
#         class BrowseClass(BrowseView):
#             columns = [
#                 ("Name", "name"),
#                 ("Text", "other__text"),
#                 (
#                     "Model1",
#                     "model1",
#                 ),
#             ]
# 
#         class BreadTestClass(Bread):
#             model = self.model
#             base_template = "bread/empty.html"
#             browse_view = BrowseClass
#             namespace = self.url_namespace
#             plural_name = "testmodels"
# 
#             def get_additional_context_data(self):
#                 context = super(BreadTestClass, self).get_additional_context_data()
#                 context["bread_test_class"] = True
#                 return context
# 
#         for k, v in self.extra_bread_attributes.items():
#             setattr(BreadTestClass, k, v)
# 
#         self.BreadTestClass = BreadTestClass
#         self.bread = BreadTestClass()
# 
#     def tearDown(self):
#         global urlpatterns
#         urlpatterns = None
# 
#     def set_urls(self, bread):
#         # Given a bread instance, set its URLs on the test urlconf
#         global urlpatterns
#         urlpatterns = bread.get_urls()
# 
#     def get_permission(self, short_name):
#         """Return a Permission object for the test model.
#         short_name should be browse, read, edit, add, or delete.
#         """
#         return Permission.objects.get_or_create(
#             content_type=ContentType.objects.get_for_model(self.model),
#             codename="%s_%s" % (short_name, self.model_name),
#         )[0]
# 
#     def give_permission(self, short_name):
#         self.user.user_permissions.add(self.get_permission(short_name))
# 
# Path: tests/models.py
# class BreadTestModel(models.Model):
#     name = models.CharField(max_length=10)
#     age = models.IntegerField()
#     other = models.ForeignKey(
#         BreadTestModel2,
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#     )
# 
#     class Meta:
#         ordering = [
#             "name",
#             "-age",  # If same name, sort oldest first
#         ]
#         permissions = [
#             ("browse_breadtestmodel", "can browse BreadTestModel"),
#         ]
# 
#     def __str__(self):
#         return self.name
# 
#     def get_name(self):
#         return self.name
# 
#     def method1(self, arg):
#         # Method that has a required arg
#         pass
# 
#     def method2(self, arg=None):
#         # method that has an optional arg
#         pass


from django import forms
from django.core.exceptions import ValidationError
from django.urls import reverse
from .base import BreadTestCase
from .models import BreadTestModel
        url = reverse(self.bread.get_url_name("edit"), kwargs={"pk": item.pk})
        request = self.request_factory.post(url, data={"name": "Fred", "age": "19"})
        request.user = self.user
        self.give_permission("change")
        view = self.bread.get_edit_view()
        rsp = view(request, pk=item.pk)
        self.assertEqual(400, rsp.status_code)
        context = rsp.context_data
        form = context["form"]
        errors = form.errors
        self.assertIn("name", errors)

    def test_get(self):
        # Get should give you a form with the item filled in
        item = self.model_factory()
        url = reverse(self.bread.get_url_name("edit"), kwargs={"pk": item.pk})
        request = self.request_factory.get(url)
        request.user = self.user
        self.give_permission("change")
        view = self.bread.get_edit_view()
        rsp = view(request, pk=item.pk)
        self.assertEqual(200, rsp.status_code)
        form = rsp.context_data["form"]
        self.assertFalse(form.is_bound)
        self.assertEqual(item.name, form.initial["name"])
        rsp.render()
        body = rsp.content.decode("utf-8")
        self.assertIn('method="POST"', body)



# Path: tests/base.py
# class BreadTestCase(TestCase):
#     url_namespace = ""
#     extra_bread_attributes = {}
# 
#     def setUp(self):
#         self.username = "joe"
#         self.password = "random"
#         User = get_user_model()
#         self.user = User.objects.create_user(username=self.username)
#         self.user.set_password(self.password)
#         self.user.save()
#         assert self.client.login(username=self.username, password=self.password)
#         self.model = BreadTestModel
#         self.model_name = self.model._meta.model_name
#         self.model_factory = BreadTestModelFactory
#         self.request_factory = RequestFactory()
# 
#         class ReadClass(ReadView):
#             columns = [
#                 ("Name", "name"),
#                 ("Text", "other__text"),
#                 (
#                     "Model1",
#                     "model1",
#                 ),
#             ]
# 
#         class BrowseClass(BrowseView):
#             columns = [
#                 ("Name", "name"),
#                 ("Text", "other__text"),
#                 (
#                     "Model1",
#                     "model1",
#                 ),
#             ]
# 
#         class BreadTestClass(Bread):
#             model = self.model
#             base_template = "bread/empty.html"
#             browse_view = BrowseClass
#             namespace = self.url_namespace
#             plural_name = "testmodels"
# 
#             def get_additional_context_data(self):
#                 context = super(BreadTestClass, self).get_additional_context_data()
#                 context["bread_test_class"] = True
#                 return context
# 
#         for k, v in self.extra_bread_attributes.items():
#             setattr(BreadTestClass, k, v)
# 
#         self.BreadTestClass = BreadTestClass
#         self.bread = BreadTestClass()
# 
#     def tearDown(self):
#         global urlpatterns
#         urlpatterns = None
# 
#     def set_urls(self, bread):
#         # Given a bread instance, set its URLs on the test urlconf
#         global urlpatterns
#         urlpatterns = bread.get_urls()
# 
#     def get_permission(self, short_name):
#         """Return a Permission object for the test model.
#         short_name should be browse, read, edit, add, or delete.
#         """
#         return Permission.objects.get_or_create(
#             content_type=ContentType.objects.get_for_model(self.model),
#             codename="%s_%s" % (short_name, self.model_name),
#         )[0]
# 
#     def give_permission(self, short_name):
#         self.user.user_permissions.add(self.get_permission(short_name))
# 
# Path: tests/models.py
# class BreadTestModel(models.Model):
#     name = models.CharField(max_length=10)
#     age = models.IntegerField()
#     other = models.ForeignKey(
#         BreadTestModel2,
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#     )
# 
#     class Meta:
#         ordering = [
#             "name",
#             "-age",  # If same name, sort oldest first
#         ]
#         permissions = [
#             ("browse_breadtestmodel", "can browse BreadTestModel"),
#         ]
# 
#     def __str__(self):
#         return self.name
# 
#     def get_name(self):
#         return self.name
# 
#     def method1(self, arg):
#         # Method that has a required arg
#         pass
# 
#     def method2(self, arg=None):
#         # method that has an optional arg
#         pass


# Path: tests/test_forms.py
from django import forms
from django.core.exceptions import ValidationError
from django.urls import reverse
from .base import BreadTestCase
from .models import BreadTestModel

        url = reverse(self.bread.get_url_name("edit"), kwargs={"pk": item.pk})
        request = self.request_factory.post(url, data={"name": "Fred", "age": "19"})
        request.user = self.user
        self.give_permission("change")
        view = self.bread.get_edit_view()
        rsp = view(request, pk=item.pk)
        self.assertEqual(400, rsp.status_code)
        context = rsp.context_data
        form = context["form"]
        errors = form.errors
        self.assertIn("name", errors)

    def test_get(self):
        # Get should give you a form with the item filled in
        item = self.model_factory()
        url = reverse(self.bread.get_url_name("edit"), kwargs={"pk": item.pk})
        request = self.request_factory.get(url)
        request.user = self.user
        self.give_permission("change")
        view = self.bread.get_edit_view()
        rsp = view(request, pk=item.pk)
        self.assertEqual(200, rsp.status_code)
        form = rsp.context_data["form"]
        self.assertFalse(form.is_bound)
        self.assertEqual(item.name, form.initial["name"])
        rsp.render()
        body = rsp.content.decode("utf-8")
        self.assertIn('method="POST"', body)



class BreadExcludeTest(BreadTestCase):