Skip to content

Commit

Permalink
Merge ac7bd44 into a34a1d9
Browse files Browse the repository at this point in the history
  • Loading branch information
fanglinfang committed Mar 12, 2019
2 parents a34a1d9 + ac7bd44 commit 9882da0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 65 deletions.
4 changes: 3 additions & 1 deletion restclients_core/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
For restclients methods that use threading, this can be used to prevent
innodb gap locks from deadlocking sequential inserts.
"""
from logging import getLogger

logger = getLogger(__name__)
__manage_bulk_inserts = False
__bulk_insert_queue = []

Expand Down Expand Up @@ -31,7 +33,7 @@ def save_all_queued_entries():
entry.save()
seen_urls[entry.url] = True
except Exception as ex:
print("Error bulk saving cache entries: ", ex)
logger.error("Error bulk saving cache entries: ", str(ex))

__bulk_insert_queue = []

Expand Down
14 changes: 4 additions & 10 deletions restclients_core/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,10 @@ def load(self, method, url, headers, body):
return value

for path in self._get_mock_paths():
response = load_resource_from_path(path, service, "file", url,
headers)

if response:
set_cache_value(cache_key, response)
return response

response = MockHTTP()
response.status = 404
response.reason = "Not Found"
response = load_resource_from_path(
path, service, "file", url, headers)

set_cache_value(cache_key, response)
if response.status == 404:
response.reason = "Not Found"
return response
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{ "headers": {"Custom": "My Custom Value"},
"status": 200
}
3 changes: 2 additions & 1 deletion restclients_core/tests/dao_implementation/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def service_name(self):
return 'testing'

def service_mock_paths(self):
# tests/dao_implementation/resources/
return [abspath(dirname(__file__) + "/resources/")]


Expand Down Expand Up @@ -46,7 +47,7 @@ def test_registered_paths(self):

response = TDAO().getURL('/override.json', {})
self.assertEquals(response.status, 200)
self.assertEquals(response.read(), b'{"override": true }\n')
self.assertEquals(response.read(), b'{"override": false }\n')

def test_binary_data(self):
response = TDAO().getURL('/image.jpg', {})
Expand Down
105 changes: 52 additions & 53 deletions restclients_core/util/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,75 @@
import os
from os.path import isfile, join, dirname
import json

from urllib.parse import unquote
from restclients_core.exceptions import DataFailureException

try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
from restclients_core.models import MockHTTP


def load_resource_from_path(resource_dir, service_name,
def load_resource_from_path(resource_dir,
service_name,
implementation_name,
url, headers):

RESOURCE_ROOT = os.path.join(resource_dir,
service_name,
implementation_name)

url,
headers):
if url == "///":
# Just a placeholder to put everything else in an else.
# If there are things that need dynamic work, they'd go here
pass
else:
response = MockHTTP()
response.status = 404
response.headers = None
RESOURCE_ROOT = os.path.join(resource_dir,
service_name,
implementation_name)
orig_file_path = RESOURCE_ROOT + url
handle = open_file(orig_file_path)
header_handle = open_file(orig_file_path + ".http-headers")

# attempt to open query permutations even on success
# so that if there are multiple files we throw an exception
if "?" in url:
handle = attempt_open_query_permutations(url, orig_file_path,
False)

if "?" in url:
header_handle = attempt_open_query_permutations(url,
orig_file_path,
True)

if handle is None and header_handle is None:
return None

handle = open_file(orig_file_path)
if handle is not None:
data = handle.read()

response = MockHTTP()
response.status = 200
if handle is not None:
response.data = data
response.headers = {"X-Data-Source": service_name + " file mock data",
}
response.status = 200
response.data = handle.read()

header_handle = open_file(orig_file_path + ".http-headers")
if header_handle is not None:
try:
data = header_handle.read()
file_values = json.loads(data)

if "headers" in file_values:
response.headers.update(file_values['headers'])
__read_header(header_handle, response, service_name)

if 'status' in file_values:
response.status = file_values['status']
# Check query permutations even on success
# so that if there are multiple files we throw an exception
if "?" in url:
handle = attempt_open_query_permutations(
url, orig_file_path, False)
if handle is not None and response.status == 404:
response.status = 200
response.data = handle.read()

header_handle = attempt_open_query_permutations(
url, orig_file_path, True)
if header_handle is not None and response.headers is None:
__read_header(header_handle, response, service_name)
return response

else:
response.headers.update(file_values)
except UnicodeDecodeError:
pass
except IOError:
pass

return response
def __read_header(header_handle, response, service_name):
try:
data = header_handle.read()
file_values = json.loads(data)

if response.headers is None:
response.headers = {
"X-Data-Source": service_name + " file mock data",
}

if "headers" in file_values:
response.headers.update(file_values['headers'])

if 'status' in file_values:
response.status = file_values['status']
else:
response.headers.update(file_values)
except UnicodeDecodeError:
pass
except IOError:
pass


def convert_to_platform_safe(dir_file_name):
Expand Down Expand Up @@ -105,7 +104,7 @@ def open_file(orig_file_path):
file_path = path
handle = open(path, "rb")
break
except IOError as ex:
except IOError:
pass

return handle
Expand Down

0 comments on commit 9882da0

Please sign in to comment.