Skip to content

Commit

Permalink
Merge pull request #16 from arthur-schnitzler/42-higher-test-coverage
Browse files Browse the repository at this point in the history
42 higher test coverage
  • Loading branch information
csae8092 authored Jan 2, 2024
2 parents d0980be + 3a993f5 commit c8a5df3
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 211 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% extends "base.html" %}
{% load static %}
{% load django_tables2 %}
{% load browsing_extras %}
{% load crispy_forms_field %}
{% load crispy_forms_tags %}
{% block title %} Alle {{ verbose_name }} {% endblock %}
Expand Down
19 changes: 19 additions & 0 deletions apis_core/apis_entities/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,22 @@ def test_017_import_gndpersonwithoutwikidata(self):
)
response = client.post(url, payload, follow=True)
self.assertEqual(response.status_code, 200)

def test_018_download_vocabs(self):
model_name = "worktype"
url = reverse(
"apis_core:apis_vocabularies:dl-vocabs", kwargs={"model_name": model_name}
)
client.login(**USER)
response = client.get(url, follow=True)
content_disposition = response.headers["Content-Disposition"]
self.assertTrue(model_name in content_disposition)

def test_019_download_vocabs_not_exist(self):
model_name = "doesnotexistworktype"
url = reverse(
"apis_core:apis_vocabularies:dl-vocabs", kwargs={"model_name": model_name}
)
client.login(**USER)
response = client.get(url, follow=True)
self.assertTrue(response.status_code, 404)
14 changes: 11 additions & 3 deletions apis_core/apis_vocabularies/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import pandas as pd
from django.http import HttpResponseNotFound

from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse


@login_required
def dl_vocabs_as_csv(request, model_name):
kwargs = {"app_label": "apis_vocabularies", "entity": model_name}
model = ContentType.objects.get(
app_label=kwargs.get("app_label"), model=kwargs.get("entity").lower()
).model_class()
try:
model = ContentType.objects.get(
app_label=kwargs.get("app_label"), model=kwargs.get("entity").lower()
).model_class()
except ObjectDoesNotExist:
return HttpResponseNotFound(
f"<h1>Error</h1><p>Es konnte keine Klasse mit dem Name <strong>{model_name}</strong> gefunden werden</p>"
)
data = [[x.id, x.name, x.parent_class] for x in model.objects.all()]
columns = ["id", "name", "parent_class"]
df = pd.DataFrame(data, columns=columns)
Expand Down
39 changes: 39 additions & 0 deletions apis_core/openrefine/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.apps import apps
from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse

from apis_core.apis_entities.models import Person

client = Client()
USER = {"username": "testuser", "password": "somepassword"}
BAHR = {"name": "Bahr", "first_name": "Hermann", "start_date_written": "1900"}
DUMMY_OBJECT = {"name": "test", "start_date_written": "1900"}

ENTITY_TYPES = ["person", "place", "event", "work", "institution"]

MODELS = list(apps.all_models["apis_entities"].values())


class OpenrefineTestCase(TestCase):
def setUp(self):
# Create two users
User.objects.create_user(**USER)
Person.objects.create(**BAHR)

def test_01_tei_reconcile(self):
url = reverse("apis:openrefine:reconcile")
response = client.get(url)
self.assertEqual(response.status_code, 200)
response = client.post(url)
self.assertEqual(response.status_code, 200)

def test_02_properties(self):
url = reverse("apis:openrefine:properties")
response = client.get(url)
self.assertEqual(response.status_code, 200)

def test_03_suggest_types(self):
url = reverse("apis:openrefine:suggest_types")
response = client.get(url)
self.assertEqual(response.status_code, 200)
6 changes: 3 additions & 3 deletions apis_core/openrefine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
app_name = "openrefine"

urlpatterns = [
path("reconcile", views.reconcile),
path("properties", views.properties),
path("suggest/type", views.suggest_types),
path("reconcile", views.reconcile, name="reconcile"),
path("properties", views.properties, name="properties"),
path("suggest/type", views.suggest_types, name="suggest_types"),
]
79 changes: 0 additions & 79 deletions browsing/browsing_utils.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import django_tables2
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django.utils.safestring import mark_safe
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django_tables2.export.views import ExportMixin

input_form = """
<input type="checkbox" name="keep" value="{}" title="keep this"/> |
<input type="checkbox" name="remove" value="{}" title="remove this"/>
"""


class MergeColumn(django_tables2.Column):
"""renders a column with to checkbox - used to select objects for merging"""

def __init__(self, *args, **kwargs):
super(MergeColumn, self).__init__(*args, **kwargs)

def render(self, value):
return mark_safe(input_form.format(value, value))


def get_entities_table(model_class):
class GenericEntitiesTable(django_tables2.Table):
Expand Down Expand Up @@ -50,7 +34,6 @@ class GenericListView(ExportMixin, django_tables2.SingleTableView):
paginate_by = 25
template_name = "browsing/generic_list.html"
init_columns = []
enable_merge = False
excluded_cols = []
verbose_name = "Personen"
help_text = "Personen help text"
Expand Down Expand Up @@ -85,7 +68,6 @@ def get_table(self, **kwargs):

def get_context_data(self, **kwargs):
context = super(GenericListView, self).get_context_data()
context["enable_merge"] = self.enable_merge
togglable_colums = {
key: value
for key, value in self.get_all_cols().items()
Expand Down Expand Up @@ -147,65 +129,4 @@ def get_context_data(self, **kwargs):
context = super(BaseUpdateView, self).get_context_data()
context["docstring"] = "{}".format(self.model.__doc__)
context["class_name"] = "{}".format(self.model.__name__)
# if self.model.__name__.endswith('s'):
# context['class_name'] = "{}".format(self.model.__name__)
# else:
# context['class_name'] = "{}s".format(self.model.__name__)
return context


def model_to_dict(instance):
"""
serializes a model.object to dict, including non editable fields as well as
ManyToManyField fields
"""
field_dicts = []
for x in instance._meta.get_fields():
f_type = "{}".format(type(x))
field_dict = {
"name": x.name,
"help_text": getattr(x, "help_text", ""),
}
try:
field_dict["extra_fields"] = x.extra
except AttributeError:
field_dict["extra_fields"] = None
if "reverse_related" in f_type:
values = getattr(instance, x.name, None)
if values is not None:
field_dict["value"] = values.all()
else:
field_dict["value"] = []
if getattr(x, "related_name", None) is not None:
field_dict["verbose_name"] = getattr(x, "related_name", x.name)
else:
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["f_type"] = "REVRESE_RELATION"
elif "related.ForeignKey" in f_type:
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["value"] = getattr(instance, x.name, "")
field_dict["f_type"] = "FK"
elif "TreeForeignKey" in f_type:
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["value"] = getattr(instance, x.name, "")
field_dict["f_type"] = "FK"
elif "related.ManyToManyField" in f_type:
values = getattr(instance, x.name, None)
if values is not None:
field_dict["value"] = values.all()
else:
field_dict["value"] = []
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["f_type"] = "M2M"
elif "fields.DateTimeField" in f_type:
field_value = getattr(instance, x.name, "")
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["f_type"] = "DateTime"
if field_value:
field_dict["value"] = field_value.strftime("%Y-%m-%d %H:%M:%S")
else:
field_dict["verbose_name"] = getattr(x, "verbose_name", x.name)
field_dict["value"] = f"{getattr(instance, x.name, '')}"
field_dict["f_type"] = "SIMPLE"
field_dicts.append(field_dict)
return field_dicts
1 change: 0 additions & 1 deletion browsing/templates/browsing/tags/class_definition.html

This file was deleted.

23 changes: 0 additions & 23 deletions browsing/templates/browsing/tags/column_selector.html

This file was deleted.

Empty file removed browsing/templatetags/__init__.py
Empty file.
50 changes: 0 additions & 50 deletions browsing/templatetags/browsing_extras.py

This file was deleted.

9 changes: 0 additions & 9 deletions browsing/urls.py

This file was deleted.

40 changes: 0 additions & 40 deletions browsing/views.py

This file was deleted.

3 changes: 2 additions & 1 deletion dumper/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def test_03_beacon(self):
self.assertTrue(uris.count())

def test_04_wikidata_minter(self):
Uri.objects.filter(domain="wikidata").first().delete()
before_uris = Uri.objects.all().count()
call_command("wikidata_minter")
after_uris = Uri.objects.all().count()
self.assertEqual(before_uris, after_uris)
self.assertTrue(before_uris < after_uris)

def test_05_startpage(self):
url = reverse("dumper:home")
Expand Down
1 change: 0 additions & 1 deletion pmb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
path("admin/", admin.site.urls),
path("arche/", include("archemd.urls", namespace="archemd")),
path("", include("dumper.urls", namespace="dumper")),
path("browsing", include("browsing.urls", namespace="browsing")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

0 comments on commit c8a5df3

Please sign in to comment.