You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

496 lines
20 KiB

10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. # Various small unit tests
  10. import io
  11. import json
  12. import xml.etree.ElementTree
  13. from youtube_dl.utils import (
  14. age_restricted,
  15. args_to_str,
  16. clean_html,
  17. DateRange,
  18. detect_exe_version,
  19. encodeFilename,
  20. escape_rfc3986,
  21. escape_url,
  22. find_xpath_attr,
  23. fix_xml_ampersands,
  24. InAdvancePagedList,
  25. intlist_to_bytes,
  26. is_html,
  27. js_to_json,
  28. limit_length,
  29. OnDemandPagedList,
  30. orderedSet,
  31. parse_duration,
  32. parse_filesize,
  33. parse_iso8601,
  34. read_batch_urls,
  35. sanitize_filename,
  36. shell_quote,
  37. smuggle_url,
  38. str_to_int,
  39. strip_jsonp,
  40. struct_unpack,
  41. timeconvert,
  42. unescapeHTML,
  43. unified_strdate,
  44. unsmuggle_url,
  45. uppercase_escape,
  46. url_basename,
  47. urlencode_postdata,
  48. version_tuple,
  49. xpath_with_ns,
  50. render_table,
  51. match_str,
  52. )
  53. class TestUtil(unittest.TestCase):
  54. def test_timeconvert(self):
  55. self.assertTrue(timeconvert('') is None)
  56. self.assertTrue(timeconvert('bougrg') is None)
  57. def test_sanitize_filename(self):
  58. self.assertEqual(sanitize_filename('abc'), 'abc')
  59. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  60. self.assertEqual(sanitize_filename('123'), '123')
  61. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  62. self.assertFalse('/' in sanitize_filename('abc/de///'))
  63. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
  64. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
  65. self.assertEqual('yes no', sanitize_filename('yes? no'))
  66. self.assertEqual('this - that', sanitize_filename('this: that'))
  67. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  68. aumlaut = 'ä'
  69. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  70. tests = '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430'
  71. self.assertEqual(sanitize_filename(tests), tests)
  72. self.assertEqual(
  73. sanitize_filename('New World record at 0:12:34'),
  74. 'New World record at 0_12_34')
  75. forbidden = '"\0\\/'
  76. for fc in forbidden:
  77. for fbc in forbidden:
  78. self.assertTrue(fbc not in sanitize_filename(fc))
  79. def test_sanitize_filename_restricted(self):
  80. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  81. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  82. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  83. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  84. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  85. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  86. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  87. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  88. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  89. tests = 'a\xe4b\u4e2d\u56fd\u7684c'
  90. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  91. self.assertTrue(sanitize_filename('\xf6', restricted=True) != '') # No empty filename
  92. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  93. for fc in forbidden:
  94. for fbc in forbidden:
  95. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  96. # Handle a common case more neatly
  97. self.assertEqual(sanitize_filename('\u5927\u58f0\u5e26 - Song', restricted=True), 'Song')
  98. self.assertEqual(sanitize_filename('\u603b\u7edf: Speech', restricted=True), 'Speech')
  99. # .. but make sure the file name is never empty
  100. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  101. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  102. def test_sanitize_ids(self):
  103. self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  104. self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  105. self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  106. def test_ordered_set(self):
  107. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  108. self.assertEqual(orderedSet([]), [])
  109. self.assertEqual(orderedSet([1]), [1])
  110. # keep the list ordered
  111. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  112. def test_unescape_html(self):
  113. self.assertEqual(unescapeHTML('%20;'), '%20;')
  114. self.assertEqual(
  115. unescapeHTML('&eacute;'), 'é')
  116. def test_daterange(self):
  117. _20century = DateRange("19000101", "20000101")
  118. self.assertFalse("17890714" in _20century)
  119. _ac = DateRange("00010101")
  120. self.assertTrue("19690721" in _ac)
  121. _firstmilenium = DateRange(end="10000101")
  122. self.assertTrue("07110427" in _firstmilenium)
  123. def test_unified_dates(self):
  124. self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
  125. self.assertEqual(unified_strdate('8/7/2009'), '20090708')
  126. self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
  127. self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
  128. self.assertEqual(unified_strdate('1968 12 10'), '19681210')
  129. self.assertEqual(unified_strdate('1968-12-10'), '19681210')
  130. self.assertEqual(unified_strdate('28/01/2014 21:00:00 +0100'), '20140128')
  131. self.assertEqual(
  132. unified_strdate('11/26/2014 11:30:00 AM PST', day_first=False),
  133. '20141126')
  134. self.assertEqual(
  135. unified_strdate('2/2/2015 6:47:40 PM', day_first=False),
  136. '20150202')
  137. def test_find_xpath_attr(self):
  138. testxml = '''<root>
  139. <node/>
  140. <node x="a"/>
  141. <node x="a" y="c" />
  142. <node x="b" y="d" />
  143. </root>'''
  144. doc = xml.etree.ElementTree.fromstring(testxml)
  145. self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
  146. self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
  147. self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
  148. def test_xpath_with_ns(self):
  149. testxml = '''<root xmlns:media="http://example.com/">
  150. <media:song>
  151. <media:author>The Author</media:author>
  152. <url>http://server.com/download.mp3</url>
  153. </media:song>
  154. </root>'''
  155. doc = xml.etree.ElementTree.fromstring(testxml)
  156. find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
  157. self.assertTrue(find('media:song') is not None)
  158. self.assertEqual(find('media:song/media:author').text, 'The Author')
  159. self.assertEqual(find('media:song/url').text, 'http://server.com/download.mp3')
  160. def test_smuggle_url(self):
  161. data = {"ö": "ö", "abc": [3]}
  162. url = 'https://foo.bar/baz?x=y#a'
  163. smug_url = smuggle_url(url, data)
  164. unsmug_url, unsmug_data = unsmuggle_url(smug_url)
  165. self.assertEqual(url, unsmug_url)
  166. self.assertEqual(data, unsmug_data)
  167. res_url, res_data = unsmuggle_url(url)
  168. self.assertEqual(res_url, url)
  169. self.assertEqual(res_data, None)
  170. def test_shell_quote(self):
  171. args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')]
  172. self.assertEqual(shell_quote(args), """ffmpeg -i 'ñ€ß'"'"'.mp4'""")
  173. def test_str_to_int(self):
  174. self.assertEqual(str_to_int('123,456'), 123456)
  175. self.assertEqual(str_to_int('123.456'), 123456)
  176. def test_url_basename(self):
  177. self.assertEqual(url_basename('http://foo.de/'), '')
  178. self.assertEqual(url_basename('http://foo.de/bar/baz'), 'baz')
  179. self.assertEqual(url_basename('http://foo.de/bar/baz?x=y'), 'baz')
  180. self.assertEqual(url_basename('http://foo.de/bar/baz#x=y'), 'baz')
  181. self.assertEqual(url_basename('http://foo.de/bar/baz/'), 'baz')
  182. self.assertEqual(
  183. url_basename('http://media.w3.org/2010/05/sintel/trailer.mp4'),
  184. 'trailer.mp4')
  185. def test_parse_duration(self):
  186. self.assertEqual(parse_duration(None), None)
  187. self.assertEqual(parse_duration(False), None)
  188. self.assertEqual(parse_duration('invalid'), None)
  189. self.assertEqual(parse_duration('1'), 1)
  190. self.assertEqual(parse_duration('1337:12'), 80232)
  191. self.assertEqual(parse_duration('9:12:43'), 33163)
  192. self.assertEqual(parse_duration('12:00'), 720)
  193. self.assertEqual(parse_duration('00:01:01'), 61)
  194. self.assertEqual(parse_duration('x:y'), None)
  195. self.assertEqual(parse_duration('3h11m53s'), 11513)
  196. self.assertEqual(parse_duration('3h 11m 53s'), 11513)
  197. self.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513)
  198. self.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513)
  199. self.assertEqual(parse_duration('62m45s'), 3765)
  200. self.assertEqual(parse_duration('6m59s'), 419)
  201. self.assertEqual(parse_duration('49s'), 49)
  202. self.assertEqual(parse_duration('0h0m0s'), 0)
  203. self.assertEqual(parse_duration('0m0s'), 0)
  204. self.assertEqual(parse_duration('0s'), 0)
  205. self.assertEqual(parse_duration('01:02:03.05'), 3723.05)
  206. self.assertEqual(parse_duration('T30M38S'), 1838)
  207. self.assertEqual(parse_duration('5 s'), 5)
  208. self.assertEqual(parse_duration('3 min'), 180)
  209. self.assertEqual(parse_duration('2.5 hours'), 9000)
  210. self.assertEqual(parse_duration('02:03:04'), 7384)
  211. self.assertEqual(parse_duration('01:02:03:04'), 93784)
  212. def test_fix_xml_ampersands(self):
  213. self.assertEqual(
  214. fix_xml_ampersands('"&x=y&z=a'), '"&amp;x=y&amp;z=a')
  215. self.assertEqual(
  216. fix_xml_ampersands('"&amp;x=y&wrong;&z=a'),
  217. '"&amp;x=y&amp;wrong;&amp;z=a')
  218. self.assertEqual(
  219. fix_xml_ampersands('&amp;&apos;&gt;&lt;&quot;'),
  220. '&amp;&apos;&gt;&lt;&quot;')
  221. self.assertEqual(
  222. fix_xml_ampersands('&#1234;&#x1abC;'), '&#1234;&#x1abC;')
  223. self.assertEqual(fix_xml_ampersands('&#&#'), '&amp;#&amp;#')
  224. def test_paged_list(self):
  225. def testPL(size, pagesize, sliceargs, expected):
  226. def get_page(pagenum):
  227. firstid = pagenum * pagesize
  228. upto = min(size, pagenum * pagesize + pagesize)
  229. for i in range(firstid, upto):
  230. yield i
  231. pl = OnDemandPagedList(get_page, pagesize)
  232. got = pl.getslice(*sliceargs)
  233. self.assertEqual(got, expected)
  234. iapl = InAdvancePagedList(get_page, size // pagesize + 1, pagesize)
  235. got = iapl.getslice(*sliceargs)
  236. self.assertEqual(got, expected)
  237. testPL(5, 2, (), [0, 1, 2, 3, 4])
  238. testPL(5, 2, (1,), [1, 2, 3, 4])
  239. testPL(5, 2, (2,), [2, 3, 4])
  240. testPL(5, 2, (4,), [4])
  241. testPL(5, 2, (0, 3), [0, 1, 2])
  242. testPL(5, 2, (1, 4), [1, 2, 3])
  243. testPL(5, 2, (2, 99), [2, 3, 4])
  244. testPL(5, 2, (20, 99), [])
  245. def test_struct_unpack(self):
  246. self.assertEqual(struct_unpack('!B', b'\x00'), (0,))
  247. def test_read_batch_urls(self):
  248. f = io.StringIO('''\xef\xbb\xbf foo
  249. bar\r
  250. baz
  251. # More after this line\r
  252. ; or after this
  253. bam''')
  254. self.assertEqual(read_batch_urls(f), ['foo', 'bar', 'baz', 'bam'])
  255. def test_urlencode_postdata(self):
  256. data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'})
  257. self.assertTrue(isinstance(data, bytes))
  258. def test_parse_iso8601(self):
  259. self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266)
  260. self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266)
  261. self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266)
  262. self.assertEqual(parse_iso8601('2014-03-23T22:04:26.1234Z'), 1395612266)
  263. def test_strip_jsonp(self):
  264. stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);')
  265. d = json.loads(stripped)
  266. self.assertEqual(d, [{"id": "532cb", "x": 3}])
  267. stripped = strip_jsonp('parseMetadata({"STATUS":"OK"})\n\n\n//epc')
  268. d = json.loads(stripped)
  269. self.assertEqual(d, {'STATUS': 'OK'})
  270. def test_uppercase_escape(self):
  271. self.assertEqual(uppercase_escape(''), '')
  272. self.assertEqual(uppercase_escape('\\U0001d550'), '𝕐')
  273. def test_limit_length(self):
  274. self.assertEqual(limit_length(None, 12), None)
  275. self.assertEqual(limit_length('foo', 12), 'foo')
  276. self.assertTrue(
  277. limit_length('foo bar baz asd', 12).startswith('foo bar'))
  278. self.assertTrue('...' in limit_length('foo bar baz asd', 12))
  279. def test_escape_rfc3986(self):
  280. reserved = "!*'();:@&=+$,/?#[]"
  281. unreserved = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
  282. self.assertEqual(escape_rfc3986(reserved), reserved)
  283. self.assertEqual(escape_rfc3986(unreserved), unreserved)
  284. self.assertEqual(escape_rfc3986('тест'), '%D1%82%D0%B5%D1%81%D1%82')
  285. self.assertEqual(escape_rfc3986('%D1%82%D0%B5%D1%81%D1%82'), '%D1%82%D0%B5%D1%81%D1%82')
  286. self.assertEqual(escape_rfc3986('foo bar'), 'foo%20bar')
  287. self.assertEqual(escape_rfc3986('foo%20bar'), 'foo%20bar')
  288. def test_escape_url(self):
  289. self.assertEqual(
  290. escape_url('http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavré_FD.mp4'),
  291. 'http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavre%CC%81_FD.mp4'
  292. )
  293. self.assertEqual(
  294. escape_url('http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erklärt/Das-Erste/Video?documentId=22673108&bcastId=5290'),
  295. 'http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erkl%C3%A4rt/Das-Erste/Video?documentId=22673108&bcastId=5290'
  296. )
  297. self.assertEqual(
  298. escape_url('http://тест.рф/фрагмент'),
  299. 'http://тест.рф/%D1%84%D1%80%D0%B0%D0%B3%D0%BC%D0%B5%D0%BD%D1%82'
  300. )
  301. self.assertEqual(
  302. escape_url('http://тест.рф/абв?абв=абв#абв'),
  303. 'http://тест.рф/%D0%B0%D0%B1%D0%B2?%D0%B0%D0%B1%D0%B2=%D0%B0%D0%B1%D0%B2#%D0%B0%D0%B1%D0%B2'
  304. )
  305. self.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0')
  306. def test_js_to_json_realworld(self):
  307. inp = '''{
  308. 'clip':{'provider':'pseudo'}
  309. }'''
  310. self.assertEqual(js_to_json(inp), '''{
  311. "clip":{"provider":"pseudo"}
  312. }''')
  313. json.loads(js_to_json(inp))
  314. inp = '''{
  315. 'playlist':[{'controls':{'all':null}}]
  316. }'''
  317. self.assertEqual(js_to_json(inp), '''{
  318. "playlist":[{"controls":{"all":null}}]
  319. }''')
  320. def test_js_to_json_edgecases(self):
  321. on = js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}")
  322. self.assertEqual(json.loads(on), {"abc_def": "1'\\2\\'3\"4"})
  323. on = js_to_json('{"abc": true}')
  324. self.assertEqual(json.loads(on), {'abc': True})
  325. # Ignore JavaScript code as well
  326. on = js_to_json('''{
  327. "x": 1,
  328. y: "a",
  329. z: some.code
  330. }''')
  331. d = json.loads(on)
  332. self.assertEqual(d['x'], 1)
  333. self.assertEqual(d['y'], 'a')
  334. def test_clean_html(self):
  335. self.assertEqual(clean_html('a:\nb'), 'a: b')
  336. self.assertEqual(clean_html('a:\n "b"'), 'a: "b"')
  337. def test_intlist_to_bytes(self):
  338. self.assertEqual(
  339. intlist_to_bytes([0, 1, 127, 128, 255]),
  340. b'\x00\x01\x7f\x80\xff')
  341. def test_args_to_str(self):
  342. self.assertEqual(
  343. args_to_str(['foo', 'ba/r', '-baz', '2 be', '']),
  344. 'foo ba/r -baz \'2 be\' \'\''
  345. )
  346. def test_parse_filesize(self):
  347. self.assertEqual(parse_filesize(None), None)
  348. self.assertEqual(parse_filesize(''), None)
  349. self.assertEqual(parse_filesize('91 B'), 91)
  350. self.assertEqual(parse_filesize('foobar'), None)
  351. self.assertEqual(parse_filesize('2 MiB'), 2097152)
  352. self.assertEqual(parse_filesize('5 GB'), 5000000000)
  353. self.assertEqual(parse_filesize('1.2Tb'), 1200000000000)
  354. self.assertEqual(parse_filesize('1,24 KB'), 1240)
  355. def test_version_tuple(self):
  356. self.assertEqual(version_tuple('1'), (1,))
  357. self.assertEqual(version_tuple('10.23.344'), (10, 23, 344))
  358. self.assertEqual(version_tuple('10.1-6'), (10, 1, 6)) # avconv style
  359. def test_detect_exe_version(self):
  360. self.assertEqual(detect_exe_version('''ffmpeg version 1.2.1
  361. built on May 27 2013 08:37:26 with gcc 4.7 (Debian 4.7.3-4)
  362. configuration: --prefix=/usr --extra-'''), '1.2.1')
  363. self.assertEqual(detect_exe_version('''ffmpeg version N-63176-g1fb4685
  364. built on May 15 2014 22:09:06 with gcc 4.8.2 (GCC)'''), 'N-63176-g1fb4685')
  365. self.assertEqual(detect_exe_version('''X server found. dri2 connection failed!
  366. Trying to open render node...
  367. Success at /dev/dri/renderD128.
  368. ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4')
  369. def test_age_restricted(self):
  370. self.assertFalse(age_restricted(None, 10)) # unrestricted content
  371. self.assertFalse(age_restricted(1, None)) # unrestricted policy
  372. self.assertFalse(age_restricted(8, 10))
  373. self.assertTrue(age_restricted(18, 14))
  374. self.assertFalse(age_restricted(18, 18))
  375. def test_is_html(self):
  376. self.assertFalse(is_html(b'\x49\x44\x43<html'))
  377. self.assertTrue(is_html(b'<!DOCTYPE foo>\xaaa'))
  378. self.assertTrue(is_html( # UTF-8 with BOM
  379. b'\xef\xbb\xbf<!DOCTYPE foo>\xaaa'))
  380. self.assertTrue(is_html( # UTF-16-LE
  381. b'\xff\xfe<\x00h\x00t\x00m\x00l\x00>\x00\xe4\x00'
  382. ))
  383. self.assertTrue(is_html( # UTF-16-BE
  384. b'\xfe\xff\x00<\x00h\x00t\x00m\x00l\x00>\x00\xe4'
  385. ))
  386. self.assertTrue(is_html( # UTF-32-BE
  387. b'\x00\x00\xFE\xFF\x00\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4'))
  388. self.assertTrue(is_html( # UTF-32-LE
  389. b'\xFF\xFE\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4\x00\x00\x00'))
  390. def test_render_table(self):
  391. self.assertEqual(
  392. render_table(
  393. ['a', 'bcd'],
  394. [[123, 4], [9999, 51]]),
  395. 'a bcd\n'
  396. '123 4\n'
  397. '9999 51')
  398. def test_match_str(self):
  399. self.assertRaises(ValueError, match_str, 'xy>foobar', {})
  400. self.assertFalse(match_str('xy', {'x': 1200}))
  401. self.assertTrue(match_str('!xy', {'x': 1200}))
  402. self.assertTrue(match_str('x', {'x': 1200}))
  403. self.assertFalse(match_str('!x', {'x': 1200}))
  404. self.assertTrue(match_str('x', {'x': 0}))
  405. self.assertFalse(match_str('x>0', {'x': 0}))
  406. self.assertFalse(match_str('x>0', {}))
  407. self.assertTrue(match_str('x>?0', {}))
  408. self.assertTrue(match_str('x>1K', {'x': 1200}))
  409. self.assertFalse(match_str('x>2K', {'x': 1200}))
  410. self.assertTrue(match_str('x>=1200 & x < 1300', {'x': 1200}))
  411. self.assertFalse(match_str('x>=1100 & x < 1200', {'x': 1200}))
  412. self.assertFalse(match_str('y=a212', {'y': 'foobar42'}))
  413. self.assertTrue(match_str('y=foobar42', {'y': 'foobar42'}))
  414. self.assertFalse(match_str('y!=foobar42', {'y': 'foobar42'}))
  415. self.assertTrue(match_str('y!=foobar2', {'y': 'foobar42'}))
  416. self.assertFalse(match_str(
  417. 'like_count > 100 & dislike_count <? 50 & description',
  418. {'like_count': 90, 'description': 'foo'}))
  419. self.assertTrue(match_str(
  420. 'like_count > 100 & dislike_count <? 50 & description',
  421. {'like_count': 190, 'description': 'foo'}))
  422. self.assertFalse(match_str(
  423. 'like_count > 100 & dislike_count <? 50 & description',
  424. {'like_count': 190, 'dislike_count': 60, 'description': 'foo'}))
  425. self.assertFalse(match_str(
  426. 'like_count > 100 & dislike_count <? 50 & description',
  427. {'like_count': 190, 'dislike_count': 10}))
  428. if __name__ == '__main__':
  429. unittest.main()