Skip to content

Commit 0f80a0d

Browse files
committed
Running test an py3
1 parent 8b19c2d commit 0f80a0d

37 files changed

+263
-253
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ __pycache__/
77

88
# docker file to test all pytho3 version
99
docker-*
10+
Dockerfile*
1011

1112
# Distribution / packaging
1213
.Python

py3resttest/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#__all__ = ["resttest", "generators", "binding", "parsing",
1+
# __all__ = ["resttest", "generators", "binding", "parsing",
22
# "validators", "contenthandling", "benchmarks", "tests", "six"]

py3resttest/benchmark_framework_mini.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323

2424
# Time for full curl execution on Django testing rest app
2525
# Time: 41.4s for 10k runs, or about 4.14 ms per
26-
timeit.timeit("mycurl=mytest.configure_curl(); mycurl.setopt(pycurl.WRITEFUNCTION, lambda x: None); mycurl.perform(); mycurl.close()",
27-
setup='import pycurl; from resttest import Test; input = {"url": "/api/person/", "NAME":"foo", "group":"bar"}; mytest=Test.parse_test("http://localhost:8000", input);',
28-
number=10000)
26+
timeit.timeit(
27+
"mycurl=mytest.configure_curl(); mycurl.setopt(pycurl.WRITEFUNCTION, lambda x: None); mycurl.perform(); mycurl.close()",
28+
setup='import pycurl; from resttest import Test; input = {"url": "/api/person/", "NAME":"foo", "group":"bar"}; mytest=Test.parse_test("http://localhost:8000", input);',
29+
number=10000)
2930

3031
# Github perf test, 27 s for 100 runs = 270 ms per
31-
timeit.timeit("mycurl=mytest.configure_curl(); mycurl.setopt(pycurl.WRITEFUNCTION, lambda x: None); mycurl.perform(); mycurl.close()",
32-
setup='import pycurl; from resttest import Test; input = {"url": "/search/users?q=jewzaam", "NAME":"foo", "group":"bar"}; mytest=Test.parse_test("https://api.github.com", input);',
33-
number=100)
32+
timeit.timeit(
33+
"mycurl=mytest.configure_curl(); mycurl.setopt(pycurl.WRITEFUNCTION, lambda x: None); mycurl.perform(); mycurl.close()",
34+
setup='import pycurl; from resttest import Test; input = {"url": "/search/users?q=jewzaam", "NAME":"foo", "group":"bar"}; mytest=Test.parse_test("https://api.github.com", input);',
35+
number=100)

py3resttest/benchmarks.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import math
21
import json
2+
import math
3+
34
import pycurl
45

56
from py3resttest.constants import DEFAULT_TIMEOUT
6-
from py3resttest.tests import Test, coerce_to_string
77
from py3resttest.parsing import *
8-
9-
8+
from py3resttest.testcase import Test, coerce_to_string
109

1110
"""
1211
Encapsulates logic related to benchmarking
@@ -15,7 +14,6 @@
1514
- Templating/Caching logic specific to benchmarks
1615
"""
1716

18-
1917
# Curl metrics for benchmarking, key is name in config file, value is pycurl variable
2018
# Taken from pycurl docs, this is libcurl variable minus the CURLINFO prefix
2119
# Descriptions of the timing variables are taken from libcurl docs:
@@ -51,7 +49,6 @@
5149
# Total time of the previous request.
5250
'total_time': pycurl.TOTAL_TIME,
5351

