Esempio n. 1
0
    def test_with_nested_initial_elements(self):
        fake = Fake.objects.create(nested=[
            Nested(name=faker.name(), sub=SubNested(name=faker.word())),
            Nested(name=faker.name(), sub=SubNested(name=faker.word())),
        ])
        order = [n.id for n in fake.nested]
        data = [
            {'id': str(fake.nested[0].id)},
            {'id': str(fake.nested[1].id)},
            {'name': faker.name(), 'sub': {'name': faker.name()}},
        ]
        form = self.factory({'nested': data}, fake, sub=True)

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(len(fake.nested), 3)
        for idx, id in enumerate(order):
            nested = fake.nested[idx]
            self.assertEqual(nested.id, id)
            self.assertIsNotNone(nested.sub)
            self.assertIsNotNone(nested.sub.name)
        self.assertIsNotNone(fake.nested[2].id)
        self.assertEqual(fake.nested[2].sub.name, data[2]['sub']['name'])
Esempio n. 2
0
    def test_suggest_zones_on_extra_key(self):
        '''It should suggest zones based on any key'''
        with self.autoindex():
            for i in range(4):
                GeoZoneFactory(name='in' if i % 2 else 'not-in',
                               keys={
                                   str(i):
                                   'test-{0}'.format(i) if i %
                                   2 else faker.word()
                               })

        response = self.get(url_for('api.suggest_zones'),
                            qs={
                                'q': 'test',
                                'size': '5'
                            })
        self.assert200(response)

        self.assertEqual(len(response.json), 2)

        for suggestion in response.json:
            self.assertIn('id', suggestion)
            self.assertIn('name', suggestion)
            self.assertIn('code', suggestion)
            self.assertIn('level', suggestion)
            self.assertIn('keys', suggestion)
            self.assertIsInstance(suggestion['keys'], dict)
            self.assertEqual(suggestion['name'], 'in')
Esempio n. 3
0
    def test_create_with_non_submitted_elements(self):
        form = self.factory({'name': faker.word()})

        form.validate()
        self.assertEqual(form.errors, {})

        fake = form.save()

        self.assertIsNotNone(fake.name)
        self.assertIsNone(fake.nested)
Esempio n. 4
0
def hit_factory():
    return {
        "_score": float(faker.random_number(2)),
        "_type": "fake",
        "_id": faker.md5(),
        "_source": {
            "title": faker.sentence(),
            "tags": [faker.word() for _ in range(faker.random_digit())]
        },
        "_index": "udata-test"
    }
Esempio n. 5
0
def hit_factory():
    return {
        "_score": float(faker.random_number(2)),
        "_type": "fake",
        "_id": faker.md5(),
        "_source": {
            "title": faker.sentence(),
            "tags": [faker.word() for _ in range(faker.random_digit())]
        },
        "_index": "udata-test"
    }
Esempio n. 6
0
    def test_create_source_with_owner(self):
        '''It should create and attach a new source to an owner'''
        user = self.login()
        data = {'name': faker.word(), 'url': faker.url(), 'backend': 'factory'}
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert201(response)

        source = response.json
        self.assertEqual(source['validation']['state'], VALIDATION_PENDING)
        self.assertEqual(source['owner']['id'], str(user.id))
        self.assertIsNone(source['organization'])
Esempio n. 7
0
    def test_with_non_submitted_initial_elements(self):
        fake = Fake.objects.create(nested=Nested(name=faker.name()))
        initial_id = fake.nested.id
        initial_name = fake.nested.name
        form = self.factory({'name': faker.word()}, fake)

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(fake.nested.id, initial_id)
        self.assertEqual(fake.nested.name, initial_name)
Esempio n. 8
0
    def test_create_source_with_org_not_member(self):
        '''It should create and attach a new source to an organization'''
        user = self.login()
        member = Member(user=user, role='editor')
        org = OrganizationFactory(members=[member])
        data = {
            'name': faker.word(),
            'url': faker.url(),
            'backend': 'factory',
            'organization': str(org.id)
        }
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert403(response)
Esempio n. 9
0
    def test_create_source_with_org_not_member(self):
        '''It should create and attach a new source to an organization'''
        user = self.login()
        member = Member(user=user, role='editor')
        org = OrganizationFactory(members=[member])
        data = {
            'name': faker.word(),
            'url': faker.url(),
            'backend': 'factory',
            'organization': str(org.id)
        }
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert403(response)
Esempio n. 10
0
    def test_from_response(self):
        response = es_factory()
        response['facets'] = {
            'test': {
                '_type': 'terms',
                'total': 229,
                'other': 33,
                'missing': 2,
                'terms': [{'term': faker.word(), 'count': faker.random_number(2)} for _ in range(10)],
            }
        }

        extracted = self.facet.from_response('test', response)
        self.assertEqual(extracted['type'], 'terms')
        self.assertEqual(len(extracted['terms']), 10)
Esempio n. 11
0
    def test_create_source_with_owner(self):
        '''It should create and attach a new source to an owner'''
        user = self.login()
        data = {
            'name': faker.word(),
            'url': faker.url(),
            'backend': 'factory'
        }
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert201(response)

        source = response.json
        self.assertEqual(source['validation']['state'], VALIDATION_PENDING)
        self.assertEqual(source['owner']['id'], str(user.id))
        self.assertIsNone(source['organization'])
Esempio n. 12
0
    def test_with_non_submitted_initial_elements(self):
        fake = Fake.objects.create(nested=[
            Nested(name=faker.name()),
            Nested(name=faker.name()),
        ])
        initial = [(n.id, n.name) for n in fake.nested]
        form = self.factory({'name': faker.word()}, fake)

        form.validate()
        self.assertEqual(form.errors, {})

        form.populate_obj(fake)

        self.assertEqual(len(fake.nested), len(initial))
        for idx, (id, name) in enumerate(initial):
            nested = fake.nested[idx]
            self.assertEqual(nested.id, id)
            self.assertEqual(nested.name, name)
Esempio n. 13
0
    def test_create_source_with_org(self):
        '''It should create and attach a new source to an organization'''
        user = self.login()
        member = Member(user=user, role='admin')
        org = OrganizationFactory(members=[member])
        data = {
            'name': faker.word(),
            'url': faker.url(),
            'backend': 'factory',
            'organization': str(org.id)
        }
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert201(response)

        source = response.json
        self.assertEqual(source['validation']['state'], VALIDATION_PENDING)
        self.assertIsNone(source['owner'])
        self.assertEqual(source['organization']['id'], str(org.id))
Esempio n. 14
0
    def test_create_source_with_org(self):
        '''It should create and attach a new source to an organization'''
        user = self.login()
        member = Member(user=user, role='admin')
        org = OrganizationFactory(members=[member])
        data = {
            'name': faker.word(),
            'url': faker.url(),
            'backend': 'factory',
            'organization': str(org.id)
        }
        response = self.post(url_for('api.harvest_sources'), data)

        self.assert201(response)

        source = response.json
        self.assertEqual(source['validation']['state'], VALIDATION_PENDING)
        self.assertIsNone(source['owner'])
        self.assertEqual(source['organization']['id'], str(org.id))
Esempio n. 15
0
    def test_suggest_territory_on_code(self):
        '''It should suggest territory based on its code'''
        with self.autoindex():
            for i in range(4):
                TerritoryFactory(code='test-{0}'.format(i) if i % 2 else faker.word())

        response = self.get(url_for('api.suggest_territories'), qs={'q': 'test', 'size': '5'})
        self.assert200(response)

        self.assertEqual(len(response.json), 2)

        for suggestion in response.json:
            self.assertIn('id', suggestion)
            self.assertIn('name', suggestion)
            self.assertIn('code', suggestion)
            self.assertIn('level', suggestion)
            self.assertIn('keys', suggestion)
            self.assertIsInstance(suggestion['keys'], dict)
            self.assertTrue(suggestion['code'].startswith('test'))
