Home > Net >  Django Test key association
Django Test key association

Time:12-20

I am new to Django and Unit Testing.

I'm writing tests for my app which is a learning platform consisting of several labs that include a lesson and questions (of 3 different types), the relationship between questions and labs is done via foreign key.

I am trying to write tests in which I add a lab to the database and a multiple choice question.

The problem is that the tests always end with the same error:

DETAIL: Key (lab_id)=(1) is not present in table "labs_lab".

I understand the meaning of the sentence, but how come this happens if I provide an id to the questions to link them to the respective test-lab?

Thank you

Models.py

class Lab(models.Model):
    lab_name = models.CharField(max_length=200)
    category = models.ForeignKey(Category, unique=True, null=True, on_delete=models.PROTECT)
    pub_date = models.DateTimeField('date published')
    lab_theory = models.TextField()
    def __str__(self):
        return self.lab_name

class QuestionMultipleChoice(models.Model):
    lab = models.ForeignKey(Lab, on_delete=models.CASCADE)
    type = QuestionType.multiplechoice
    question = models.CharField(max_length=200,null=True)
    option1 = models.CharField(max_length=200,null=True)
    option2 = models.CharField(max_length=200,null=True)
    option3 = models.CharField(max_length=200,null=True)
    option4 = models.CharField(max_length=200,null=True)
    answer = models.IntegerField(max_length=200,null=True)
    
    def __str__(self):
        return self.question

    @property
    def html_name(self):
        return "q_mc_{}".format(self.pk)

    @property
    def correct_answer(self):
        correct_answer_number = int(self.answer)
        correct_answer = getattr(self, "option{}".format(correct_answer_number))
        return correct_answer

    def check_answer(self, given):
        return self.correct_answer == given

test.py

class TestData(TestCase):
    @classmethod
    def setUpTestData(cls):
        past_date = date(1997, 3, 2)
        future_date = date(2050, 3, 2)
        Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")
        Lab.objects.create(lab_name="test lab past question", pub_date=past_date, lab_theory="test lab past question")
        Lab.objects.create(lab_name="test lab future question", pub_date=future_date, lab_theory="test lab future question")
        

class QuestionIndexViewTests(TestCase):
    def test_no_questions(self):
        """
        If no questions exist, an appropriate message is displayed.
        """
        response = self.client.get(reverse('labs:index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No labs are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'], [])

    def test_past_question(self):
        """
        Questions with a pub_date in the past are displayed on the
        index page.
        """
        past_question = QuestionMultipleChoice.objects.create(question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer='1', lab_id=2)
        response = self.client.get(reverse('labs:index'))
        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            [past_question],
        )

UPDATE:

Update: I changed the test, and i gave the "QuestionMultipleChoice" different parameters:


question1 = QuestionMultipleChoice.objects.create(lab=1, question='This is a test question', option1='1', option2='2', option3='3', option4='4', answer='1')        

Result now is:

ValueError: Cannot assign "1": "QuestionMultipleChoice.lab" must be a "Lab" instance.

CodePudding user response:

ValueError: Cannot assign "1": "QuestionMultipleChoice.lab" must be a "Lab" instance.

this means lab attribute trying to get Lab instance you can provide a lab instance to assign Lab instance you have to do like this

lab1 = Lab.objects.create(lab_name="testlab", pub_date=datetime.now(), lab_theory="test theory")

QuestionMultipleChoice.objects.create(question='This is a test question', lab=lab1)
  • Related