def test_find_config_item_by_filepath(basic_site_config): """SiteConfig.find_item_by_filepath should return a config item if one is found with the given filepath""" site_config = SiteConfig(basic_site_config) all_config_items = list(site_config.iter_items()) assert (site_config.find_item_by_filepath("data/metadata.json") == all_config_items[3]) assert site_config.find_item_by_filepath("bad/path") is None
def apply_rule(data): faulty_path_tuples = {} site_config = SiteConfig(data) for _, config_item in enumerate(site_config.iter_items()): non_menu_fields, menu_fields = partition_to_lists( config_item.fields, predicate=lambda field: field["widget"] == CONTENT_MENU_FIELD, ) if not menu_fields: continue if non_menu_fields: faulty_path_tuples[config_item.name] = ( config_item.path, ", ".join([field["widget"] for field in non_menu_fields]), ) if faulty_path_tuples: return [ "Config with 'menu' fields must not have any fields with other widget types.\n{}".format( "\n".join( [ f"{' ' * 8}'{name}' ({path_fields_tuple[0]}) – widgets: {path_fields_tuple[1]}" for name, path_fields_tuple in faulty_path_tuples.items() ] ), ) ] return []
def test_data_file_deserialize(serializer_cls, file_content): """ JsonFileSerializer and YamlFileSerializer.deserialize should create the expected content object from some data file contents """ website = WebsiteFactory.create() site_config = SiteConfig(website.starter.config) file_config_item = next( config_item for config_item in site_config.iter_items() if "file" in config_item.item ) filepath = file_config_item.item["file"] website_content = serializer_cls(site_config).deserialize( website=website, filepath=filepath, file_contents=file_content, ) assert website_content.title == "Content Title" assert website_content.type == file_config_item.item["name"] assert website_content.text_id == file_config_item.item["name"] assert website_content.is_page_content is False assert website_content.metadata == { "tags": ["Design"], "description": "**This** is the description", }
def test_find_config_item_name_singleton(basic_site_config): """SiteConfig.find_item_by_name should return a singleton config item if one is found with the given name""" site_config = SiteConfig(basic_site_config) config_item = next(item for item in site_config.iter_items() if item.is_file_item()) assert config_item is not None assert site_config.find_item_by_name(config_item.name) == config_item assert site_config.find_item_by_name("other-name-123") is None
def test_find_file_field(basic_site_config, content_type, field_name): """The expected file field should be returned if any""" site_config = SiteConfig(basic_site_config) config_item = next( (item for item in site_config.iter_items() if item.name == content_type), None) file_field = site_config.find_file_field(config_item) if field_name: assert file_field["name"] == "image" else: assert file_field is None
def test_is_page_content(basic_site_config, content_dir, folder_file_target, exp_result): """ SiteConfig.is_page_content should return True if the folder target of the repeatable config item starts with the content directory in the site config (or a default value) """ site_config = SiteConfig(basic_site_config) site_config.raw_data[WEBSITE_CONFIG_CONTENT_DIR_KEY] = content_dir config_item = next(item for item in site_config.iter_items() if item.is_folder_item()) config_item.item["folder"] = folder_file_target assert site_config.is_page_content(config_item) is exp_result
def test_site_config_iter_items(basic_site_config): """SiteConfig.iter_items should yield each individual config item""" site_config = SiteConfig(basic_site_config) config_items = list(site_config.iter_items()) assert len(config_items) == 5 collections = basic_site_config["collections"] assert config_items[0] == ConfigItem(item=collections[0], parent_item=None, path="collections.0") assert config_items[1] == ConfigItem(item=collections[1], parent_item=None, path="collections.1") assert config_items[2] == ConfigItem(item=collections[2], parent_item=None, path="collections.2") assert config_items[3] == ConfigItem( item=collections[2]["files"][0], parent_item=collections[2], path="collections.2.files.0", )
def apply_rule(data): faulty_paths = {} site_config = SiteConfig(data) for _, config_item in enumerate(site_config.iter_items()): if config_item.is_folder_item() and not site_config.is_page_content( config_item ): faulty_paths[config_item.name] = config_item.path if faulty_paths: return [ "Found 'folder' item(s) that do not point to the content directory ({}).\n{}".format( site_config.content_dir, "\n".join( [ f"{' ' * 8}'{name}' ({path})" for name, path in faulty_paths.items() ] ), ) ] return []
def apply_rule(data): faulty_paths = {} site_config = SiteConfig(data) for _, config_item in enumerate(site_config.iter_items()): title_field = first_or_none( [field for field in config_item.fields if field["name"] == "title"] ) if title_field is not None and ( title_field.get("required", False) is False or title_field.get("widget", "string") != "string" ): faulty_paths[config_item.name] = config_item.path if faulty_paths: return [ "'title' fields must use the 'string' widget, and must be set to be required.\n{}".format( "\n".join( [ f"{' ' * 8}'{name}' ({path})" for name, path in faulty_paths.items() ] ), ) ] return []