Files
@ ead70abc037d
Branch filter:
Location: pydenticon/tests/test_pydenticon.py
ead70abc037d
14.2 KiB
text/x-python
Noticket: Preparing for 0.2 release.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | # Standard library imports.
import hashlib
import unittest
from io import BytesIO
# Third-party Python library imports.
import mock
import PIL
import PIL.ImageChops
# Library imports.
from pydenticon import Generator
class GeneratorTest(unittest.TestCase):
"""
Implements tests for pydenticon.Generator class.
"""
def test_init_entropy(self):
"""
Tests if the constructor properly checks for entropy provided by a
digest algorithm.
"""
# Set-up the mock instance.
hexdigest_method = mock.MagicMock(return_value="aabb")
digest_instance = mock.MagicMock()
digest_instance.hexdigest = hexdigest_method
# Set-up digest function that will always return the same digest
# instance.
digest_method = mock.MagicMock(return_value=digest_instance)
# This should require 23 bits of entropy, while the digest we defined
# provided 2*8 bits of entropy (2 bytes).
self.assertRaises(ValueError, Generator, 5, 5, digest=digest_method)
def test_init_parameters(self):
"""
Verifies that the constructor sets-up the instance properties correctly.
"""
generator = Generator(5, 5, digest=hashlib.sha1, foreground=["#111111", "#222222"], background="#aabbcc")
# sha1 provides 160 bits of entropy - 20 bytes.
self.assertEqual(generator.digest_entropy, 20 * 8)
self.assertEqual(generator.digest, hashlib.sha1)
self.assertEqual(generator.rows, 5)
self.assertEqual(generator.columns, 5)
self.assertEqual(generator.foreground, ["#111111", "#222222"])
self.assertEqual(generator.background, "#aabbcc")
def test_get_bit(self):
"""
Tests if the check whether bit is 1 or 0 is performed correctly.
"""
generator = Generator(5, 5)
hash_bytes = [0b10010001, 0b10001000, 0b00111001]
# Check a couple of bits from the above hash bytes.
self.assertEqual(True, generator._get_bit(0, hash_bytes))
self.assertEqual(True, generator._get_bit(7, hash_bytes))
self.assertEqual(False, generator._get_bit(22, hash_bytes))
self.assertEqual(True, generator._get_bit(23, hash_bytes))
def test_generate_matrix(self):
"""
Verifies that the matrix is generated correctly based on passed hashed
bytes.
"""
# The resulting half-matrix should be as follows (first byte is for
# ignored in matrix generation):
#
# 100
# 011
# 100
# 001
# 110
hash_bytes = [0b11111111, 0b10101010, 0b01010101]
expected_matrix = [
[True, False, False, False, True],
[False, True, True, True, False],
[True, False, False, False, True],
[False, False, True, False, False],
[True, True, False, True, True],
]
generator = Generator(5, 5)
matrix = generator._generate_matrix(hash_bytes)
self.assertEqual(matrix, expected_matrix)
def test_data_to_digest_byte_list_raw(self):
"""
Test if correct digest byte list is returned for raw (non-hex-digest)
data passed to the method.
"""
# Set-up some raw data, and set-up the expected result.
data = "this is a test\n"
expected_digest_byte_list = [225, 156, 18, 131, 201, 37, 179, 32, 102, 133, 255, 82, 42, 207, 227, 230]
# Instantiate a generator.
generator = Generator(5, 5, digest=hashlib.md5)
# Call the method and get the results.
digest_byte_list = generator._data_to_digest_byte_list(data)
# Verify the expected and actual result are identical.
self.assertEqual(expected_digest_byte_list, digest_byte_list)
def test_data_to_digest_byte_list_hex(self):
"""
Test if correct digest byte list is returned for passed hex digest
string.
"""
# Set-up some test hex digest (md5), and expected result.
hex_digest = "e19c1283c925b3206685ff522acfe3e6"
expected_digest_byte_list = [225, 156, 18, 131, 201, 37, 179, 32, 102, 133, 255, 82, 42, 207, 227, 230]
# Instantiate a generator.
generator = Generator(5, 5, digest=hashlib.md5)
# Call the method and get the results.
digest_byte_list = generator._data_to_digest_byte_list(hex_digest)
# Verify the expected and actual result are identical.
self.assertEqual(expected_digest_byte_list, digest_byte_list)
def test_data_to_digest_byte_list_hex_lookalike(self):
"""
Test if correct digest byte list is returned for passed raw data that
has same length as hex digest string.
"""
# Set-up some test hex digest (md5), and expected result.
data = "qqwweerrttyyuuiiooppaassddffgghh"
expected_digest_byte_list = [25, 182, 52, 218, 118, 220, 26, 145, 164, 222, 33, 221, 183, 140, 98, 246]
# Instantiate a generator.
generator = Generator(5, 5, digest=hashlib.md5)
# Call the method and get the results.
digest_byte_list = generator._data_to_digest_byte_list(data)
# Verify the expected and actual result are identical.
self.assertEqual(expected_digest_byte_list, digest_byte_list)
def test_generate_png_basics(self):
"""
Tests some basics about generated PNG identicon image. This includes:
- Dimensions of generated image.
- Format of generated image.
- Mode of generated image.
"""
# Set-up parameters that will be used for generating the image.
width = 200
height = 200
padding = [20, 20, 20, 20]
foreground = "#ffffff"
background = "#000000"
matrix = [
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
]
# Set-up a generator.
generator = Generator(5, 5)
# Generate the raw image.
raw_image = generator._generate_png(matrix, width, height, padding, foreground, background)
# Try to load the raw image.
image_stream = BytesIO(raw_image)
image = PIL.Image.open(image_stream)
# Verify image size, format, and mode.
self.assertEqual(image.size[0], 240)
self.assertEqual(image.size[1], 240)
self.assertEqual(image.format, "PNG")
self.assertEqual(image.mode, "RGB")
def test_generate_ascii(self):
"""
Tests the generated identicon in ASCII format.
"""
# Set-up parameters that will be used for generating the image.
foreground = "1"
background = "0"
matrix = [
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
]
# Set-up a generator.
generator = Generator(5, 5)
# Generate the ASCII image.
ascii_image = generator._generate_ascii(matrix, foreground, background)
# Verify that the result is as expected.
expected_result = """00100
00100
00100
01110
01110"""
self.assertEqual(ascii_image, expected_result)
def test_generate_format(self):
"""
Tests if identicons are generated in requested format.
"""
# Set-up a generator.
generator = Generator(5, 5)
# Set-up some test data.
data = "some test data"
# Verify that PNG image is returned when requested.
raw_image = generator.generate(data, 200, 200, output_format="png")
image_stream = BytesIO(raw_image)
image = PIL.Image.open(image_stream)
self.assertEqual(image.format, "PNG")
# Verify that ASCII "image" is returned when requested.
raw_image = generator.generate(data, 200, 200, output_format="ascii")
self.assertIsInstance(raw_image, str)
def test_generate_format_invalid(self):
"""
Tests if an exception is raised in case an unsupported format is
requested when generating the identicon.
"""
# Set-up a generator.
generator = Generator(5, 5)
# Set-up some test data.
data = "some test data"
# Verify that an exception is raised in case of unsupported format.
self.assertRaises(ValueError, generator.generate, data, 200, 200, output_format="invalid")
@mock.patch.object(Generator, '_generate_png')
def test_generate_inverted_png(self, generate_png_mock):
"""
Tests if the foreground and background are properly inverted when
generating PNG images.
"""
# Set-up some test data.
data = "Some test data"
# Set-up one foreground and background colour.
foreground = "#ffffff"
background = "#000000"
# Set-up the generator.
generator = Generator(5, 5, foreground=[foreground], background=background)
# Verify that colours are picked correctly when no inverstion is requsted.
generator.generate(data, 200, 200, inverted=False, output_format="png")
generate_png_mock.assert_called_with(mock.ANY, mock.ANY, mock.ANY, mock.ANY, foreground, background)
# Verify that colours are picked correctly when inversion is requsted.
generator.generate(data, 200, 200, inverted=True, output_format="png")
generate_png_mock.assert_called_with(mock.ANY, mock.ANY, mock.ANY, mock.ANY, background, foreground)
@mock.patch.object(Generator, '_generate_ascii')
def test_generate_inverted_ascii(self, generate_ascii_mock):
"""
Tests if the foreground and background are properly inverted when
generating ASCII "images".
"""
# Set-up some test data.
data = "Some test data"
# Set-up one foreground and background colour. These are not used for
# ASCII itself (instead a plus/minus sign is used).
foreground = "#ffffff"
background = "#000000"
# Set-up the generator.
generator = Generator(5, 5, foreground=[foreground], background=background)
# Verify that foreground/background is picked correctly when no
# inverstion is requsted.
generator.generate(data, 200, 200, inverted=False, output_format="ascii")
generate_ascii_mock.assert_called_with(mock.ANY, "+", "-")
# Verify that foreground/background is picked correctly when inversion
# is requsted.
generator.generate(data, 200, 200, inverted=True, output_format="ascii")
generate_ascii_mock.assert_called_with(mock.ANY, "-", "+")
@mock.patch.object(Generator, '_generate_png')
def test_generate_foreground(self, generate_png_mock):
"""
Tests if the foreground colour is picked correctly.
"""
# Set-up some foreground colours and a single background colour.
foreground = ["#000000", "#111111", "#222222", "#333333", "#444444", "#555555"]
background = "#ffffff"
# Set-up the generator.
generator = Generator(5, 5, foreground=foreground, background=background)
# The first byte of hex digest should be 121 for this data, which should
# result in foreground colour of index '1'.
data = "some test data"
generator.generate(data, 200, 200)
generate_png_mock.assert_called_with(mock.ANY, mock.ANY, mock.ANY, mock.ANY, foreground[1], background)
# The first byte of hex digest should be 149 for this data, which should
# result in foreground colour of index '5'.
data = "some other test data"
generator.generate(data, 200, 200)
generate_png_mock.assert_called_with(mock.ANY, mock.ANY, mock.ANY, mock.ANY, foreground[5], background)
def test_generate_png_compare(self):
"""
Tests generated PNG identicon against a set of pre-generated samples.
"""
# Set-up a list of foreground colours (taken from Sigil). Same as used
# for reference images.
foreground = ["rgb(45,79,255)",
"rgb(254,180,44)",
"rgb(226,121,234)",
"rgb(30,179,253)",
"rgb(232,77,65)",
"rgb(49,203,115)",
"rgb(141,69,170)"]
# Set-up a background colour (taken from Sigil). Same as used for
# reference images.
background = "rgb(224,224,224)"
# Set-up parameters equivalent as used for samples.
width = 200
height = 200
padding = (20, 20, 20, 20)
# Load the reference images, making sure they're in RGB mode.
test1_ref = PIL.Image.open("tests/samples/test1.png").convert(mode="RGB")
test2_ref = PIL.Image.open("tests/samples/test2.png").convert(mode="RGB")
test3_ref = PIL.Image.open("tests/samples/test3.png").convert(mode="RGB")
# Set-up the Generator.
generator = Generator(5, 5, foreground=foreground, background=background)
# Generate first test identicon.
raw_image = generator.generate("test1", width, height, padding=padding)
image_stream = BytesIO(raw_image)
test1 = PIL.Image.open(image_stream)
# Generate second test identicon.
raw_image = generator.generate("test2", width, height, padding=padding)
image_stream = BytesIO(raw_image)
test2 = PIL.Image.open(image_stream)
# Generate third test identicon.
raw_image = generator.generate("test3", width, height, padding=padding)
image_stream = BytesIO(raw_image)
test3 = PIL.Image.open(image_stream)
# Calculate differences between generated identicons and references.
diff1 = PIL.ImageChops.difference(test1, test1_ref)
diff2 = PIL.ImageChops.difference(test2, test2_ref)
diff3 = PIL.ImageChops.difference(test3, test3_ref)
# Verify that all the diffs are essentially black (i.e. no differences
# between generated identicons and reference samples).
expected_extrema = ((0, 0), (0, 0), (0, 0))
self.assertEqual(diff1.getextrema(), expected_extrema)
self.assertEqual(diff2.getextrema(), expected_extrema)
self.assertEqual(diff3.getextrema(), expected_extrema)
if __name__ == '__main__':
unittest.main()
|