def test_import_json(send_mail_function):
    """This test runs with elasticsearch if available and otherwise uses saved responses"""
    # Create the base state
    old = load_ris_data("importer/test-data/amtzell_old.json")
    body = Body(name=old.meta.name, short_name=old.meta.name, ags=old.meta.ags)
    body.save()

    import_data(body, old)

    actual = make_db_snapshot()
    expected = json.loads(
        Path("importer/test-data/amtzell_old_db.json").read_text())
    assert expected == actual

    last_notification = timezone.now()

    # Create notification
    user = User(username="******", email="*****@*****.**")
    user.save()
    UserProfile.objects.create(user=user)

    user_alert = UserAlert(
        user=user,
        search_string="Digitalisierungsstrategie",
        created=datetime.fromisoformat("2008-01-01T12:00:00+01:00"),
    )
    user_alert.save()

    # Import the new data
    new = load_ris_data("importer/test-data/amtzell_new.json")
    import_data(body, new)

    actual = make_db_snapshot()
    expected = json.loads(
        Path("importer/test-data/amtzell_new_db.json").read_text())
    assert expected == actual

    # Check that the notification was sent
    elasticsearch_mock = ElasticsearchMock({
        "importer/test-data/notification_request.json":
        "importer/test-data/notification_response.json"
    })
    if is_es_online():
        context = contextlib.nullcontext()
    else:
        context = mock.patch(
            "elasticsearch_dsl.search.get_connection",
            new=lambda _alias: elasticsearch_mock,
        )
    with context:
        if is_es_online():
            notifier = NotifyUsers(last_notification)
        else:
            notifier = NotifyUsers(
                datetime.fromisoformat("2020-05-17T12:07:37.887853+00:00"))
        notifier.notify_all()

        assert send_mail_function.call_count == 1
        assert send_mail_function.call_args[0][0] == "*****@*****.**"
        assert "Digitalisierungsstrategie" in send_mail_function.call_args[0][
            2]
        assert "Digitalisierungsstrategie" in send_mail_function.call_args[0][
            3]
    return converter.structure(json.loads(Path(path).read_text()), RisData)


def make_db_snapshot():
    snapshot = dict()
    for name, obj in inspect.getmembers(models):
        if (not inspect.isclass(obj) or not issubclass(obj, DefaultFields)
                or obj == DefaultFields):
            continue
        snapshot[name] = sorted(
            list(i) for i in obj.objects_with_deleted.values_list(
                "oparl_id", "deleted"))
    return snapshot


@override_settings(ELASTICSEARCH_ENABLED=is_es_online())
@modify_settings(INSTALLED_APPS={"append": "django_elasticsearch_dsl"})
@mock.patch("mainapp.functions.notify_users.send_mail")
@pytest.mark.django_db
def test_import_json(send_mail_function):
    """This test runs with elasticsearch if available and otherwise uses saved responses"""
    # Create the base state
    old = load_ris_data("importer/test-data/amtzell_old.json")
    body = Body(name=old.meta.name, short_name=old.meta.name, ags=old.meta.ags)
    body.save()

    import_data(body, old)

    actual = make_db_snapshot()
    expected = json.loads(
        Path("importer/test-data/amtzell_old_db.json").read_text())