def test_query(self): datapackage = json.loads(open('fixtures/datapackage.json').read()) datapackage['owner'] = 'demo' catalog = Catalog() catalog.load([datapackage]) self.assertEqual(catalog.query(), [datapackage])
def test_adds_local_urls(self): descriptor = { 'name': 'test', 'resources': [{ 'name': 'first' }, { 'name': 'second' }] } with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = MetaDataDB(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) dataset = catalog.construct_dataset('http://example.com/') self.assertEqual(dataset.\ get('resources')[0].get('localurl'), 'http://example.com/api/dataproxy/demo/demo-package/r/first.csv') self.assertEqual(dataset.\ get('resources')[1].get('localurl'), 'http://example.com/api/dataproxy/demo/demo-package/r/second.csv')
def test_load(self): datapackage = json.loads(open('fixtures/datapackage.json').read()) datapackage['owner'] = 'demo' catalog = Catalog() catalog.load([datapackage]) self.assertIn('demo', catalog._cache) self.assertIn('demo-package', catalog._cache['demo']) self.assertEqual(catalog._cache['demo']['demo-package'], datapackage)
def test_adds_empty_readme_if_there_is_not(self): descriptor = {'name': 'test', 'resources': []} with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = MetaDataDB(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) dataset = catalog.construct_dataset() self.assertEqual(dataset.get('readme'), '')
def test_get_views_if_views_dont_exist(self): descriptor = {'name': 'test', 'resources': []} with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = MetaDataDB(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) views = catalog.get_views() self.assertEqual(views, [])
def test_construct_dataset(self): descriptor = json.loads(open('fixtures/datapackage.json').read()) with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = MetaDataDB(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) dataset = catalog.construct_dataset() self.assertEqual(dataset.get('name'), descriptor.get('name')) self.assertIn('localurl', dataset.get('resources')[0]) self.assertNotEqual(len(dataset.get('views')), 0)
def datapackage_show(publisher, package): """ Loads datapackage page for given owner --- tags: - site parameters: - name: publisher in: path type: string required: true description: datapackage owner name - name: package in: path type: string description: datapackage name responses: 404: description: Datapackage does not exist 200: description: Succesfuly loaded """ metadata = json.loads( app.test_client().\ get('/api/package/{publisher}/{package}'.\ format(publisher=publisher, package=package)).data) try: if metadata['error_code'] == 'DATA_NOT_FOUND': return "404 Not Found", 404 except: pass catalog = Catalog(metadata) dataset = catalog.construct_dataset(request.url_root) dataViews = catalog.get_views() return render_template("dataset.html", dataset=dataset, showDataApi=True, jsonDataPackage=dataset, dataViews=dataViews, zappa_env=get_zappa_prefix(), s3_cdn=get_s3_cdn_prefix()), 200
def test_get_views(self): descriptor = { 'name': 'test', 'resources': [], 'views': [{"type": "graph"}] } with self.app.app_context(): db.drop_all() db.create_all() publisher = Publisher(name=self.publisher) metadata = MetaDataDB(name=self.package) metadata.descriptor = json.dumps(descriptor) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() response = self.client.get('/api/package/%s/%s'%\ (self.publisher, self.package)) catalog = Catalog(json.loads(response.data)) views = catalog.get_views() self.assertNotEqual(len(views), 0) self.assertEqual(views[0].get('type'), 'graph')
def test_get(self): datapackage = json.loads(open('fixtures/datapackage.json').read()) datapackage['owner'] = 'demo' catalog = Catalog() catalog.load([datapackage]) result = catalog.get('demo', 'demo-package') self.assertEqual(result, datapackage) # test unknown owner result = catalog.get('anon', 'demo-package') self.assertIsNone(result)
from flask import Blueprint, render_template, json from flask import current_app as app from app.mod_site.models import Catalog mod_site = Blueprint('site', __name__) catalog = Catalog() @mod_site.route("/", methods=["GET"]) def home(): """ Loads home page --- tags: - site responses: 404: description: Publiser does not exist 200: description: Succesfuly loaded home page """ return render_template("index.html", title='Home'), 200 @mod_site.route("/<owner>/<id>", methods=["GET"]) def datapackage_show(owner, id): """ Loads datapackage page for given owner --- tags: