#Need help in writing test cases

20 messages ยท Page 1 of 1 (latest)

mossy raven
#
class TestGenerateDischargeSummaryPDF(TestUtils, unittest.TestCase):
    @classmethod
    def setUpTestData(cls) -> None:
        cls.state = cls.create_state()
        cls.district = cls.create_district(cls.state)
        cls.local_body = cls.create_local_body(cls.district)
        cls.super_user = cls.create_super_user("su", cls.district)
        cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
        cls.user = cls.create_user("staff1", cls.district, home_facility=cls.facility)
        cls.patient = cls.create_patient(
            cls.district, cls.facility, local_body=cls.local_body
        )
        cls.consultation = cls.create_consultation(cls.patient, cls.facility)

    def setUp(self) -> None:
        self.client = APIClient()

    def test_pdf_generation_success(self):
        test_data = {"consultation": self.consultation}

        with tempfile.NamedTemporaryFile(suffix=".pdf") as file:
            compile_typ(file.name, test_data)  # Assuming compile_typ generates the PDF

            # Assert that the PDF file exists
            self.assertTrue(os.path.exists(file.name))

            # Optional: Check PDF size (if size is expected to be non-zero)
            self.assertGreater(os.path.getsize(file.name), 0)  # Uncomment if needed

I'm not able to access self.consultation - test_data = {"consultation": self.consultation}

actually I'm not able to access any variable I've defined in setUpTestData , not sure why. The similar fuctions elsewhere works perfectly fine

dull dock
#

are cls.create_state() etc. classmethods that come in via the TestUtils mixin?

dull dock
#

can you confirm that setUpTestData is executed at all? e.g. add a raise ValueError at top

mossy raven
#

I tried adding print statements , but they didn't execute

dull dock
#

hm. can you remove the mixin for a moment and try

class TestGenerateDischargeSummaryPDF(unittest.TestCase):
    @classmethod
    def setUpTestData(cls) -> None:
        raise ValueError("setUpTestData is being executed")

that should give you the ValueError on test run, does it?

mossy raven
#

directly going to test function after executing setUp

dull dock
#

hm. how are you running the test

#

do you maybe have a second setUpTestData() method defined further down below your class/file?

mossy raven
#

no , like that's the whole code of class which I copy pasted above

dull dock
#

okay. did you by any chance change the database engine recently? what db engine are you running your tests against?

dreamy solstice
#

You should use TestCase from django.test right, not unittest

dull dock
#

ah jeez, nice catch

dreamy solstice
#

the setUpTestData method is a django thing, not a unittest thing

mossy raven
#

oo

dull dock
#

I totally did not see that

dreamy solstice
#

It helps if you never use django testcase and didn't know about a setUpTestData method and had to google it ๐Ÿ˜‰

mossy raven
#

yay! working fine ๐Ÿ›