from django.test import TestCase

from .factories import PartyFactory, PartyEmblemFactory


class TestPartyModels(TestCase):
    def setUp(self):
        PartyFactory.reset_sequence()

    def test_party_str(self):
        party = PartyFactory()
        self.assertEqual(str(party), "Party 0 (PP0)")

    def test_party_emblem(self):
        party = PartyFactory()
        PartyEmblemFactory.create_batch(3, party=party)
        self.assertEqual(party.emblems.count(), 3)
        self.assertTrue(
            party.emblems.first().image.url.startswith(
                "/media/emblems/PP0/0_example"
            )
        )

        # Add a default image and assert it's the deafult on the party
        PartyEmblemFactory(party=party, __sequence=99, default=True)
        self.assertTrue(
            party.default_emblem.image.url.startswith(
                "/media/emblems/PP0/99_example"
            )
        )

from django.test import TestCase
from django.core.files.storage import DefaultStorage

from candidates.tests.helpers import TmpMediaRootMixin
from .factories import PartyFactory, PartyEmblemFactory


class TestPartyModels(TmpMediaRootMixin, TestCase):
    def setUp(self):
        self.storage = DefaultStorage()
        PartyFactory.reset_sequence()

    def test_party_str(self):
        party = PartyFactory()
        self.assertEqual(str(party), "Party 0 (PP0)")

    def test_party_emblem(self):
        party = PartyFactory()
        PartyEmblemFactory.create_batch(3, party=party)
        self.assertEqual(party.emblems.count(), 3)
        self.assertTrue(
            party.emblems.first().image.url.startswith(
                "/media/emblems/PP0/0_example"
            )
        )

        # Add a default image and assert it's the deafult on the party
        PartyEmblemFactory(party=party, __sequence=99, default=True)
        self.assertTrue(
            party.default_emblem.image.url.startswith(
                "/media/emblems/PP0/99_example"
            )
        )

Test using tmp media root
Test using tmp media root

  from django.test import TestCase
+ from django.core.files.storage import DefaultStorage
  
+ from candidates.tests.helpers import TmpMediaRootMixin
  from .factories import PartyFactory, PartyEmblemFactory
  
  
- class TestPartyModels(TestCase):
+ class TestPartyModels(TmpMediaRootMixin, TestCase):
      def setUp(self):
+         self.storage = DefaultStorage()
          PartyFactory.reset_sequence()
  
      def test_party_str(self):
          party = PartyFactory()
          self.assertEqual(str(party), "Party 0 (PP0)")
  
      def test_party_emblem(self):
          party = PartyFactory()
          PartyEmblemFactory.create_batch(3, party=party)
          self.assertEqual(party.emblems.count(), 3)
          self.assertTrue(
              party.emblems.first().image.url.startswith(
                  "/media/emblems/PP0/0_example"
              )
          )
  
          # Add a default image and assert it's the deafult on the party
          PartyEmblemFactory(party=party, __sequence=99, default=True)
          self.assertTrue(
              party.default_emblem.image.url.startswith(
                  "/media/emblems/PP0/99_example"
              )
          )
  
Test using tmp media root
## Code Before:
from django.test import TestCase

from .factories import PartyFactory, PartyEmblemFactory


class TestPartyModels(TestCase):
    def setUp(self):
        PartyFactory.reset_sequence()

    def test_party_str(self):
        party = PartyFactory()
        self.assertEqual(str(party), "Party 0 (PP0)")

    def test_party_emblem(self):
        party = PartyFactory()
        PartyEmblemFactory.create_batch(3, party=party)
        self.assertEqual(party.emblems.count(), 3)
        self.assertTrue(
            party.emblems.first().image.url.startswith(
                "/media/emblems/PP0/0_example"
            )
        )

        # Add a default image and assert it's the deafult on the party
        PartyEmblemFactory(party=party, __sequence=99, default=True)
        self.assertTrue(
            party.default_emblem.image.url.startswith(
                "/media/emblems/PP0/99_example"
            )
        )

## Instruction:
Test using tmp media root
## Code After:
from django.test import TestCase
from django.core.files.storage import DefaultStorage

from candidates.tests.helpers import TmpMediaRootMixin
from .factories import PartyFactory, PartyEmblemFactory


class TestPartyModels(TmpMediaRootMixin, TestCase):
    def setUp(self):
        self.storage = DefaultStorage()
        PartyFactory.reset_sequence()

    def test_party_str(self):
        party = PartyFactory()
        self.assertEqual(str(party), "Party 0 (PP0)")

    def test_party_emblem(self):
        party = PartyFactory()
        PartyEmblemFactory.create_batch(3, party=party)
        self.assertEqual(party.emblems.count(), 3)
        self.assertTrue(
            party.emblems.first().image.url.startswith(
                "/media/emblems/PP0/0_example"
            )
        )

        # Add a default image and assert it's the deafult on the party
        PartyEmblemFactory(party=party, __sequence=99, default=True)
        self.assertTrue(
            party.default_emblem.image.url.startswith(
                "/media/emblems/PP0/99_example"
            )
        )
