Skip to content

Commit

Permalink
Move mock data to factories
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaudDauce committed Jan 30, 2024
1 parent bd68e92 commit 1cd555d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
33 changes: 33 additions & 0 deletions udata/core/dataset/factories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import factory

import json
from os.path import join

from udata.app import ROOT_DIR
from udata.factories import ModelFactory

from .models import Dataset, Resource, Checksum, CommunityResource, License
Expand Down Expand Up @@ -72,3 +76,32 @@ class Meta:
id = factory.Faker('unique_string')
title = factory.Faker('sentence')
url = factory.Faker('uri')

class ResourceSchemaMockData():
@staticmethod
def get_mock_data():
return json.load(open(join(ROOT_DIR, 'tests', 'schemas.json')))

@staticmethod
def get_expected_v1_result_from_mock_data():
return [
{
"id": "etalab/schema-irve-statique",
"label": "IRVE statique",
"versions": [
"2.2.0",
"2.2.1"
]
},
{
"id": "139bercy/format-commande-publique",
"label": "Données essentielles des marchés publics français",
"versions": [
"1.3.0",
"1.4.0",
"1.5.0",
"2.0.0",
"2.0.1"
]
}
]
32 changes: 1 addition & 31 deletions udata/core/dataset/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import json
import logging
from pprint import pprint
from os.path import join

from datetime import datetime, timedelta
from collections import OrderedDict
Expand All @@ -17,7 +14,7 @@
from werkzeug.utils import cached_property
import requests

from udata.app import cache, ROOT_DIR
from udata.app import cache
from udata.core import storages
from udata.frontend.markdown import mdstrip
from udata.models import db, WithMetrics, BadgeMixin, SpatialCoverage
Expand Down Expand Up @@ -1006,33 +1003,6 @@ def all():

return schemas

@staticmethod
def get_mock_data():
return json.load(open(join(ROOT_DIR, 'tests', 'schemas.json')))

@staticmethod
def get_expected_v1_result_from_mock_data():
return [
{
"id": "etalab/schema-irve-statique",
"label": "IRVE statique",
"versions": [
"2.2.0",
"2.2.1"
]
},
{
"id": "139bercy/format-commande-publique",
"label": "Données essentielles des marchés publics français",
"versions": [
"1.3.0",
"1.4.0",
"1.5.0",
"2.0.0",
"2.0.1"
]
}
]

def get_resource(id):
'''Fetch a resource given its UUID'''
Expand Down
21 changes: 10 additions & 11 deletions udata/tests/api/test_datasets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
from datetime import datetime
from io import BytesIO
from uuid import uuid4
from os.path import join

import pytest
import pytz
from flask import url_for
import requests_mock

from udata.api import fields
from udata.app import ROOT_DIR, cache
from udata.app import cache
from udata.core import storages
from udata.core.badges.factories import badge_factory
from udata.core.dataset.api_fields import (dataset_harvest_fields,
resource_harvest_fields)
from udata.core.dataset.factories import (CommunityResourceFactory,
DatasetFactory, LicenseFactory,
ResourceFactory,
ResourceFactory, ResourceSchemaMockData,
VisibleDatasetFactory)
from udata.core.dataset.models import (HarvestDatasetMetadata,
HarvestResourceMetadata, ResourceMixin, ResourceSchema)
HarvestResourceMetadata, ResourceMixin)
from udata.core.organization.factories import OrganizationFactory
from udata.core.spatial.factories import SpatialCoverageFactory
from udata.core.topic.factories import TopicFactory
Expand Down Expand Up @@ -722,7 +721,7 @@ def test_dataset_api_unfeature_already(self):
@requests_mock.Mocker(kw='rmock')
def test_dataset_new_resource_with_schema(self, rmock):
'''Tests api validation to prevent schema creation with a name and a url'''
rmock.get('https://example.com/schemas', json=ResourceSchema.get_mock_data())
rmock.get('https://example.com/schemas', json=ResourceSchemaMockData.get_mock_data())

