def xlsx_catalog():
    with open_catalog('catalogo-justicia_valido.xlsx') as catalog_fd:
        model = CatalogUpload(format=CatalogUpload.FORMAT_XLSX,
                              xlsx_file=File(catalog_fd),
                              node=_node())
        model.save()

    return model
def _catalog():
    with open_catalog('data.json') as catalog_fd:
        model = CatalogUpload(format=CatalogUpload.FORMAT_JSON,
                              json_file=File(catalog_fd),
                              node=_node())
        model.save()

    return model
Beispiel #3
0
def test_same_day_multiple_catalog_uploads(node):
    with open_catalog('data.json') as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        CatalogUpload.create_from_url_or_file(data_dict)

    with open_catalog('data.json') as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        CatalogUpload.create_from_url_or_file(data_dict)

    assert CatalogUpload.objects.count() == 1
Beispiel #4
0
def test_catalog_history_has_object_list_ordered_by_date_descendent(
        admin_client, catalog):
    lastweek = datetime.datetime.now() - datetime.timedelta(days=7)
    CatalogUpload.objects.filter(pk=catalog.pk).update(uploaded_at=lastweek)
    with open_catalog('data.json') as catalog_fd:
        model = CatalogUpload(format=CatalogUpload.FORMAT_JSON,
                              json_file=File(catalog_fd),
                              node=catalog.node)
        model.save()

    response = admin_client.get(
        reverse('catalog:catalog_history', kwargs={'node_id':
                                                   catalog.node.id}))
    catalogs = response.context['object_list']
    for i in range(len(catalogs) - 1):
        assert catalogs[i].uploaded_at > catalogs[i + 1].uploaded_at
Beispiel #5
0
def test_create_from_file(node):
    filename = 'data.json'
    with open_catalog(filename) as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        catalog = CatalogUpload.create_from_url_or_file(data_dict)
        assert b'dataset' in catalog.json_file.read()
Beispiel #6
0
def test_catalog_upload_creates_both_formats(node):
    filename = 'data.json'
    with open_catalog(filename) as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        catalog = CatalogUpload.create_from_url_or_file(data_dict)
        assert catalog.json_file is not None
        assert catalog.xlsx_file is not None
Beispiel #7
0
def test_create_from_valid_url(node, requests_mock):
    with open_catalog('data.json') as sample:
        requests_mock.get("https://datos.gob.ar/data.json",
                          content=sample.read())
        data_dict = {
            'format': 'json',
            'node': node,
            'url': "https://datos.gob.ar/data.json"
        }
        catalog = CatalogUpload.create_from_url_or_file(data_dict)
        assert catalog is not None
Beispiel #8
0
def test_validate_returns_error_message_if_catalog_is_not_valid(node):
    error_messages = [
        "'publisher' is a required property",
        "'title' is a required property",
        "'superThemeTaxonomy' is a required property",
        "'description' is a required property",
        "'Índice-precios-internos-basicos-al-por-mayor-desagregado-base-1993-anual.csv' "
        "is not valid under any of the given schemas",
    ]

    with open_catalog('data.json') as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        catalog_upload = CatalogUpload.create_from_url_or_file(data_dict)
        validation_result = catalog_upload.validate()

    for error_message in error_messages:
        assert error_message in validation_result
Beispiel #9
0
    def post(self, request, *args, **kwargs):
        node_id = self.kwargs.get('node_id')
        self.success_url = reverse_lazy('catalog:upload_success',
                                        kwargs={'node_id': node_id})
        form = CatalogForm(request.POST, request.FILES)
        if not form.is_valid():
            return self.form_invalid(form)

        try:
            raw_data = form.cleaned_data
            raw_data['node'] = Node.objects.get(pk=node_id)
            catalog = CatalogUpload.create_from_url_or_file(raw_data)
        except ValidationError as e:
            messages.error(request, e)
            return self.form_invalid(form)

        validation_error_messages = catalog.validate()
        for error_message in validation_error_messages:
            messages.info(request, error_message)
        return self.form_valid(form)
Beispiel #10
0
def test_create_from_not_valid_url(node):
    url = "http://www.google.com"
    data_dict = {'format': 'json', 'node': node, 'url': url}
    with pytest.raises(ValidationError):
        CatalogUpload.create_from_url_or_file(data_dict)
Beispiel #11
0
def test_create_from_not_valid_file(node):
    with open_catalog('simple.json') as sample:
        temp_file = temp_uploaded_file(sample)
        data_dict = {'format': 'json', 'node': node, 'file': temp_file}
        with pytest.raises(ValidationError):
            CatalogUpload.create_from_url_or_file(data_dict)
Beispiel #12
0
def test_catalog_can_only_have_valid_formats(node):
    with pytest.raises(ValidationError):
        catalog = CatalogUpload(format='inva', node=node)
        catalog.save()