|
1 | 1 | import dkg.utils.knowledge_collection_tools as kc_tools |
| 2 | +import uuid |
2 | 3 |
|
3 | 4 |
|
4 | 5 | class TestGroupNQuadsBySubject: |
5 | 6 | def test_resource_object_quad(self): |
| 7 | + # JSON-LD equivalent of the quads being tested: |
| 8 | + # |
| 9 | + # { |
| 10 | + # "@context": "http://schema.org", |
| 11 | + # "@id": "http://example.org/book1", |
| 12 | + # "author": { |
| 13 | + # "@id": "http://example.org/author1" |
| 14 | + # } |
| 15 | + # } |
6 | 16 | quads = [ |
7 | 17 | "<http://example.org/book1> <http://schema.org/author> <http://example.org/author1> .", |
8 | 18 | "<http://example.org/book1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .", |
9 | 19 | ] |
10 | 20 | grouped = kc_tools.group_nquads_by_subject(quads) |
11 | 21 | assert len(grouped) == 1 |
| 22 | + print(grouped[0][0]) |
| 23 | + print(quads[0]) |
12 | 24 | assert quads[0] in grouped[0] |
13 | 25 | assert quads[1] in grouped[0] |
| 26 | + |
| 27 | + def test_literal_object_quad(self): |
| 28 | + # JSON-LD equivalent of the quads being tested: |
| 29 | + # |
| 30 | + # { |
| 31 | + # "@context": "http://schema.org", |
| 32 | + # "@id": "http://example.org/book1", |
| 33 | + # "type": "Book", |
| 34 | + # "title": "The Great Book" |
| 35 | + # } |
| 36 | + quads = [ |
| 37 | + '<http://example.org/book1> <http://schema.org/title> "The Great Book" .', |
| 38 | + "<http://example.org/book1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .", |
| 39 | + ] |
| 40 | + grouped = kc_tools.group_nquads_by_subject(quads) |
| 41 | + assert len(grouped) == 1 |
| 42 | + assert quads[0] in grouped[0] |
| 43 | + assert quads[1] in grouped[0] |
| 44 | + |
| 45 | + def test_literal_with_escape_character(self): |
| 46 | + # JSON-LD equivalent of the quads being tested: |
| 47 | + # |
| 48 | + # { |
| 49 | + # "@context": "http://schema.org", |
| 50 | + # "@id": "http://example.org/book1", |
| 51 | + # "type": "Book", |
| 52 | + # "title": "The Great Book \n" |
| 53 | + # } |
| 54 | + quads = [ |
| 55 | + '<http://example.org/book1> <http://schema.org/title> "The Great Book \\n" .', |
| 56 | + "<http://example.org/book1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .", |
| 57 | + ] |
| 58 | + grouped = kc_tools.group_nquads_by_subject(quads) |
| 59 | + assert len(grouped) == 1 |
| 60 | + assert quads[0] in grouped[0] |
| 61 | + assert quads[1] in grouped[0] |
| 62 | + |
| 63 | + def test_literal_with_language_tags(self): |
| 64 | + # JSON-LD equivalent of the quads being tested: |
| 65 | + # |
| 66 | + # { |
| 67 | + # "@context": "http://schema.org", |
| 68 | + # "@id": "http://example.org/book1", |
| 69 | + # "type": "Book", |
| 70 | + # "description": [ |
| 71 | + # { |
| 72 | + # "@value": "A thrilling adventure novel.", |
| 73 | + # "@language": "en" |
| 74 | + # }, |
| 75 | + # { |
| 76 | + # "@value": "Napeta pustolovska novela.", |
| 77 | + # "@language": "sl" |
| 78 | + # } |
| 79 | + # ] |
| 80 | + # } |
| 81 | + quads = [ |
| 82 | + '<http://example.org/book1> <http://schema.org/description> "A thrilling adventure novel."@en .', |
| 83 | + '<http://example.org/book1> <http://schema.org/description> "Napeta pustolovska novela."@sl .', |
| 84 | + "<http://example.org/book1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .", |
| 85 | + ] |
| 86 | + grouped = kc_tools.group_nquads_by_subject(quads) |
| 87 | + assert len(grouped) == 1 |
| 88 | + assert quads[0] in grouped[0] |
| 89 | + assert quads[1] in grouped[0] |
| 90 | + assert quads[2] in grouped[0] |
| 91 | + |
| 92 | + def test_literal_with_language_and_escape_character(self): |
| 93 | + # JSON-LD equivalent of the quads being tested: |
| 94 | + # |
| 95 | + # { |
| 96 | + # "@context": "http://schema.org", |
| 97 | + # "@id": "http://example.org/book1", |
| 98 | + # "type": "Book", |
| 99 | + # "description": [ |
| 100 | + # { |
| 101 | + # "@value": "A thrilling adventure novel. \n", |
| 102 | + # "@language": "en" |
| 103 | + # }, |
| 104 | + # { |
| 105 | + # "@value": "Napeta pustolovska novela. \n", |
| 106 | + # "@language": "sl" |
| 107 | + # } |
| 108 | + # ] |
| 109 | + # } |
| 110 | + quads = [ |
| 111 | + '<http://example.org/book1> <http://schema.org/description> "A thrilling adventure novel. \n"@en .', |
| 112 | + '<http://example.org/book1> <http://schema.org/description> "Napeta pustolovska novela. \n"@sl .', |
| 113 | + "<http://example.org/book1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Book> .", |
| 114 | + ] |
| 115 | + grouped = kc_tools.group_nquads_by_subject(quads) |
| 116 | + assert len(grouped) == 1 |
| 117 | + assert quads[0] in [nquad.encode("unicode_escape") for nquad in grouped[0]] |
| 118 | + assert quads[1] in grouped[0] |
| 119 | + assert quads[2] in grouped[0] |
| 120 | + |
| 121 | + def test_literal_with_language_and_blank_node_subject(self): |
| 122 | + # JSON-LD equivalent of the quads being tested: |
| 123 | + # |
| 124 | + # { |
| 125 | + # "@context": { |
| 126 | + # "predicate": "http://example.org/predicate" |
| 127 | + # }, |
| 128 | + # "@graph": [ |
| 129 | + # { |
| 130 | + # "predicate": { |
| 131 | + # "@value": "something", |
| 132 | + # "@language": "en" |
| 133 | + # } |
| 134 | + # } |
| 135 | + # ] |
| 136 | + # } |
| 137 | + |
| 138 | + subject = f"<uuid:{uuid.uuid4()}>" |
| 139 | + quads = [ |
| 140 | + f'{subject} <http://example.org/predicate> "something"@en .', |
| 141 | + ] |
| 142 | + grouped = kc_tools.group_nquads_by_subject(quads) |
| 143 | + assert len(grouped) == 1 |
| 144 | + assert quads[0] in grouped[0] |
0 commit comments