Skip to content

Commit

Permalink
Fix y restore de tests no corriendo (algunos siguen lentos)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslavandeira committed Sep 25, 2019
1 parent e1259d1 commit 2fe58cc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pydatajson/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _update_validation_response(self, error, response):

def _validate_landing_pages(self, catalog):
datasets = catalog.get('dataset')
datasets = list(filter(lambda x: 'landingPage' in x, datasets))
datasets = filter(lambda x: x.get('landingPage'), datasets)

for dataset_idx, dataset in enumerate(datasets):
dataset_title = dataset.get('title')
Expand Down
85 changes: 33 additions & 52 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import os.path
import re

import requests_mock
import vcr
from nose.tools import assert_true, assert_false, assert_dict_equal, \
from nose.tools import assert_true, assert_false, assert_dict_equal,\
assert_regexp_matches
from six import iteritems, text_type

Expand All @@ -28,38 +29,32 @@
record_mode='once')


@mock.patch('pydatajson.validation.is_working_url', return_value=(True, 200))
class TestDataJsonTestCase(object):
SAMPLES_DIR = os.path.join("tests", "samples")
RESULTS_DIR = RESULTS_DIR
TEMP_DIR = os.path.join("tests", "temp")
LANDING_PAGE = 'http://datos.gob.ar/dataset/' \
'sistema-de-contrataciones-electronicas-argentina-compra'
ACCESS_URL = "http://datos.gob.ar/dataset/" \
"sistema-de-contrataciones-electronicas-argentina-compra/" \
"archivo/fa3603b3-0af7-43cc-9da9-90a512217d8a"
DOWNLOAD_URL = "http://186.33.211.253/dataset/" \
"99db6631-d1c9-470b-a73e-c62daa32c420/resource/" \
"4b7447cb-31ff-4352-96c3-589d212e1cc9/download/" \
"convocatorias-abiertas-anio-2015.csv"

@classmethod
def get_sample(cls, sample_filename):
return os.path.join(cls.SAMPLES_DIR, sample_filename)

@classmethod
def setUp(cls):
cls.dj = pydatajson.DataJson(cls.get_sample("full_data.json"))
cls.catalog = pydatajson.readers.read_catalog(
cls.get_sample("full_data.json"))
cls.maxDiff = None
cls.longMessage = True

@classmethod
def tearDown(cls):
del cls.dj
def setUp(self):
self.requests_mock = requests_mock.Mocker()
self.requests_mock.start()
self.requests_mock.get(requests_mock.ANY, real_http=True)
self.requests_mock.head(requests_mock.ANY, status_code=200)
self.dj = pydatajson.DataJson(self.get_sample("full_data.json"))
self.catalog = pydatajson.readers.read_catalog(
self.get_sample("full_data.json"))
self.maxDiff = None
self.longMessage = True

def tearDown(self):
del self.dj
self.requests_mock.stop()

def run_case(self, case_filename, expected_dict=None):

sample_path = os.path.join(self.SAMPLES_DIR, case_filename + ".json")
result_path = os.path.join(self.RESULTS_DIR, case_filename + ".json")

Expand All @@ -69,6 +64,7 @@ def run_case(self, case_filename, expected_dict=None):

response_bool = self.dj.is_valid_catalog(sample_path)
response_dict = self.dj.validate_catalog(sample_path)

