|
| 1 | +# Copyright 2017 Bloomberg Finance L.P. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +""" |
| 13 | +This test covers the cases where csv file and metadata works with url safe characters like #, /, : |
| 14 | +""" |
| 15 | + |
| 16 | +from rdflib import ConjunctiveGraph, Literal, URIRef, Namespace |
| 17 | + |
| 18 | +from pycsvw import CSVW |
| 19 | + |
| 20 | +PRE_NS = Namespace('http://www.example.org/') |
| 21 | + |
| 22 | + |
| 23 | +def test_url_safe_chars(): |
| 24 | + |
| 25 | + csvw = CSVW(csv_path="tests/url_special_chars.csv", |
| 26 | + metadata_path="tests/url_special_chars.csv-metadata.json") |
| 27 | + rdf_output = csvw.to_rdf() |
| 28 | + |
| 29 | + g = ConjunctiveGraph() |
| 30 | + g.parse(data=rdf_output, format="turtle") |
| 31 | + |
| 32 | + # Check subjects |
| 33 | + sub1 = URIRef('http://www.example.org/c#1/chash2/chash3/chash4/chash5/chash6') |
| 34 | + literals = [Literal('c#1'), Literal('chash2'), Literal('chash3'), |
| 35 | + Literal('chash4'), Literal('chash6'), Literal('chash5')] |
| 36 | + verify_non_virtual_columns(sub1, g, literals) |
| 37 | + verify_virtual_columns(sub1, g, '#/:- _r1', '#/:-%20_r1') |
| 38 | + |
| 39 | + sub2 = URIRef('http://www.example.org/c/1/c/2/c/3/c/4/c/5/c/6') |
| 40 | + literals = [Literal('c/1'), Literal('c/2'), Literal('c/3'), |
| 41 | + Literal('c/4'), Literal('c/6'), Literal('c/5')] |
| 42 | + verify_non_virtual_columns(sub2, g, literals) |
| 43 | + verify_virtual_columns(sub2, g, '/#:- _r2','/#:-%20_r2') |
| 44 | + |
| 45 | + sub3 = URIRef('http://www.example.org/c:1/c:2/c:3/c:4/c:5/c:6') |
| 46 | + literals = [Literal('c:1'), Literal('c:2'), Literal('c:3'), |
| 47 | + Literal('c:4'), Literal('c:6'), Literal('c:5')] |
| 48 | + verify_non_virtual_columns(sub3, g, literals) |
| 49 | + verify_virtual_columns(sub3, g, ':#/-_ r3', ':#/-_%20r3') |
| 50 | + |
| 51 | + sub4 = URIRef('http://www.example.org/c-1/c-2/c-3/c-4/c-5/c-6') |
| 52 | + literals = [Literal('c-1'), Literal('c-2'), Literal('c-3'), |
| 53 | + Literal('c-4'), Literal('c-6'), Literal('c-5')] |
| 54 | + verify_non_virtual_columns(sub4, g, literals) |
| 55 | + verify_virtual_columns(sub4, g, '-/#_ :r4', '-/#_%20:r4') |
| 56 | + |
| 57 | + sub5 = URIRef('http://www.example.org/c%201/c%202/c%203/c%204/c%205/c%206') |
| 58 | + literals = [Literal('c 1'), Literal('c 2'), Literal('c 3'), |
| 59 | + Literal('c 4'), Literal('c 6'), Literal('c 5')] |
| 60 | + verify_non_virtual_columns(sub5, g, literals) |
| 61 | + verify_virtual_columns(sub5, g, ' -/#:_r5', '%20-/#:_r5') |
| 62 | + |
| 63 | + sub6 = URIRef('http://www.example.org/c_1/c_2/c_3/c_4/c_5/c_6') |
| 64 | + literals = [Literal('c_1'), Literal('c_2'), Literal('c_3'), |
| 65 | + Literal('c_4'), Literal('c_6'), Literal('c_5')] |
| 66 | + verify_non_virtual_columns(sub6, g, literals) |
| 67 | + verify_virtual_columns(sub6, g, '_ /:#r6', '_%20/:#r6') |
| 68 | + |
| 69 | + |
| 70 | +def verify_virtual_columns(sub, g, orig_value_str, encoded_value_str): |
| 71 | + v1_triples = list(g.triples((sub, PRE_NS['v1p{}'.format(encoded_value_str)], None))) |
| 72 | + assert len(v1_triples) == 1 |
| 73 | + assert "v1p{}".format(encoded_value_str) in str(v1_triples[0][1]) |
| 74 | + assert orig_value_str == str(v1_triples[0][2]) |
| 75 | + v2_triples = list(g.triples((sub, PRE_NS['v2p{}'.format(encoded_value_str)], None))) |
| 76 | + assert len(v2_triples) == 1 |
| 77 | + assert "v2p{}".format(encoded_value_str) in str(v2_triples[0][1]) |
| 78 | + assert 'v2v{}'.format(encoded_value_str) in str(v2_triples[0][2]) |
| 79 | + |
| 80 | + # Standalone virtual column |
| 81 | + standalone_sub = URIRef('http://www.example.org/v3s{}'.format(encoded_value_str)) |
| 82 | + v3_triples = list(g.triples((standalone_sub, None, None))) |
| 83 | + assert len(v3_triples) == 1 |
| 84 | + assert "v3p{}".format(encoded_value_str) in str(v3_triples[0][1]) |
| 85 | + assert 'v3v{}'.format(encoded_value_str) in str(v3_triples[0][2]) |
| 86 | + |
| 87 | + |
| 88 | +def verify_non_virtual_columns(sub, g, literals): |
| 89 | + all_triples = list(g.triples((sub, None, None))) |
| 90 | + assert len(all_triples) == 9 |
| 91 | + assert (sub, PRE_NS['t#p'], literals[0]) in all_triples |
| 92 | + assert (sub, PRE_NS['t/p'], literals[1]) in all_triples |
| 93 | + assert (sub, PRE_NS['t:p'], literals[2]) in all_triples |
| 94 | + assert (sub, PRE_NS['t-p'], literals[3]) in all_triples |
| 95 | + assert (sub, PRE_NS['t_p'], literals[4]) in all_triples |
| 96 | + # Space value |
| 97 | + space_triple = list(g.triples((sub, PRE_NS['t%20p'], None))) |
| 98 | + assert len(space_triple) == 1 |
| 99 | + assert "%20" in str(space_triple[0][1]) |
| 100 | + assert " " not in str(space_triple[0][1]) |
| 101 | + assert literals[5] == space_triple[0][2] |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
0 commit comments