Browse Source

Make random string and image generators more generic.

Stefano Cossu 6 years ago
parent
commit
9bb397803b
4 changed files with 62 additions and 101 deletions
  1. 3 51
      conftest.py
  2. 5 10
      tests/endpoints/test_ldp.py
  3. 53 0
      util/generators.py
  4. 1 40
      util/ingest_random_image.py

+ 3 - 51
conftest.py

@@ -1,12 +1,9 @@
-import io
 import sys
 sys.path.append('.')
 import numpy
 import random
 import uuid
 
-from hashlib import sha1
-
 import pytest
 
 from PIL import Image
@@ -15,6 +12,7 @@ from lakesuperior.app import create_app
 from lakesuperior.config_parser import config
 from lakesuperior.store_layouts.rdf.graph_store_connector import \
         GraphStoreConnector
+from util.generators import random_image
 
 
 @pytest.fixture(scope='module')
@@ -47,56 +45,10 @@ def db(app):
 
 
 @pytest.fixture
-def rnd_image(rnd_utf8_string):
+def rnd_img():
     '''
     Generate a square image with random color tiles.
     '''
-    ts = 8 # Tile width and height. @TODO parametrize.
-    ims = 256 # Image width and height. @TODO parametrize.
-    imarray = numpy.random.rand(ts, ts, 3) * 255
-    im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
-    im = im.resize((ims, ims), Image.NEAREST)
-
-    imf = io.BytesIO()
-    im.save(imf, format='png')
-    imf.seek(0)
-    hash = sha1(imf.read()).hexdigest()
-
-    return {
-        'content' : imf,
-        'hash' : hash,
-        'filename' : rnd_utf8_string + '.png'
-    }
-
-
-@pytest.fixture
-def rnd_utf8_string():
-    '''
-    Generate a random UTF-8 string.
-    '''
-    # @TODO Update this to include code point ranges to be sampled
-    include_ranges = [
-        ( 0x0021, 0x0021 ),
-        ( 0x0023, 0x0026 ),
-        ( 0x0028, 0x007E ),
-        ( 0x00A1, 0x00AC ),
-        ( 0x00AE, 0x00FF ),
-        ( 0x0100, 0x017F ),
-        ( 0x0180, 0x024F ),
-        ( 0x2C60, 0x2C7F ),
-        ( 0x16A0, 0x16F0 ),
-        ( 0x0370, 0x0377 ),
-        ( 0x037A, 0x037E ),
-        ( 0x0384, 0x038A ),
-        ( 0x038C, 0x038C ),
-    ]
-    length = 64 # String length. @TODO parametrize.
-    alphabet = [
-        chr(code_point) for current_range in include_ranges
-            for code_point in range(current_range[0], current_range[1] + 1)
-    ]
-    return ''.join(random.choice(alphabet) for i in range(length))
-
-
+    return random_image(8, 256)
 
 

+ 5 - 10
tests/endpoints/test_ldp.py

@@ -51,23 +51,18 @@ def test_put_ldp_rs(client):
             g.objects(None, RDF.type)
 
 
-def test_put_ldp_nr(client, rnd_image, rnd_utf8_string):
+def test_put_ldp_nr(client, rnd_img):
     '''
     PUT a resource with binary payload and verify checksums.
     '''
-    rnd_image['content'].seek(0)
-    print('Filename: {}'.format(rnd_image['filename']))
-    print('Data: {}'.format(rnd_image['content']))
-    client.put('/ldp/ldpnr01', data=rnd_image['content'], headers={
+    rnd_img['content'].seek(0)
+    client.put('/ldp/ldpnr01', data=rnd_img['content'], headers={
             'Content-Disposition' : 'attachment; filename={}'.format(
-            rnd_image['filename'])})
+            rnd_img['filename'])})
 
     resp = client.get('/ldp/ldpnr01', headers={'accept' : 'image/png'})
     assert resp.status_code == 200