print(text_type(json.dumps(
response_dict, indent=4, separators=(",", ": "),
ensure_ascii=False
Expand All @@ -87,11 +83,11 @@ def run_case(self, case_filename, expected_dict=None):
# Tests de CAMPOS REQUERIDOS

# Tests de inputs válidos
def test_validity(self, _mock_validation):
def test_validity(self):
for filename, value_or_none in iteritems(TEST_FILE_RESPONSES):
yield self.run_case, filename, value_or_none

def test_validity_of_invalid_dataset_type(self, _mock_validation):
def test_validity_of_invalid_dataset_type(self):
"""
Validación ante un campo 'dataset' inválido en un catalogo
:return:
Expand All @@ -104,7 +100,7 @@ def test_validity_of_invalid_dataset_type(self, _mock_validation):
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_invalid_dataset_theme_type(self, _mock_validation):
def test_invalid_dataset_theme_type(self):
case_filename = "invalid_dataset_theme_type"
expected_valid = False
path = ['error', 'dataset', 0, 'errors', 0, 'message']
Expand All @@ -113,15 +109,15 @@ def test_invalid_dataset_theme_type(self, _mock_validation):
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_invalid_empty_super_theme_list(self, _mock_validation):
def test_invalid_empty_super_theme_list(self):
case_filename = "empty_super_theme_list"
expected_valid = False
path = ['error', 'dataset', 0, 'errors', 0, 'message']
regex = "\[\] is too short"
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_invalid_keywords(self, _mock_validation):
def test_invalid_keywords(self):
case_filename = "invalid_keywords"
expected_valid = False
path = ['error', 'dataset', 1, 'errors', 0, 'message']
Expand All @@ -131,7 +127,7 @@ def test_invalid_keywords(self, _mock_validation):
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_invalid_whitespace_emails(self, _mock_validation):
def test_invalid_whitespace_emails(self):
case_filename = "invalid_whitespace_emails"
expected_valid = False
path = ['error', 'dataset', 0, 'errors', 0, 'message']
Expand All @@ -145,7 +141,7 @@ def test_invalid_whitespace_emails(self, _mock_validation):
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_invalid_multiple_emails(self, _mock_validation):
def test_invalid_multiple_emails(self):
case_filename = "invalid_multiple_emails"
expected_valid = False
path = ['error', 'dataset', 0, 'errors', 0, 'message']
Expand All @@ -159,7 +155,7 @@ def test_invalid_multiple_emails(self, _mock_validation):
self.validate_message_with_file(
case_filename, expected_valid, path, regex)

def test_several_assorted_errors(self, _mock_validation):
def test_several_assorted_errors(self):
case_filename = "several_assorted_errors"
expected_errors = [
(
Expand Down Expand Up @@ -276,13 +272,13 @@ def validate_contains_message(self, datajson, path, regex):
assert_true(any(matches))

@my_vcr.use_cassette('test_validate_bad_remote_datajson')
def test_validate_invalid_remote_datajson_is_invalid(self, _mock_valid):
def test_validate_invalid_remote_datajson_is_invalid(self):
""" Testea `is_valid_catalog` contra un data.json remoto invalido."""

res = self.dj.is_valid_catalog(BAD_DATAJSON_URL)
assert_false(res)

def test_validate_invalid_remote_datajson_has_errors(self, _mock_valid):
def test_validate_invalid_remote_datajson_has_errors(self):
""" Testea `validate_catalog` contra un data.json remoto invalido."""

errors = [(
Expand All @@ -301,13 +297,13 @@ def test_validate_invalid_remote_datajson_has_errors(self, _mock_valid):

# Tests contra una URL REMOTA
@my_vcr.use_cassette('test_validate_bad_remote_datajson2')
def test_validate_invalid_remote_datajson_is_invalid2(self, _mock_valid):
def test_validate_invalid_remote_datajson_is_invalid2(self):
""" Testea `is_valid_catalog` contra un data.json remoto invalido."""

res = self.dj.is_valid_catalog(BAD_DATAJSON_URL2)
assert_false(res)

def test_validate_invalid_remote_datajson_has_errors2(self, _mock_valid):
def test_validate_invalid_remote_datajson_has_errors2(self):
""" Testea `validate_catalog` contra un data.json remoto invalido."""
errors = [
([
Expand All @@ -320,7 +316,7 @@ def test_validate_invalid_remote_datajson_has_errors2(self, _mock_valid):
yield self.validate_contains_message, BAD_DATAJSON_URL2,\
path, regex

def test_correctness_of_accrualPeriodicity_regex(self, _mock_valid):
def test_correctness_of_accrualPeriodicity_regex(self):
"""Prueba que la regex de validación de
dataset["accrualPeriodicity"] sea correcta."""

Expand Down Expand Up @@ -348,15 +344,15 @@ def test_correctness_of_accrualPeriodicity_regex(self, _mock_valid):
res = self.dj.is_valid_catalog(datajson)
assert_false(res, msg=value)

def test_valid_catalog_list_format(self, _mock_valid):
def test_valid_catalog_list_format(self):
report_list = self.dj.validate_catalog(fmt='list')
assert_true(len(report_list['catalog']) == 1)
assert_true(report_list['catalog'][0]['catalog_status'] == 'OK')
assert_true(len(report_list['dataset']) == 2)
for report in report_list['dataset']:
assert_true(report['dataset_status'] == 'OK')

def test_invalid_catalog_list_format(self, _mock_valid):
def test_invalid_catalog_list_format(self):
catalog = pydatajson.DataJson(
self.get_sample("several_assorted_errors.json"))
report_list = catalog.validate_catalog(fmt='list')
Expand All @@ -373,18 +369,3 @@ def test_invalid_catalog_list_format(self, _mock_valid):
error['message'] in [
reported['dataset_error_message'] for reported
in report_list['dataset']])

def test_urls_with_status_code_200_is_valid(self, _mock_valid):
assert_true(self.dj.is_valid_catalog())

def test_urls_with_status_code_203_is_valid(self, mock_valid):
mock_valid.return_value = (True, 203)
assert_true(self.dj.is_valid_catalog())

def test_urls_with_status_code_302_is_valid(self, mock_valid):
mock_valid.return_value = (True, 302)
assert_true(self.dj.is_valid_catalog())

def tests_urls_with_invalid_status_codes_are_not_valid(self, mock_valid):
mock_valid.return_value = (False, 404)
assert_false(self.dj.is_valid_catalog())

0 comments on commit 2fe58cc

Please sign in to comment.