def test_yaml_dict_conversion(self) -> None: """Test yaml_from_dict and dict_from_yaml methods.""" test_dicts = [{}, {'a': 'b'}, {'a': 2}, {'a': ['b', 2, {'c': 3.5}]}] for adict in test_dicts: yaml_str = python_utils.yaml_from_dict( adict) # type: ignore[no-untyped-call] yaml_dict = utils.dict_from_yaml(yaml_str) self.assertEqual(adict, yaml_dict) with self.assertRaisesRegexp( # type: ignore[no-untyped-call] utils.InvalidInputException, 'while parsing a flow node\n' 'expected the node content, but found \'<stream end>\'\n'): yaml_str = utils.dict_from_yaml('{')
def _migrate_to_latest_yaml_version(cls, yaml_content): """Return the YAML content of the collection in the latest schema format. Args: yaml_content: str. The YAML representation of the collection. Returns: str. The YAML representation of the collection, in the latest schema format. Raises: InvalidInputException. The 'yaml_content' or the schema version is not specified. Exception. The collection schema version is not valid. """ try: collection_dict = utils.dict_from_yaml(yaml_content) except utils.InvalidInputException as e: raise utils.InvalidInputException( 'Please ensure that you are uploading a YAML text file, not ' 'a zip file. The YAML parser returned the following error: %s' % e) collection_schema_version = collection_dict.get('schema_version') if collection_schema_version is None: raise utils.InvalidInputException( 'Invalid YAML file: no schema version specified.') if not (1 <= collection_schema_version <= feconf.CURRENT_COLLECTION_SCHEMA_VERSION): raise Exception( 'Sorry, we can only process v1 to v%s collection YAML files at ' 'present.' % feconf.CURRENT_COLLECTION_SCHEMA_VERSION) while (collection_schema_version < feconf.CURRENT_COLLECTION_SCHEMA_VERSION): conversion_fn = getattr( cls, '_convert_v%s_dict_to_v%s_dict' % ( collection_schema_version, collection_schema_version + 1)) collection_dict = conversion_fn(collection_dict) collection_schema_version += 1 return collection_dict