Skip to content

Commit

Permalink
Clarified difference between parse_ and clean_request, fixed old path…
Browse files Browse the repository at this point in the history
… bug, cleaned-up copying
  • Loading branch information
Tom Aitken committed Apr 7, 2021
1 parent 8eba1fe commit dd83956
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
6 changes: 2 additions & 4 deletions scrapy_testmaster/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
import os
import sys
import copy
import json
import scrapy
import argparse
Expand Down Expand Up @@ -221,12 +220,11 @@ def update(self):
request=request, **data['response'])

cb_settings = get_cb_settings(fixture_dir)
data["result"], _ = parse_callback_result(
data['result'], _ = parse_callback_result(
request.callback(response), spider, cb_settings
)
_result = copy.deepcopy(data['result'])
items_out, requests_out = process_result(
_result, spider.settings, cb_settings)
data['result'], spider.settings, cb_settings)
validate_results(fixture_dir, spider.settings, items_out,
requests_out, data['request']['url'])

Expand Down
6 changes: 3 additions & 3 deletions scrapy_testmaster/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def process_spider_input(self, response, spider):
cb_name = 'parse'
else:
cb_name = _request['callback']
test_dir = os.path.join(self.base_path, sanitize_module_name(spider.name), cb_name)
test_dir = os.path.join(
self.base_path, 'tests', sanitize_module_name(spider.name), cb_name)
cb_settings = get_cb_settings(test_dir)
filter_args = {'crawler', 'settings', 'start_urls'}
if isinstance(spider, CrawlSpider):
Expand Down Expand Up @@ -181,9 +182,8 @@ def process_spider_output(self, response, result, spider):
_request = copy.deepcopy(data['request'])
_request = clean_request(_request, spider.settings, cb_settings)

_result = copy.deepcopy(data['result'])
items_out, requests_out = process_result(
_result, spider.settings, cb_settings)
data['result'], spider.settings, cb_settings)
validate_results(test_dir, spider.settings, items_out, requests_out,
request['url'])

Expand Down
9 changes: 6 additions & 3 deletions scrapy_testmaster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ def parse_object(_object, spider, cb_settings):
return _object


# processes request for recording, handling auth settings
def parse_request(request, spider, cb_settings):
_request = request_to_dict(request, spider=spider)
_request = copy.deepcopy(request_to_dict(request, spider=spider))
if not _request['callback']:
_request['callback'] = 'parse'

Expand All @@ -204,6 +205,7 @@ def parse_request(request, spider, cb_settings):
for key, value in _request.get('meta').items():
if key != '_testmaster':
_meta[key] = parse_object(value, spider, cb_settings)
_clean_splash(_meta, spider.settings, cb_settings)
_request['meta'] = _meta

return _request
Expand Down Expand Up @@ -293,6 +295,7 @@ def _clean_headers(headers, spider_settings, cb_settings, mode=""):
return headers


# processes request into JSON format for inscribing in view.json and for validation
def clean_request(request, spider_settings, cb_settings):
skipped_global = spider_settings.get('TESTMASTER_REQUEST_SKIPPED_FIELDS', default=[])
try:
Expand Down Expand Up @@ -326,9 +329,9 @@ def _clean(data, field_list):


def process_result(result, spider_settings, cb_settings):
items = [x["data"] for x in filter(
items = [copy.deepcopy(x["data"]) for x in filter(
lambda res: res["type"] == "item", result)]
requests = [x["data"] for x in filter(
requests = [copy.deepcopy(x["data"]) for x in filter(
lambda res: res["type"] == "request", result)]
for i in range(len(items)):
clean_item(items[i], spider_settings, cb_settings)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='scrapy-testmaster',
version='1.2',
version='1.3',
author='Thomas Aitken',
author_email='tclaitken@gmail.com',
description='Automated testing and debugging tool for Scrapy.',
Expand Down

0 comments on commit dd83956

Please sign in to comment.