-    rnd_image['content'].seek(0)
-    #print('Original sample: {}'.format(rnd_image['content'].read(64)))
-    #print('Response sample: {}'.format(resp.data))
-    assert sha1(resp.data).hexdigest() == rnd_image['hash']
+    assert sha1(resp.data).hexdigest() == rnd_img['hash']
 
 
 def test_put_existing_resource(client, db, random_uuid):

+ 53 - 0
util/generators.py

@@ -0,0 +1,53 @@
+import io
+import random
+
+from hashlib import sha1
+
+import requests
+import numpy
+
+from PIL import Image
+
+
+# @TODO Update this to include code point ranges to be sampled
+include_ranges = [
+    ( 0x0021, 0x0021 ),
+    ( 0x0023, 0x0026 ),
+    ( 0x0028, 0x007E ),
+    ( 0x00A1, 0x00AC ),
+    ( 0x00AE, 0x00FF ),
+    ( 0x0100, 0x017F ),
+    ( 0x0180, 0x024F ),
+    ( 0x2C60, 0x2C7F ),
+    ( 0x16A0, 0x16F0 ),
+    ( 0x0370, 0x0377 ),
+    ( 0x037A, 0x037E ),
+    ( 0x0384, 0x038A ),
+    ( 0x038C, 0x038C ),
+]
+
+def random_utf8_string(length):
+    alphabet = [
+        chr(code_point) for current_range in include_ranges
+            for code_point in range(current_range[0], current_range[1] + 1)
+    ]
+    return ''.join(random.choice(alphabet) for i in range(length))
+
+
+def random_image(name, ts=8, ims=256):
+    imarray = numpy.random.rand(ts, ts, 3) * 255
+    im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
+    im = im.resize((ims, ims), Image.NEAREST)
+
+    imf = io.BytesIO()
+    im.save(imf, format='png')
+    imf.seek(0)
+    hash = sha1(imf.read()).hexdigest()
+
+    return {
+        'content' : imf,
+        'hash' : hash,
+        'filename' : random_utf8_string(32) + '.png'
+    }
+
+

+ 1 - 40
util/ingest_random_image.py

@@ -1,12 +1,7 @@
 #/usr/bin/env python
 
 import uuid
-import random
-import requests
-import numpy
-import sys
-
-from PIL import Image
+from generators import random_image, random_utf8_string
 
 host='http://localhost:5000' # Set this
 user='' # Set this
@@ -16,40 +11,6 @@ password='' # Set this
 img_path = '/tmp'
 uid=str(uuid.uuid4())[-12:]
 
-## Update this to include code point ranges to be sampled
-#include_ranges = [
-#    ( 0x0021, 0x0021 ),
-#    ( 0x0023, 0x0026 ),
-#    ( 0x0028, 0x007E ),
-#    ( 0x00A1, 0x00AC ),
-#    ( 0x00AE, 0x00FF ),
-#    ( 0x0100, 0x017F ),
-#    ( 0x0180, 0x024F ),
-#    ( 0x2C60, 0x2C7F ),
-#    ( 0x16A0, 0x16F0 ),
-#    ( 0x0370, 0x0377 ),
-#    ( 0x037A, 0x037E ),
-#    ( 0x0384, 0x038A ),
-#    ( 0x038C, 0x038C ),
-#]
-#
-#def random_utf8_string(length):
-#    alphabet = [
-#        chr(code_point) for current_range in include_ranges
-#            for code_point in range(current_range[0], current_range[1] + 1)
-#    ]
-#    return ''.join(random.choice(alphabet) for i in range(length))
-
-
-def random_image(name, th=8, tv=8, w=256, h=256):
-    imarray = numpy.random.rand(th, tv, 3) * 255
-    im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
-    im = im.resize((w, h), Image.NEAREST)
-    fname = '{}/{}.png'.format(img_path, name)
-    im.save(fname)
-    return fname
-
-
 with open(random_image(uid), 'rb') as f:
     rsp = requests.post(
         '{}/ldp'.format(host),