2017-01-12 17:07:31 +03:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from ...strings import StringStore
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2017-05-29 02:05:53 +03:00
|
|
|
def test_stringstore_from_api_docs(stringstore):
|
|
|
|
apple_hash = stringstore.add('apple')
|
|
|
|
assert apple_hash == 8566208034543834098
|
|
|
|
assert stringstore[apple_hash] == u'apple'
|
|
|
|
|
|
|
|
assert u'apple' in stringstore
|
|
|
|
assert u'cherry' not in stringstore
|
|
|
|
|
|
|
|
orange_hash = stringstore.add('orange')
|
|
|
|
all_strings = [s for s in stringstore]
|
|
|
|
assert all_strings == [u'apple', u'orange']
|
|
|
|
|
|
|
|
banana_hash = stringstore.add('banana')
|
|
|
|
assert len(stringstore) == 3
|
|
|
|
assert banana_hash == 2525716904149915114
|
|
|
|
assert stringstore[banana_hash] == u'banana'
|
|
|
|
assert stringstore[u'banana'] == banana_hash
|
|
|
|
|
|
|
|
|
2017-01-12 17:07:31 +03:00
|
|
|
@pytest.mark.parametrize('text1,text2,text3', [(b'Hello', b'goodbye', b'hello')])
|
|
|
|
def test_stringstore_save_bytes(stringstore, text1, text2, text3):
|
2017-05-28 13:36:27 +03:00
|
|
|
key = stringstore.add(text1)
|
|
|
|
assert stringstore[text1] == key
|
|
|
|
assert stringstore[text2] != key
|
|
|
|
assert stringstore[text3] != key
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('text1,text2,text3', [('Hello', 'goodbye', 'hello')])
|
|
|
|
def test_stringstore_save_unicode(stringstore, text1, text2, text3):
|
2017-05-28 13:36:27 +03:00
|
|
|
key = stringstore.add(text1)
|
|
|
|
assert stringstore[text1] == key
|
|
|
|
assert stringstore[text2] != key
|
|
|
|
assert stringstore[text3] != key
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('text', [b'A'])
|
|
|
|
def test_stringstore_retrieve_id(stringstore, text):
|
2017-05-28 13:36:27 +03:00
|
|
|
key = stringstore.add(text)
|
|
|
|
assert len(stringstore) == 1
|
|
|
|
assert stringstore[key] == text.decode('utf8')
|
|
|
|
with pytest.raises(KeyError):
|
2017-05-28 15:08:09 +03:00
|
|
|
stringstore[20000]
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('text1,text2', [(b'0123456789', b'A')])
|
|
|
|
def test_stringstore_med_string(stringstore, text1, text2):
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text1)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text1.decode('utf8')
|
2017-05-28 13:36:27 +03:00
|
|
|
dummy = stringstore.add(text2)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[text1] == store
|
|
|
|
|
|
|
|
|
|
|
|
def test_stringstore_long_string(stringstore):
|
|
|
|
text = "INFORMATIVE](http://www.google.com/search?as_q=RedditMonkey&hl=en&num=50&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_nlo=&as_nhi=&as_occt=any&as_dt=i&as_sitesearch=&as_rights=&safe=off"
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('factor', [254, 255, 256])
|
|
|
|
def test_stringstore_multiply(stringstore, factor):
|
|
|
|
text = 'a' * factor
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
|
|
|
|
|
|
|
|
|
|
|
def test_stringstore_massive_strings(stringstore):
|
|
|
|
text = 'a' * 511
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
|
|
|
text2 = 'z' * 512
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text2)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text2
|
|
|
|
text3 = '1' * 513
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text3)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text3
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('text', ["qqqqq"])
|
2017-05-22 13:38:00 +03:00
|
|
|
def test_stringstore_to_bytes(stringstore, text):
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-05-22 13:38:00 +03:00
|
|
|
serialized = stringstore.to_bytes()
|
|
|
|
new_stringstore = StringStore().from_bytes(serialized)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert new_stringstore[store] == text
|