1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
import unittest
from recipe_scrapers._schemaorg import SchemaOrg
JSONLD_PAGE_TEMPLATE = """
<html>
<head>
<link href="http://recipe.test/template" type="canonical" />
<script type="application/ld+json">{jsonld}</script>
</head>
</html>
"""
SIMPLE_SCHEMA = """
{
"@context": "https://schema.org",
"@type": "Recipe",
"name": "Test Recipe",
"recipeIngredient": ["1 slice of bread", "5g margarine"],
"recipeInstructions": ["spread the margarine on the bread"]
}
"""
MULTI_ENTITY_SCHEMA = """
[
{
"@context": "https://schema.org",
"@type": "Recipe",
"@id": "http://recipe.test/template",
"name": "Test Recipe",
},
{
"@context": "https://schema.org",
"@type": "Recipe",
"@id": "http://recipe.test/other",
"name": "Another great test recipe",
},
{
"@context": "https://schema.org",
"@type": "WebPage",
"@id": "http://recipe.test/template",
"mainEntity": {
"@context": "https://schema.org",
"@type": "Recipe",
"@id": "http://recipe.test/template",
"recipeIngredient": ["1 slice of bread", "5g margarine"],
"recipeInstructions": ["spread the margarine on the bread"]
}
}
]
"""
class TestSchemaOrg(unittest.TestCase):
def test_simple(self):
page_data = JSONLD_PAGE_TEMPLATE.format(jsonld=SIMPLE_SCHEMA)
parser = SchemaOrg(page_data)
self.assertEqual("Test Recipe", parser.title())
self.assertIn("1 slice of bread", parser.ingredients())
self.assertIn("5g margarine", parser.ingredients())
self.assertEqual("spread the margarine on the bread", parser.instructions())
def test_multi_entity_aggregation(self):
page_data = JSONLD_PAGE_TEMPLATE.format(jsonld=MULTI_ENTITY_SCHEMA)
parser = SchemaOrg(page_data)
self.assertEqual("Test Recipe", parser.title())
self.assertIn("1 slice of bread", parser.ingredients())
self.assertIn("5g margarine", parser.ingredients())
self.assertEqual("spread the margarine on the bread", parser.instructions())
|