Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_rpy.py

Issue 29849585: Issue 6835 - Add R support to python-abp (Closed)
Patch Set: Created Aug. 7, 2018, 3:06 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« abp/filters/rpy.py ('K') | « abp/filters/rpy.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # This file is part of Adblock Plus <https://adblockplus.org/>,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 """Functional tests for testing rPython integration."""
17 from __future__ import unicode_literals
18
19 from collections import namedtuple
20 import pytest
21
22 from abp.filters.rpy import (
23 tuple2dict, line2dict,
24 )
25
26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar')
27
28 _ENCODED_STR_TYPE = type(''.encode('utf-8'))
29
30 _EXPECTED_TYPES = {
31 'header': {
32 'type': _ENCODED_STR_TYPE,
33 'version': _ENCODED_STR_TYPE,
34 },
35 'metadata': {
36 'type': _ENCODED_STR_TYPE,
37 'key': _ENCODED_STR_TYPE,
38 'value': _ENCODED_STR_TYPE,
39 },
40 'empty': {
41 'type': _ENCODED_STR_TYPE,
42 },
43 'comment': {
44 'type': _ENCODED_STR_TYPE,
45 'text': _ENCODED_STR_TYPE,
46 },
47 'include': {
48 'type': _ENCODED_STR_TYPE,
49 'target': _ENCODED_STR_TYPE,
50 },
51 'filter': {
52 'type': _ENCODED_STR_TYPE,
53 'text': _ENCODED_STR_TYPE,
54 'selector': dict,
55 'action': _ENCODED_STR_TYPE,
56 'options': list,
57 },
58 }
59
60
61 def check_correct_datatypes(data, expected_types):
Vasily Kuznetsov 2018/08/08 14:32:50 What do you think about checking generated dicts a
Tudor Avram 2018/08/09 12:55:02 Done.
62 """Check if the resulting data has the correct keys and datatypes.
63
64 Parameters
65 ----------
66 data: dict
67 The dictonary produced by the API
68
69 expected_types: dict
70 The expected keys and datatypes associated with them.
71
72 Raises
73 -------
74 AssertionError
75 If any of the types/ keys don't correspond
76
77 """
78 def encode_fn(x):
79 return x.encode('utf-8')
80
81 data_keys = sorted(list(data.keys()))
82 expected_keys = sorted(list(map(encode_fn, expected_types.keys())))
83
84 if data_keys != expected_keys:
85 raise AssertionError('Invalid dict format. Got keys {0}, '
86 'expected {1}'.format(data_keys, expected_keys))
87
88 for key in data:
89 if not isinstance(data[key], expected_types[key.decode('utf-8')]):
90 raise AssertionError(
91 'Invalid dict format. {0} should be {1}. Got {2}'.format(
92 key,
93 expected_types[key.decode('utf-8')],
94 type(data[key]),
95 ),
96 )
97
98
99 def check_data_utf8(data):
100 """Check if all the strings in a dict are encoded as unicode.
101
102 Parameters
103 ----------
104 data: dict
105 The dictionary to be checked
106
107 Raises
108 -------
109 AssertionError
110 If any of the strings encountered are not unicode
111
112 """
113 if isinstance(data, dict):
114 for key, value in data.items():
115 check_data_utf8(key)
116 check_data_utf8(value)
117 elif isinstance(data, (list, tuple)):
118 for item in data:
119 check_data_utf8(item)
120 elif isinstance(data, type('')):
121 raise AssertionError('{} is str. Expected bytes.'.format(data))
122
123
124 @pytest.mark.parametrize('foo,bar', [
125 ('test_foo', 1),
126 ({'foofoo': 'test', 'foobar': 2}, [1, 2, 3]),
127 ((1,), [('a', True), ('b', False)]),
128 ])
129 def test_tuple2dict(foo, bar):
130 """Test that dicts are produced correctly from a named tuple."""
131 data = _SAMPLE_TUPLE(foo=foo, bar=bar)
132 exp = {'foo': foo, 'bar': bar, 'type': 'tuple'}
133
134 result = tuple2dict(data)
135
136 assert exp == result
137
138
139 @pytest.mark.parametrize('filter_text', [
140 'abc$image',
141 '\u0432\u0435\u0431\u0441\u0430\u0439.\u0440\u0444$domain=\xfcber.de',
142 ])
143 def test_line2dict_encoding(filter_text):
144 """Test that the resulting object has all strings encoded as utf-8."""
145 data = line2dict(filter_text.encode('utf-8'))
146 check_data_utf8(data)
147
148
149 @pytest.mark.parametrize('line,expected', [
150 ('[Adblock Plus 2.0]', _EXPECTED_TYPES['header']),
151 ('! Title: Example list', _EXPECTED_TYPES['metadata']),
152 ('! Comment', _EXPECTED_TYPES['comment']),
153 ('abc.com,cdf.com##div#ad1', _EXPECTED_TYPES['filter']),
154 ('%include www.test.py/filtelist.txt%', _EXPECTED_TYPES['include']),
155 ('', _EXPECTED_TYPES['empty']),
156 ])
157 def test_line2dict_format(line, expected):
158 """Test that the API result has the appropriate format.
159
160 Checks for both keys and datatypes.
161 """
162 data = line2dict(line)
163 check_correct_datatypes(data, expected)
OLDNEW
« abp/filters/rpy.py ('K') | « abp/filters/rpy.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld