from django.test import TestCase

import os

from .models import Part, PartCategory
from .models import rename_part_image


class SimplePartTest(TestCase):

    def setUp(self):

        cat = PartCategory.objects.create(name='TLC', description='Top level category')

        self.px = Part.objects.create(name='x', description='A part called x', buildable=True)
        self.py = Part.objects.create(name='y', description='A part called y', consumable=False)
        self.pz = Part.objects.create(name='z', description='A part called z', category=cat)

    def test_metadata(self):
        self.assertEqual(self.px.name, 'x')
        self.assertEqual(self.py.get_absolute_url(), '/part/2/')
        self.assertEqual(str(self.pz), 'z - A part called z')

    def test_category(self):
        self.assertEqual(self.px.category_path, '')
        self.assertEqual(self.pz.category_path, 'TLC')

    def test_rename_img(self):
        img = rename_part_image(self.px, 'hello.png')
        self.assertEqual(img, os.path.join('part_images', 'part_1_img.png'))

        img = rename_part_image(self.pz, 'test')
        self.assertEqual(img, os.path.join('part_images', 'part_3_img'))

    def test_stock(self):
        # Stock should initially be zero
        self.assertEqual(self.px.total_stock, 0)
        self.assertEqual(self.py.available_stock, 0)

from django.test import TestCase

import os

from .models import Part
from .models import rename_part_image


class SimplePartTest(TestCase):
    """ Tests for the Part model """

    fixtures = [
        'category',
        'part',
        'location',
    ]

    def setUp(self):
        self.R1 = Part.objects.get(name='R_2K2_0805')
        self.R2 = Part.objects.get(name='R_4K7_0603')

        self.C1 = Part.objects.get(name='C_22N_0805')

    def test_metadata(self):
        self.assertEqual(self.R1.name, 'R_2K2_0805')
        self.assertEqual(self.R1.get_absolute_url(), '/part/3/')

    def test_category(self):
        self.assertEqual(str(self.C1.category), 'Electronics/Capacitors')

    def test_rename_img(self):
        img = rename_part_image(self.R1, 'hello.png')
        self.assertEqual(img, os.path.join('part_images', 'part_3_img.png'))

        img = rename_part_image(self.R2, 'test')
        self.assertEqual(img, os.path.join('part_images', 'part_4_img'))

    def test_stock(self):
        # No stock of any resistors
        res = Part.objects.filter(description__contains='resistor')
        for r in res:
            self.assertEqual(r.total_stock, 0)
            self.assertEqual(r.available_stock, 0)

Convert test_part to use fixture data