Esempio n. 16
0
    def test_from_response(self):
        response = es_factory()
        response['facets'] = {
            'test': {
                '_type':
                'terms',
                'total':
                229,
                'other':
                33,
                'missing':
                2,
                'terms': [{
                    'term': faker.word(),
                    'count': faker.random_number(2)
                } for _ in range(10)],
            }
        }

        extracted = self.facet.from_response('test', response)
        self.assertEqual(extracted['type'], 'terms')
        self.assertEqual(len(extracted['terms']), 10)
Esempio n. 17
0
    def test_suggest_territory_on_extra_key(self):
        '''It should suggest territory based on any key'''
        with self.autoindex():
            for i in range(4):
                TerritoryFactory(
                    name='in' if i % 2 else 'not-in',
                    keys={str(i): 'test-{0}'.format(i) if i % 2 else faker.word()}
                )

        response = self.get(url_for('api.suggest_territories'), qs={'q': 'test', 'size': '5'})
        self.assert200(response)

        self.assertEqual(len(response.json), 2)

        for suggestion in response.json:
            self.assertIn('id', suggestion)
            self.assertIn('name', suggestion)
            self.assertIn('code', suggestion)
            self.assertIn('level', suggestion)
            self.assertIn('keys', suggestion)
            self.assertIsInstance(suggestion['keys'], dict)
            self.assertEqual(suggestion['name'], 'in')
Esempio n. 18
0
    def test_suggest_zones_on_code(self):
        '''It should suggest zones based on its code'''
        with self.autoindex():
            for i in range(4):
                GeoZoneFactory(code='test-{0}'.format(i) if i %
                               2 else faker.word())

        response = self.get(url_for('api.suggest_zones'),
                            qs={
                                'q': 'test',
                                'size': '5'
                            })
        self.assert200(response)

        self.assertEqual(len(response.json), 2)

        for suggestion in response.json:
            self.assertIn('id', suggestion)
            self.assertIn('name', suggestion)
            self.assertIn('code', suggestion)
            self.assertIn('level', suggestion)
            self.assertIn('keys', suggestion)
            self.assertIsInstance(suggestion['keys'], dict)
            self.assertTrue(suggestion['code'].startswith('test'))
Esempio n. 19
0
def package_show_factory(name):
    org_id = str(uuid4())
    created = faker.date_time_between(start_date='-3y', end_date='-7d')
    updated = faker.date_time_between(start_date='-7d', end_date='now')
    nb_resources = faker.randomize_nb_elements(4)
    nb_tags = faker.randomize_nb_elements(10)
    return {
        "help": "",
        "success": True,
        "result": {
            "license_title":
            "Licence ouverte / Open license (Etalab)",
            "maintainer":
            None,
            "relationships_as_object": [],
            "private":
            False,
            "maintainer_email":
            None,
            "revision_timestamp":
            updated.isoformat(),
            "id":
            str(uuid4()),
            "metadata_created":
            created.isoformat(),
            "owner_org":
            org_id,
            "metadata_modified":
            updated.isoformat(),
            "author":
            None,
            "author_email":
            None,
            "state":
            "active",
            "version":
            None,
            "license_id":
            "lool",
            "type":
            "dataset",
            "resources": [resource_factory() for _ in range(nb_resources)],
            "num_resources":
            nb_resources,
            "tags": [tag_factory(faker.word()) for _ in range(nb_tags)],
            "tracking_summary": {
                "total": 0,
                "recent": 0
            },
            "groups": [{
                "title": " Actions Région",
                "description":
                "Les actions régionales, l'agriculture, l'aménagement du territoire...",
                "name": "actions-region",
                "id": "7859d498-316e-4a31-8487-f7f26df0929e"
            }],
            "relationships_as_subject": [],
            "num_tags":
            nb_tags,
            "name":
            faker.slug(),
            "isopen":
            False,
            "url":
            None,
            "notes":
            faker.paragraph(),
            "title":
            faker.sentence(),
            "extras": [{
                "value": "01/08/2013",
                "key": "date",
                "__extras": {
                    "package_id": "f9f07d12-f810-4cb5-b3c0-52d7b1130c2e",
                    "revision_id": "e108a7c6-0e5f-4714-a8e6-68162c69ae68"
                }
            }, {
                "value": "Annuelle",
                "key": "freqMAJ",
                "__extras": {
                    "revision_id": "e108a7c6-0e5f-4714-a8e6-68162c69ae68",
                    "package_id": "f9f07d12-f810-4cb5-b3c0-52d7b1130c2e"
                }
            }],
            "license_url":
            "http://www.etalab.gouv.fr/pages/Licence_ouverte_Open_licence-5899923.html",
            "organization": {
                "description": "",
                "created": "2013-07-18T11:25:15.969631",
                "title": "Conseil Régional Nord-Pas de Calais",
                "name": "conseil-regional-nord-pas-de-calais",
                "revision_timestamp": "2013-07-29T15:25:28.634142",
                "is_organization": True,
                "state": "active",
                "image_url": "/images/npdc-icon.png",
                "revision_id": str(uuid4()),
                "type": "organization",
                "id": org_id,
                "approval_status": "approved"
            },
            "revision_id":
            str(uuid4())
        }
    }