54-
5552
# Transfer sizes and speeds
5653
'size_download': pycurl.SIZE_DOWNLOAD,
5754
'size_upload': pycurl.SIZE_UPLOAD,
@@ -68,11 +65,11 @@
6865
# aggregation on an array
6966
AGGREGATES = {
7067
'mean_arithmetic': # AKA the average, good for many things
71-
lambda x: float(sum(x)) / float(len(x)),
68+
lambda x: float(sum(x)) / float(len(x)),
7269
'mean': # Alias for arithmetic mean
73-
lambda x: float(sum(x)) / float(len(x)),
70+
lambda x: float(sum(x)) / float(len(x)),
7471
'mean_harmonic': # Harmonic mean, better predicts average of rates: http://en.wikipedia.org/wiki/Harmonic_mean
75-
lambda x: 1.0 / (sum([1.0 / float(y) for y in x]) / float(len(x))),
72+
lambda x: 1.0 / (sum([1.0 / float(y) for y in x]) / float(len(x))),
7673
'median': lambda x: median(x),
7774
'std_deviation': lambda x: std_deviation(x),
7875
'sum': lambda x: sum(x),
@@ -99,7 +96,7 @@ def std_deviation(array):
9996
return 0
10097

10198
average = AGGREGATES['mean_arithmetic'](array)
102-
variance = map(lambda x: (x - average)**2, array)
99+
variance = map(lambda x: (x - average) ** 2, array)
103100
variance = list(variance)
104101
stdev = AGGREGATES['mean_arithmetic'](variance)
105102
return math.sqrt(stdev)
@@ -234,7 +231,7 @@ def parse_benchmark(base_url, node):
234231
"Invalid aggregate input: non-string aggregate name")
235232
# TODO unicode-safe this
236233
benchmark.add_metric(coerce_to_string(metricname),
237-
coerce_to_string(aggregate))
234+
coerce_to_string(aggregate))
238235

239236
elif isinstance(metric, str):
240237
benchmark.add_metric(coerce_to_string(metric))
@@ -248,7 +245,7 @@ def parse_benchmark(base_url, node):
248245
raise TypeError(
249246
"Invalid aggregate input: non-string aggregate name")
250247
benchmark.add_metric(coerce_to_string(metricname),
251-
coerce_to_string(aggregate))
248+
coerce_to_string(aggregate))
252249
else:
253250
raise TypeError(
254251
"Invalid benchmark metric datatype: " + str(value))

py3resttest/binding.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def bind_variable(self, variable_name, variable_value):
2323
if prev != variable_value:
2424
self.variables[str(variable_name)] = variable_value
2525
self.mod_count = self.mod_count + 1
26-
#logging.debug('Context: altered variable named {0} to value {1}'.format(str_name, variable_value))
26+
# logging.debug('Context: altered variable named {0} to value {1}'.format(str_name, variable_value))
2727

2828
def bind_variables(self, variable_map):
2929
for key, value in variable_map.items():
@@ -38,7 +38,7 @@ def add_generator(self, generator_name, generator):
3838
'Cannot add generator named {0}, it is not a generator type'.format(generator_name))
3939

4040
self.generators[str(generator_name)] = generator
41-
#logging.debug('Context: Added generator named {0}'.format(generator_name))
41+
# logging.debug('Context: Added generator named {0}'.format(generator_name))
4242

4343
def bind_generator_next(self, variable_name, generator_name):
4444
""" Binds the next value for generator_name to variable_name and return value used """
@@ -51,7 +51,7 @@ def bind_generator_next(self, variable_name, generator_name):
5151
self.variables[str_name] = val
5252
self.mod_count = self.mod_count + 1
5353
# Logging is /expensive/
54-
#logging.debug('Context: Set variable named {0} to next value {1} from generator named {2}'.format(variable_name, val, generator_name))
54+
# logging.debug('Context: Set variable named {0} to next value {1} from generator named {2}'.format(variable_name, val, generator_name))
5555
return val
5656

5757
def get_values(self):

py3resttest/constants.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
DEFAULT_TIMEOUT = 10
32

3+
44
class YamlKeyWords:
55
IMPORT = 'import'
66
TEST = 'test'
@@ -13,4 +13,3 @@ class YamlKeyWords:
1313
VARS = 'variable_binds'
1414
GENERATORS = 'generators'
1515
RETRIES = 'retries'
16-

py3resttest/contenthandling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
from py3resttest.parsing import *
32

3+
from py3resttest.parsing import *
44

55
"""
66
Encapsulates contend handling logic, for pulling file content into tests

py3resttest/generators.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import logging
2+
import os
13
import random
24
import string
3-
import os
4-
import logging
5+
56
from py3resttest.parsing import flatten_dictionaries, lowercase_keys
67

78
""" Collection of generators to be used in templating for test data

py3resttest/parsing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def encode_unicode_bytes(my_string):
1414

1515
return my_string
1616

