import unittest from itertools import islice from util.validation import generate_valid_usernames from util.generatorfile import GeneratorFile 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("thisisatest", f.read()) def test_same_lengths(self): with GeneratorFile(self.sample_generator()) as f: self.assertEquals("this", f.read(4)) self.assertEquals("is", f.read(2)) self.assertEquals("a", f.read(1)) self.assertEquals("test", f.read(4)) def test_indexed_lengths(self): with GeneratorFile(self.sample_generator()) as f: self.assertEquals("thisis", f.read(6)) self.assertEquals("atest", f.read(5)) def test_misindexed_lengths(self): with GeneratorFile(self.sample_generator()) as f: self.assertEquals("thisis", f.read(6)) self.assertEquals("ate", f.read(3)) self.assertEquals("st", f.read(2)) self.assertEquals("", f.read(2)) def test_misindexed_lengths_2(self): with GeneratorFile(self.sample_generator()) as f: self.assertEquals("thisisat", f.read(8)) self.assertEquals("e", f.read(1)) self.assertEquals("st", f.read(2)) self.assertEquals("", f.read(2)) def test_overly_long(self): with GeneratorFile(self.sample_generator()) as f: self.assertEquals("thisisatest", f.read(60)) 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') def test_names_with_caps(self): self.assert_generated_output('Jake', 'jake') self.assert_generated_output('FranK', 'frank') def test_short_names(self): self.assert_generated_output('a', 'a___') self.assert_generated_output('ab', 'ab__') self.assert_generated_output('abc', 'abc_') def test_long_names(self): self.assert_generated_output('abcdefghijklmnopqrstuvwxyz1234567890', 'abcdefghijklmnopqrstuvwxyz1234') 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', '____') self.assert_generated_output(u'\u03ff', '____') def test_multiple_suggestions(self): name_gen = generate_valid_usernames('a') generated_output = list(islice(name_gen, 4)) self.assertEquals('a___', generated_output[0]) self.assertEquals('a__0', generated_output[1]) self.assertEquals('a__1', generated_output[2]) self.assertEquals('a__2', generated_output[3]) if __name__ == '__main__': unittest.main()