Esempio n. 20
0
def package_show_factory(name):
    org_id = str(uuid4())
    created = faker.date_time_between(start_date='-3y', end_date='-7d')
    updated = faker.date_time_between(start_date='-7d', end_date='now')
    nb_resources = faker.randomize_nb_elements(4)
    nb_tags = faker.randomize_nb_elements(10)
    return {
        "help": "",
        "success": True,
        "result": {
            "license_title": "Licence ouverte / Open license (Etalab)",
            "maintainer": None,
            "relationships_as_object": [],
            "private": False,
            "maintainer_email": None,
            "revision_timestamp": updated.isoformat(),
            "id": str(uuid4()),
            "metadata_created": created.isoformat(),
            "owner_org": org_id,
            "metadata_modified": updated.isoformat(),
            "author": None,
            "author_email": None,
            "state": "active",
            "version": None,
            "license_id": "lool",
            "type": "dataset",
            "resources": [resource_factory()
                          for _ in range(nb_resources)],
            "num_resources": nb_resources,
            "tags": [tag_factory(faker.word())
                     for _ in range(nb_tags)],
            "tracking_summary": {
                "total": 0,
                "recent": 0
            },
            "groups": [{
                "title": " Actions Région",
                "description": "Les actions régionales, l'agriculture, l'aménagement du territoire...",
                "name": "actions-region",
                "id": "7859d498-316e-4a31-8487-f7f26df0929e"
            }],
            "relationships_as_subject": [],
            "num_tags": nb_tags,
            "name": faker.slug(),
            "isopen": False,
            "url": None,
            "notes": faker.paragraph(),
            "title": faker.sentence(),
            "extras": [{
                "value": "01/08/2013",
                "key": "date",
                "__extras": {
                    "package_id": "f9f07d12-f810-4cb5-b3c0-52d7b1130c2e",
                    "revision_id": "e108a7c6-0e5f-4714-a8e6-68162c69ae68"
                }
            }, {
                "value": "Annuelle",
                "key": "freqMAJ",
                "__extras": {
                    "revision_id": "e108a7c6-0e5f-4714-a8e6-68162c69ae68",
                    "package_id": "f9f07d12-f810-4cb5-b3c0-52d7b1130c2e"
                }
            }],
            "license_url": "http://www.etalab.gouv.fr/pages/Licence_ouverte_Open_licence-5899923.html",
            "organization": {
                "description": "",
                "created": "2013-07-18T11:25:15.969631",
                "title": "Conseil Régional Nord-Pas de Calais",
                "name": "conseil-regional-nord-pas-de-calais",
                "revision_timestamp": "2013-07-29T15:25:28.634142",
                "is_organization": True,
                "state": "active",
                "image_url": "/images/npdc-icon.png",
                "revision_id": str(uuid4()),
                "type": "organization",
                "id": org_id,
                "approval_status": "approved"
            },
            "revision_id": str(uuid4())
        }
    }