Move remaining util tests into pytest

This commit is contained in:
Joseph Schorr 2018-07-19 11:40:40 -04:00
parent fa354d695a
commit 69406c6aec
4 changed files with 198 additions and 226 deletions

View file

@ -1,225 +0,0 @@
import unittest
from _pyio import BufferedReader
from itertools import islice
import magic
from util.validation import generate_valid_usernames, validate_username
from util.registry.generatorfile import GeneratorFile
from util import slash_join
class TestUsernameValidation(unittest.TestCase):
def assertValid(self, username):
result, _ = validate_username(username)
self.assertTrue(result)
def assertInvalid(self, username):
result, _ = validate_username(username)
self.assertFalse(result)
def test_valid(self):
self.assertValid('ja')
self.assertValid('jak')
self.assertValid('jake')
self.assertValid('ja_ke')
self.assertValid('te-st')
self.assertValid('te.st')
def test_properlength(self):
self.assertValid('z' * 30)
self.assertValid('z' * 255)
def test_tooshort(self):
self.assertInvalid('j')
def test_toolong(self):
self.assertInvalid('z' * 256)
def test_invalids(self):
self.assertInvalid('_test')
self.assertInvalid('Test')
self.assertInvalid('hello world')
self.assertInvalid('te---st')
class TestGeneratorFile(unittest.TestCase):
def sample_generator(self):
yield 'this'
yield 'is'
yield 'a'
yield 'test'
def test_basic_generator(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals(0, f.tell())
self.assertEquals("thisisatest", f.read())
self.assertEquals(len("thisisatest"), f.tell())
def test_same_lengths(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals("this", f.read(4))
self.assertEquals(4, f.tell())
self.assertEquals("is", f.read(2))
self.assertEquals(6, f.tell())
self.assertEquals("a", f.read(1))
self.assertEquals(7, f.tell())
self.assertEquals("test", f.read(4))
self.assertEquals(11, f.tell())
def test_indexed_lengths(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals("thisis", f.read(6))
self.assertEquals(6, f.tell())
self.assertEquals("atest", f.read(5))
self.assertEquals(11, f.tell())
def test_misindexed_lengths(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals("thisis", f.read(6))
self.assertEquals(6, f.tell())
self.assertEquals("ate", f.read(3))
self.assertEquals(9, f.tell())
self.assertEquals("st", f.read(2))
self.assertEquals(11, f.tell())
self.assertEquals("", f.read(2))
self.assertEquals(11, f.tell())
def test_misindexed_lengths_2(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals("thisisat", f.read(8))
self.assertEquals(8, f.tell())
self.assertEquals("e", f.read(1))
self.assertEquals(9, f.tell())
self.assertEquals("st", f.read(2))
self.assertEquals(11, f.tell())
self.assertEquals("", f.read(2))
self.assertEquals(11, f.tell())
def test_overly_long(self):
with GeneratorFile(self.sample_generator()) as f:
self.assertEquals("thisisatest", f.read(60))
self.assertEquals(11, f.tell())
def test_with_bufferedreader(self):
with GeneratorFile(self.sample_generator()) as f:
buffered = BufferedReader(f)
self.assertEquals("thisisatest", buffered.peek(10))
self.assertEquals("thisisates", buffered.read(10))
def mimed_html_generator(self):
yield '<html>'
yield '<body>'
yield 'sometext' * 1024
yield '</body>'
yield '</html>'
def test_magic(self):
mgc = magic.Magic(mime=True)
with GeneratorFile(self.mimed_html_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
self.assertEquals("text/html", mgc.from_buffer(file_header_bytes))
with GeneratorFile(self.sample_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
self.assertEquals("text/plain", mgc.from_buffer(file_header_bytes))
class TestUsernameGenerator(unittest.TestCase):
def assert_generated_output(self, input_username, expected_output):
name_gen = generate_valid_usernames(input_username)
generated_output = list(islice(name_gen, 1))[0]
self.assertEquals(expected_output, generated_output)
def test_basic_ascii_names(self):
self.assert_generated_output('jake', 'jake')
self.assert_generated_output('frank', 'frank')
self.assert_generated_output('fra-nk', 'fra_nk')
def test_names_with_caps(self):
self.assert_generated_output('Jake', 'jake')
self.assert_generated_output('FranK', 'frank')
def test_multiple_underscores(self):
self.assert_generated_output('ja__ke', 'ja_ke')
self.assert_generated_output('ja___ke', 'ja_ke')
def test_trailing_underscores(self):
self.assert_generated_output('ja__', 'ja')
self.assert_generated_output('jake__', 'jake')
def test_starting_underscore(self):
self.assert_generated_output('_jake', 'jake')
def test_short_names(self):
self.assert_generated_output('a', 'a0')
self.assert_generated_output('ab', 'ab')
self.assert_generated_output('abc', 'abc')
def test_long_names(self):
self.assert_generated_output('abcdefghijklmnopqrstuvwxyz1234567890',
'abcdefghijklmnopqrstuvwxyz1234567890')
self.assert_generated_output('c' * 256, 'c' * 255)
def test_unicode_transliteration(self):
self.assert_generated_output(u'\xc6neid', 'aeneid')
self.assert_generated_output(u'\xe9tude', 'etude')
self.assert_generated_output(u'\u5317\u4eb0', 'bei_jing')
self.assert_generated_output(u'\u1515\u14c7\u14c7', 'shanana')
self.assert_generated_output(u'\u13d4\u13b5\u13c6', 'taliqua')
self.assert_generated_output(u'\u0726\u071b\u073d\u0710\u073a', 'ptu_i')
self.assert_generated_output(u'\u0905\u092d\u093f\u091c\u0940\u0924', 'abhijiit')
self.assert_generated_output(u'\u0985\u09ad\u09bf\u099c\u09c0\u09a4', 'abhijiit')
self.assert_generated_output(u'\u0d05\u0d2d\u0d3f\u0d1c\u0d40\u0d24', 'abhijiit')
self.assert_generated_output(u'\u0d2e\u0d32\u0d2f\u0d3e\u0d32\u0d2e\u0d4d', 'mlyaalm')
self.assert_generated_output(u'\ue000', '00')
self.assert_generated_output(u'\u03ff', '00')
self.assert_generated_output(u'\u0d2e\u0d32\u03ff\u03ff\u0d2e\u0d32', 'mlml')
def test_multiple_suggestions(self):
name_gen = generate_valid_usernames('a')
generated_output = list(islice(name_gen, 4))
self.assertEquals('a0', generated_output[0])
self.assertEquals('a1', generated_output[1])
self.assertEquals('a2', generated_output[2])
self.assertEquals('a3', generated_output[3])
class TestSlashJoining(unittest.TestCase):
def test_joining(self):
test_cases = [
(['https://github.com', '/coreos-inc/' 'quay/pull/1092/files'],
'https://github.com/coreos-inc/quay/pull/1092/files'),
(['https://', 'github.com/', '/coreos-inc', '/quay/pull/1092/files/'],
'https://github.com/coreos-inc/quay/pull/1092/files'),
(['https://somegithub.com/', '/api/v3/'],
'https://somegithub.com/api/v3'),
(['https://github.somedomain.com/', '/api/v3/'],
'https://github.somedomain.com/api/v3'),
]
for args, url in test_cases:
joined_url = slash_join(*args)
self.assertEquals(url, joined_url)
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,98 @@
from _pyio import BufferedReader
import magic
from util.registry.generatorfile import GeneratorFile
def sample_generator():
yield 'this'
yield 'is'
yield 'a'
yield 'test'
def test_basic_generator():
with GeneratorFile(sample_generator()) as f:
assert f.tell() == 0
assert f.read() == "thisisatest"
assert f.tell() == len("thisisatest")
def test_same_lengths():
with GeneratorFile(sample_generator()) as f:
assert f.read(4) == "this"
assert f.tell() == 4
assert f.read(2) == "is"
assert f.tell() == 6
assert f.read(1) == "a"
assert f.tell() == 7
assert f.read(4) == "test"
assert f.tell() == 11
def test_indexed_lengths():
with GeneratorFile(sample_generator()) as f:
assert f.read(6) == "thisis"
assert f.tell() == 6
assert f.read(5) == "atest"
assert f.tell() == 11
def test_misindexed_lengths():
with GeneratorFile(sample_generator()) as f:
assert f.read(6) == "thisis"
assert f.tell() == 6
assert f.read(3) == "ate"
assert f.tell() == 9
assert f.read(2) == "st"
assert f.tell() == 11
assert f.read(2) == ""
assert f.tell() == 11
def test_misindexed_lengths_2():
with GeneratorFile(sample_generator()) as f:
assert f.read(8) == "thisisat"
assert f.tell() == 8
assert f.read(1) == "e"
assert f.tell() == 9
assert f.read(2) == "st"
assert f.tell() == 11
assert f.read(2) == ""
assert f.tell() == 11
def test_overly_long():
with GeneratorFile(sample_generator()) as f:
assert f.read(60) == "thisisatest"
assert f.tell() == 11
def test_with_bufferedreader():
with GeneratorFile(sample_generator()) as f:
buffered = BufferedReader(f)
assert buffered.peek(10) == "thisisatest"
assert buffered.read(10) == "thisisates"
def mimed_html_generator():
yield '<html>'
yield '<body>'
yield 'sometext' * 1024
yield '</body>'
yield '</html>'
def test_magic():
mgc = magic.Magic(mime=True)
with GeneratorFile(mimed_html_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
assert mgc.from_buffer(file_header_bytes) == "text/html"
with GeneratorFile(sample_generator()) as f:
buffered = BufferedReader(f)
file_header_bytes = buffered.peek(1024)
assert mgc.from_buffer(file_header_bytes) == "text/plain"

20
util/test/test_util.py Normal file
View file

@ -0,0 +1,20 @@
import pytest
from util import slash_join
@pytest.mark.parametrize('pieces, expected', [
(['https://github.com', '/coreos-inc/' 'quay/pull/1092/files'],
'https://github.com/coreos-inc/quay/pull/1092/files'),
(['https://', 'github.com/', '/coreos-inc', '/quay/pull/1092/files/'],
'https://github.com/coreos-inc/quay/pull/1092/files'),
(['https://somegithub.com/', '/api/v3/'],
'https://somegithub.com/api/v3'),
(['https://github.somedomain.com/', '/api/v3/'],
'https://github.somedomain.com/api/v3'),
])
def test_slash_join(pieces, expected):
joined_url = slash_join(*pieces)
assert joined_url == expected

View file

@ -1,6 +1,32 @@
from itertools import islice
import pytest import pytest
from util.validation import is_json, validate_label_key from util.validation import is_json, validate_label_key, generate_valid_usernames, validate_username
@pytest.mark.parametrize('username, is_valid', [
('ja', True),
('jak', True),
('jake', True),
('ja_ke', True),
('te-st', True),
('te.st', True),
('z' * 30, True),
('z' * 255, True),
('j', False),
('z' * 256, False),
('_test', False),
('Test', False),
('hello world', False),
('te---st', False),
])
def test_validate_username(username, is_valid):
valid, _ = validate_username(username)
assert valid == is_valid
@pytest.mark.parametrize('string_value,expected', [ @pytest.mark.parametrize('string_value,expected', [
@ -48,3 +74,56 @@ def test_is_json(string_value, expected):
]) ])
def test_validate_label_key(key, is_valid): def test_validate_label_key(key, is_valid):
assert validate_label_key(key) == is_valid assert validate_label_key(key) == is_valid
@pytest.mark.parametrize('input_username, expected_output', [
('jake', 'jake'),
('frank', 'frank'),
('fra-nk', 'fra_nk'),
('Jake', 'jake'),
('FranK', 'frank'),
('ja__ke', 'ja_ke'),
('ja___ke', 'ja_ke'),
('ja__', 'ja'),
('jake__', 'jake'),
('_jake', 'jake'),
('a', 'a0'),
('ab', 'ab'),
('abc', 'abc'),
('abcdefghijklmnopqrstuvwxyz1234567890', 'abcdefghijklmnopqrstuvwxyz1234567890'),
('c' * 256, 'c' * 255),
(u'\xc6neid', 'aeneid'),
(u'\xe9tude', 'etude'),
(u'\u5317\u4eb0', 'bei_jing'),
(u'\u1515\u14c7\u14c7', 'shanana'),
(u'\u13d4\u13b5\u13c6', 'taliqua'),
(u'\u0726\u071b\u073d\u0710\u073a', 'ptu_i'),
(u'\u0905\u092d\u093f\u091c\u0940\u0924', 'abhijiit'),
(u'\u0985\u09ad\u09bf\u099c\u09c0\u09a4', 'abhijiit'),
(u'\u0d05\u0d2d\u0d3f\u0d1c\u0d40\u0d24', 'abhijiit'),
(u'\u0d2e\u0d32\u0d2f\u0d3e\u0d32\u0d2e\u0d4d', 'mlyaalm'),
(u'\ue000', '00'),
(u'\u03ff', '00'),
(u'\u0d2e\u0d32\u03ff\u03ff\u0d2e\u0d32', 'mlml'),
])
def test_generate_valid_usernames(input_username, expected_output):
name_gen = generate_valid_usernames(input_username)
generated_output = list(islice(name_gen, 1))[0]
assert generated_output == expected_output
def test_multiple_suggestions():
name_gen = generate_valid_usernames('a')
generated_output = list(islice(name_gen, 4))
assert generated_output[0] == 'a0'
assert generated_output[1] == 'a1'
assert generated_output[2] == 'a2'
assert generated_output[3] == 'a3'