<commit_before>import json
import re
from html import unescape

import pytest
from django.conf import settings
from django.test.utils import override_settings
from django.utils.html import escape

from adhocracy4.maps.templatetags import maps_tags
from adhocracy4.test import helpers


def test_get_points_empty():
    items = []

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': []
    }


@pytest.mark.django_db
def test_get_points_one(location_factory):
    items = [
        location_factory()
    ]

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': [{
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [1.0, 1.0]
            },
            'properties': {
                'name': '',
                'slug': 'location',
                'url': '/location/1/',
                'image': '',
                'category_icon': '',
                'comments_count': '',
                'positive_rating_count': '',
                'negative_rating_count': ''
            }
        }]
    }


@pytest.mark.django_db
def test_get_points_two(location_factory):
    items = [
        location_factory(),
        location_factory()
    ]

    result = json.loads(maps_tags.get_points(items))

    assert len(result['features']) == 2


@pytest.mark.django_db
def test_get_points_with_properties(location_factory):
    items = [
        location_factory()
    ]

    items[0].comment_count = 2
    items[0].positive_rating_count = 3
    items[0].negative_rating_count = 4

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': [{
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [1.0, 1.0]
            },
            'properties': {
                'name': '',
                'slug': 'location',
                'url': '/location/1/',
                'image': '',
                'category_icon': '',
                'comments_count': 2,
                'positive_rating_count': 3,
                'negative_rating_count': 4
            }
        }]
    }


@override_settings(A4_MAP_BASEURL='https://{s}.tile.openstreetmap.org/')
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_map_display_points(area_settings, location_factory):
    items = [
        location_factory()
    ]

    points = {
        "type": "FeatureCollection",
        "features": [
            {
                "properties": {
                    "name": "",
                    "comments_count": "",
                    "url": "/location/1/",
                    "positive_rating_count": "",
                    "slug": "location",
                    "image": "",
                    "category_icon": "",
                    "negative_rating_count": ""},
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [1.0, 1.0]
                }
            }
        ]
    }

    template = '{% load maps_tags %}{% map_display_points items polygon %}'
    context = {'items': items, 'polygon': area_settings.polygon}

    expected = (
        r'^<div'
        r' style="height: 300px"'
        r' data-map="display_points"'
        r' data-baseurl="{baseurl}"'
        r' data-usevectormap="0"'
        r' data-mapbox-token=""'<commit_msg>tests/maps: Accept any url ids<commit_after>import json
import re
from html import unescape

import pytest
from django.conf import settings
from django.test.utils import override_settings
from django.utils.html import escape

from adhocracy4.maps.templatetags import maps_tags
from adhocracy4.test import helpers
from tests.helpers import pytest_regex


def test_get_points_empty():
    items = []

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': []
    }


@pytest.mark.django_db
def test_get_points_one(location_factory):
    items = [
        location_factory()
    ]

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': [{
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [1.0, 1.0]
            },
            'properties': {
                'name': '',
                'slug': 'location',
                'url': pytest_regex('^/location/[0-9]*/$'),
                'image': '',
                'category_icon': '',
                'comments_count': '',
                'positive_rating_count': '',
                'negative_rating_count': ''
            }
        }]
    }


@pytest.mark.django_db
def test_get_points_two(location_factory):
    items = [
        location_factory(),
        location_factory()
    ]

    result = json.loads(maps_tags.get_points(items))

    assert len(result['features']) == 2


@pytest.mark.django_db
def test_get_points_with_properties(location_factory):
    items = [
        location_factory()
    ]

    items[0].comment_count = 2
    items[0].positive_rating_count = 3
    items[0].negative_rating_count = 4

    result = json.loads(maps_tags.get_points(items))

    assert result == {
        'type': 'FeatureCollection',
        'features': [{
            'type': 'Feature',
            'geometry': {
                'type': 'Point',
                'coordinates': [1.0, 1.0]
            },
            'properties': {
                'name': '',
                'slug': 'location',
                'url': pytest_regex('^/location/[0-9]*/$'),
                'image': '',
                'category_icon': '',
                'comments_count': 2,
                'positive_rating_count': 3,
                'negative_rating_count': 4
            }
        }]
    }


@override_settings(A4_MAP_BASEURL='https://{s}.tile.openstreetmap.org/')
@override_settings(A4_MAP_ATTRIBUTION='<a href="example.com">attribution</a>')
@pytest.mark.django_db
def test_map_display_points(area_settings, location_factory):
    items = [
        location_factory()
    ]

    points = {
        "type": "FeatureCollection",
        "features": [
            {
                "properties": {
                    "name": "",
                    "comments_count": "",
                    "url": pytest_regex('^/location/[0-9]*/$'),
                    "positive_rating_count": "",
                    "slug": "location",
                    "image": "",
                    "category_icon": "",
                    "negative_rating_count": ""},
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [1.0, 1.0]
                }
            }
        ]
    }

    template = '{% load maps_tags %}{% map_display_points items polygon %}'
    context = {'items': items, 'polygon': area_settings.polygon}

    expected = (
        r'^<div'
        r' style="height: 300px"'
        r' data-map="display_points"'
        r' data-baseurl="{baseurl}"'
        r' data-usevectormap="0"'
        r' data-mapbox-token=""'
{'fraction_non_alphanumeric': 0.140500338066261, 'fraction_numerical': 0.009330628803245436, 'mean_word_length': 1.7271386430678466, 'pattern_counts': {'":': 30, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '393558', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2105, 'n_words': 363}