def test_update_right_expected_version(self):
        q = Questionnaire(resource=RESOURCE_1)
        with FakeClock(TIME):
            self.dao.insert(q)

        q = Questionnaire(questionnaireId=1, version=1, resource=RESOURCE_2)
        with FakeClock(TIME_2):
            self.dao.update(q)

        expected_questionnaire = Questionnaire(questionnaireId=1,
                                               version=2,
                                               created=TIME,
                                               lastModified=TIME_2,
                                               resource=RESOURCE_2_WITH_ID)
        questionnaire = self.dao.get(1)
        self.assertEquals(expected_questionnaire.asdict(),
                          questionnaire.asdict())
    def test_insert(self):
        q = Questionnaire(resource=RESOURCE_1)
        q.concepts.append(self.CONCEPT_1)
        q.concepts.append(self.CONCEPT_2)
        q.questions.append(self.QUESTION_1)
        q.questions.append(self.QUESTION_2)

        with FakeClock(TIME):
            self.dao.insert(q)

        # Creating a questionnaire creates a history entry with children
        self.check_history()

        expected_questionnaire = Questionnaire(questionnaireId=1,
                                               version=1,
                                               created=TIME,
                                               lastModified=TIME,
                                               resource=RESOURCE_1_WITH_ID)
        questionnaire = self.dao.get(1)
        self.assertEquals(expected_questionnaire.asdict(),
                          questionnaire.asdict())

        expected_questionnaire.concepts.append(EXPECTED_CONCEPT_1)
        expected_questionnaire.concepts.append(EXPECTED_CONCEPT_2)
        expected_questionnaire.questions.append(EXPECTED_QUESTION_1)
        expected_questionnaire.questions.append(EXPECTED_QUESTION_2)

        questionnaire = self.dao.get_with_children(1)

        self.assertEquals(
            sort_lists(expected_questionnaire.asdict_with_children()),
            sort_lists(questionnaire.asdict_with_children()))
        self.assertEquals(
            questionnaire.asdict(),
            self.dao.get_latest_questionnaire_with_concept(
                self.CODE_1.codeId).asdict())