user = self.login()
dataset = DatasetFactory(owner=user)
Expand Down Expand Up @@ -1702,11 +1701,11 @@ def test_dataset_schemas_api_list(self, api, rmock, app):
# made before setting up rmock at module load, resulting in a 404
app.config['SCHEMA_CATALOG_URL'] = 'https://example.com/schemas'

rmock.get('https://example.com/schemas', json=ResourceSchema.get_mock_data())
rmock.get('https://example.com/schemas', json=ResourceSchemaMockData.get_mock_data())
response = api.get(url_for('api.schemas'))

assert200(response)
assert response.json == ResourceSchema.get_expected_v1_result_from_mock_data()
assert response.json == ResourceSchemaMockData.get_expected_v1_result_from_mock_data()

@pytest.mark.options(SCHEMA_CATALOG_URL=None)
def test_dataset_schemas_api_list_no_catalog_url(self, api):
Expand All @@ -1730,13 +1729,13 @@ def test_dataset_schemas_api_list_error_no_cache(self, api, rmock):
@pytest.mark.options(SCHEMA_CATALOG_URL='https://example.com/schemas')
def test_dataset_schemas_api_list_error_w_cache(self, api, rmock, mocker):
cache_mock_set = mocker.patch.object(cache, 'set')
mocker.patch.object(cache, 'get', return_value=ResourceSchema.get_mock_data()['schemas'])
mocker.patch.object(cache, 'get', return_value=ResourceSchemaMockData.get_mock_data()['schemas'])

# Fill cache
rmock.get('https://example.com/schemas', json=ResourceSchema.get_mock_data())
rmock.get('https://example.com/schemas', json=ResourceSchemaMockData.get_mock_data())
response = api.get(url_for('api.schemas'))
assert200(response)
assert response.json == ResourceSchema.get_expected_v1_result_from_mock_data()
assert response.json == ResourceSchemaMockData.get_expected_v1_result_from_mock_data()
assert cache_mock_set.called

# Endpoint becomes unavailable
Expand All @@ -1745,7 +1744,7 @@ def test_dataset_schemas_api_list_error_w_cache(self, api, rmock, mocker):
# Long term cache is used
response = api.get(url_for('api.schemas'))
assert200(response)
assert response.json == ResourceSchema.get_expected_v1_result_from_mock_data()
assert response.json == ResourceSchemaMockData.get_expected_v1_result_from_mock_data()


@pytest.mark.usefixtures('clean_db')
Expand Down
8 changes: 4 additions & 4 deletions udata/tests/dataset/test_dataset_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from udata.core.dataset.models import HarvestDatasetMetadata, HarvestResourceMetadata
from udata.core.dataset.factories import (
ResourceFactory, DatasetFactory, CommunityResourceFactory, LicenseFactory
ResourceFactory, DatasetFactory, CommunityResourceFactory, LicenseFactory, ResourceSchemaMockData
)
from udata.core.dataset.exceptions import (
SchemasCatalogNotFoundException, SchemasCacheUnavailableException
Expand Down Expand Up @@ -565,13 +565,13 @@ def test_resource_schema_objects_w_cache(self, rmock, mocker):
cache_mock_set = mocker.patch.object(cache, 'set')

# fill cache
rmock.get('https://example.com/schemas', json=ResourceSchema.get_mock_data())
rmock.get('https://example.com/schemas', json=ResourceSchemaMockData.get_mock_data())
ResourceSchema.objects()
assert cache_mock_set.called

mocker.patch.object(cache, 'get', return_value=ResourceSchema.get_mock_data()['schemas'])
mocker.patch.object(cache, 'get', return_value=ResourceSchemaMockData.get_mock_data()['schemas'])
rmock.get('https://example.com/schemas', status_code=500)
assert ResourceSchema.get_expected_v1_result_from_mock_data() == ResourceSchema.objects()
assert ResourceSchemaMockData.get_expected_v1_result_from_mock_data() == ResourceSchema.objects()
assert rmock.call_count == 2

def test_resource_schema_validation(self):
Expand Down

0 comments on commit 1cd555d

Please sign in to comment.