17+
1718
# TODO create a full class that extends string.Template
1819
def safe_substitute_unicode_template(templated_string, variable_map):
1920
""" Perform string.Template safe_substitute on unicode input with unicode variable values by using escapes
@@ -23,7 +24,6 @@ def safe_substitute_unicode_template(templated_string, variable_map):
2324
return string.Template(templated_string).safe_substitute(variable_map)
2425

2526

26-
2727
def safe_to_json(in_obj):
2828
""" Safely get dict from object if present for json dumping """
2929
if isinstance(in_obj, bytearray):
@@ -73,7 +73,6 @@ def safe_to_bool(input):
7373
raise TypeError(
7474
'Input Object is not a boolean or string form of boolean!')
7575

76-
7776
# class SuperConfigurator(object):
7877
# """ It's a bird! It's a plane! No, it's....
7978
# The solution to handling horribly nasty, thorny configuration handling methods

py3resttest/resttest.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@
88
import time
99
import traceback
1010
from email import message_from_string
11+
from io import BytesIO
1112
from optparse import OptionParser
1213
from urllib.parse import urljoin
1314

1415
import pycurl
1516
import yaml
1617

17-
from io import BytesIO
18-
19-
from py3resttest.benchmarks import METRICS, AGGREGATES, parse_benchmark
20-
from py3resttest.constants import YamlKeyWords
21-
from py3resttest.binding import Context
2218
from py3resttest import generators
2319
from py3resttest import validators
20+
from py3resttest.benchmarks import METRICS, AGGREGATES, parse_benchmark
21+
from py3resttest.binding import Context
22+
from py3resttest.constants import DEFAULT_TIMEOUT
23+
from py3resttest.constants import YamlKeyWords
2424
from py3resttest.generators import parse_generator
2525
from py3resttest.parsing import flatten_dictionaries, lowercase_keys, safe_to_bool, safe_to_json
26-
26+
from py3resttest.testcase import Test
2727
from py3resttest.validators import Failure
28-
from py3resttest.tests import Test
29-
from py3resttest.constants import DEFAULT_TIMEOUT
28+
3029
ESCAPE_DECODING = 'unicode_escape'
3130

3231
"""
@@ -732,14 +731,17 @@ def register_extensions(modules):
732731
raise ImportError(
733732
"Extension to register did not contain any registries: {0}".format(ext))
734733

734+
735735
try:
736736
import jsonschema
737+
737738
register_extensions('py3resttest.ext.validator_jsonschema')
738739
except ImportError as ie:
739740
logging.debug("Failed to load jsonschema validator, make sure the jsonschema "
740741
"module is installed if you wish to use schema validators.")
741742
try:
742743
import jmespath
744+
743745
register_extensions('py3resttest.ext.extractor_jmespath')
744746
except ImportError as ie:
745747
logging.debug("Failed to load jmespath extractor, make sure the jmespath module "

py3resttest/tests.py renamed to py3resttest/testcase.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
21
import copy
32
import json
3+
from io import BytesIO as MyIO
4+
from urllib.parse import urljoin
5+
46
import pycurl
57

8+
from py3resttest import validators
69
from py3resttest.constants import DEFAULT_TIMEOUT
710
from py3resttest.contenthandling import ContentHandler
8-
from urllib.parse import urljoin
9-
from py3resttest import validators
1011
from py3resttest.parsing import *
11-
from io import BytesIO as MyIO
1212

1313
"""
1414
Pull out the Test objects and logic associated with them
@@ -54,7 +54,6 @@ def coerce_http_method(val: str):
5454
if not isinstance(val, str) or len(val) == 0:
5555
raise TypeError("Invalid HTTP method name: input {0} is not a string or has 0 length".format(val))
5656

57-
5857
return val.upper()
5958

6059

@@ -91,6 +90,11 @@ class Test(object):
9190
generator_binds = None # Dict of variable name and then generator name
9291
extract_binds = None # Dict of variable name and extract function to run
9392

93+
def __init__(self):
94+
self.headers = dict()
95+
self.expected_status = [200]
96+
self.templated = dict()
97+
9498
@staticmethod
9599
def has_contains():
96100
return 'contains' in validators.VALIDATORS
@@ -257,11 +261,6 @@ def realize_partial(self, context=None):
257261
output._body = newbod
258262
return output
259263

260-
def __init__(self):
261-
self.headers = dict()
262-
self.expected_status = [200]
263-
self.templated = dict()
264-
265264
def __str__(self):
266265
return json.dumps(self, default=safe_to_json)
267266

0 commit comments

Comments
 (0)