Esempio n. 1
0
    def test_cannot_get_topic_from_model_with_invalid_schema_version(
            self) -> None:
        topic_services.create_new_topic_rights(
            'topic_id', self.user_id_a)  # type: ignore[no-untyped-call]
        commit_cmd = topic_domain.TopicChange({
            'cmd': topic_domain.CMD_CREATE_NEW,
            'name': 'name'
        })
        subtopic_dict = {'id': 1, 'title': 'subtopic_title', 'skill_ids': []}
        model = topic_models.TopicModel(id='topic_id',
                                        name='name',
                                        abbreviated_name='abbrev',
                                        url_fragment='name-two',
                                        canonical_name='canonical_name',
                                        description='description',
                                        next_subtopic_id=1,
                                        language_code='en',
                                        subtopics=[subtopic_dict],
                                        subtopic_schema_version=0,
                                        story_reference_schema_version=0,
                                        page_title_fragment_for_web='fragm')
        commit_cmd_dicts = [commit_cmd.to_dict()]
        model.commit(self.user_id_a, 'topic model created', commit_cmd_dicts)

        with self.assertRaisesRegex(  # type: ignore[no-untyped-call]
                Exception,
                'Sorry, we can only process v1-v%d subtopic schemas at '
                'present.' % feconf.CURRENT_SUBTOPIC_SCHEMA_VERSION):
            topic_fetchers.get_topic_from_model(model)

        topic_services.create_new_topic_rights(
            'topic_id_2', self.user_id_a)  # type: ignore[no-untyped-call]
        model = topic_models.TopicModel(id='topic_id_2',
                                        name='name 2',
                                        description='description 2',
                                        abbreviated_name='abbrev',
                                        url_fragment='name-three',
                                        canonical_name='canonical_name_2',
                                        next_subtopic_id=1,
                                        language_code='en',
                                        subtopics=[subtopic_dict],
                                        subtopic_schema_version=1,
                                        story_reference_schema_version=0,
                                        page_title_fragment_for_web='fragm')
        commit_cmd_dicts = [commit_cmd.to_dict()]
        model.commit(self.user_id_a, 'topic model created', commit_cmd_dicts)

        with self.assertRaisesRegex(  # type: ignore[no-untyped-call]
                Exception,
                'Sorry, we can only process v1-v%d story reference schemas at '
                'present.' % feconf.CURRENT_STORY_REFERENCE_SCHEMA_VERSION):
            topic_fetchers.get_topic_from_model(model)
Esempio n. 2
0
 def test_topic_model_migration_to_higher_version(self) -> None:
     topic_services.create_new_topic_rights(
         'topic_id', self.user_id_a)  # type: ignore[no-untyped-call]
     commit_cmd = topic_domain.TopicChange({
         'cmd': topic_domain.CMD_CREATE_NEW,
         'name': 'name'
     })
     subtopic_v1_dict = {
         'id': 1,
         'title': 'subtopic_title',
         'skill_ids': []
     }
     model = topic_models.TopicModel(id='topic_id',
                                     name='name 2',
                                     description='description 2',
                                     abbreviated_name='abbrev',
                                     url_fragment='name-three',
                                     canonical_name='canonical_name_2',
                                     next_subtopic_id=1,
                                     language_code='en',
                                     subtopics=[subtopic_v1_dict],
                                     subtopic_schema_version=1,
                                     story_reference_schema_version=1,
                                     page_title_fragment_for_web='fragment')
     commit_cmd_dicts = [commit_cmd.to_dict()]
     model.commit(self.user_id_a, 'topic model created', commit_cmd_dicts)
     swap_topic_object = self.swap(topic_domain, 'Topic', MockTopicObject)
     current_story_refrence_schema_version_swap = self.swap(
         feconf, 'CURRENT_STORY_REFERENCE_SCHEMA_VERSION', 2)
     with swap_topic_object, current_story_refrence_schema_version_swap:
         topic: topic_domain.Topic = (
             topic_fetchers.get_topic_from_model(model))
         self.assertEqual(topic.story_reference_schema_version, 2)
    def test_message(self) -> None:
        model = topic_models.TopicModel(id='test',
                                        name='name',
                                        url_fragment='name-two',
                                        canonical_name='canonical_name',
                                        next_subtopic_id=1,
                                        language_code='en',
                                        subtopic_schema_version=0,
                                        story_reference_schema_version=0)
        error = topic_validation_errors.ModelCanonicalNameMismatchError(model)

        self.assertEqual(
            error.stderr,
            'ModelCanonicalNameMismatchError in TopicModel(id="test"): '
            'Entity name %s in lowercase does not match canonical name %s' %
            (model.name, model.canonical_name))
Esempio n. 4
0
 def test_process_for_matching_canonical_name(self) -> None:
     model_with_same_name = topic_models.TopicModel(
         id='123',
         name='SOMEthing',
         created_on=self.NOW,
         last_updated=self.NOW,
         url_fragment='name-two',
         canonical_name='something',
         next_subtopic_id=1,
         language_code='en',
         subtopic_schema_version=0,
         story_reference_schema_version=0)
     output = (self.pipeline
               | beam.Create([model_with_same_name])
               | beam.ParDo(topic_validation.
                            ValidateCanonicalNameMatchesNameInLowercase()))
     self.assert_pcoll_equal(output, [])
Esempio n. 5
0
    def test_that_subsidiary_models_are_created_when_new_model_is_saved(
            self
    ) -> None:
        """Tests the _trusted_commit() method."""

        topic_rights = topic_models.TopicRightsModel(
            id=self.TOPIC_ID,
            manager_ids=[],
            topic_is_published=True
        )
        # Topic is created but not committed/saved.
        topic = topic_models.TopicModel(
            id=self.TOPIC_ID,
            name=self.TOPIC_NAME,
            abbreviated_name='abbrev',
            url_fragment='url-fragment',
            description='description',
            canonical_name=self.TOPIC_CANONICAL_NAME,
            subtopic_schema_version=feconf.CURRENT_SUBTOPIC_SCHEMA_VERSION,
            story_reference_schema_version=(
                feconf.CURRENT_STORY_REFERENCE_SCHEMA_VERSION),
            next_subtopic_id=1,
            language_code='en'
        )
        # We check that topic has not been saved before calling commit().
        self.assertIsNone(topic_models.TopicModel.get_by_name(self.TOPIC_NAME))
        # We call commit() expecting that _trusted_commit works fine
        # and saves topic to datastore.
        topic_rights.commit(
            committer_id=feconf.SYSTEM_COMMITTER_ID,
            commit_message='Created new topic rights',
            commit_cmds=[{'cmd': topic_domain.CMD_CREATE_NEW}]
        )
        topic.commit(
            committer_id=feconf.SYSTEM_COMMITTER_ID,
            commit_message='Created new topic',
            commit_cmds=[{'cmd': topic_domain.CMD_CREATE_NEW}]
        )
        # Now we check that topic is not None and that actually
        # now topic exists, that means that commit() worked fine.
        self.assertIsNotNone(
            topic_models.TopicModel.get_by_name(self.TOPIC_NAME)
        )