<commit_before>from django.test import TestCase
from django.contrib.gis.geos import (LineString, Polygon, MultiPolygon,
                                     MultiLineString, MultiPoint, Point)
from django.core.exceptions import ValidationError
from django.conf import settings

from bs4 import BeautifulSoup

from geotrek.common.tests import TranslationResetMixin
from geotrek.core.factories import PathFactory
from geotrek.zoning.factories import DistrictFactory, CityFactory
from geotrek.trekking.factories import (POIFactory, TrekFactory,
                                        TrekWithPOIsFactory, ServiceFactory)
from geotrek.trekking.models import Trek, OrderedTrekChild


class TrekTest(TranslationResetMixin, TestCase):
    def test_is_publishable(self):
        t = TrekFactory.create()
        t.geom = LineString((0, 0), (1, 1))
        self.assertTrue(t.has_geom_valid())

        t.description_teaser = ''
        self.assertFalse(t.is_complete())
        self.assertFalse(t.is_publishable())
        t.description_teaser = 'ba'
        t.departure = 'zin'
        t.arrival = 'ga'
        self.assertTrue(t.is_complete())
        self.assertTrue(t.is_publishable())

        t.geom = MultiLineString([LineString((0, 0), (1, 1)), LineString((2, 2), (3, 3))])
        self.assertFalse(t.has_geom_valid())
        self.assertFalse(t.is_publishable())

    def test_any_published_property(self):
        t = TrekFactory.create(published=False)
        t.published_fr = False
        t.published_it = False
        t.save()
        self.assertFalse(t.any_published)
        t.published_it = True
        t.save()
        self.assertTrue(t.any_published)

    def test_published_status(self):
        t = TrekFactory.create(published=False)
        t.published_fr = False
        t.published_it = True
        t.save()
        self.assertEqual(t.published_status, [
            {'lang': 'en', 'language': 'English', 'status': False},
            {'lang': 'es', 'language': 'Spanish', 'status': False},
            {'lang': 'fr', 'language': 'French', 'status': False},
            {'lang': 'it', 'language': 'Italian', 'status': True}])

    def test_kml_coordinates_should_be_3d(self):
        trek = TrekWithPOIsFactory.create()
        kml = trek.kml()
        parsed = BeautifulSoup(kml, 'lxml')
        for placemark in parsed.findAll('placemark'):
            coordinates = placemark.find('coordinates')
            tuples = [s.split(',') for s in coordinates.string.split(' ')]
            self.assertTrue(all([len(i) == 3 for i in tuples]))

    def test_pois_types(self):
        trek = TrekWithPOIsFactory.create()
        type0 = trek.pois[0].type
        type1 = trek.pois[1].type
        self.assertEqual(2, len(trek.poi_types))
        self.assertIn(type0, trek.poi_types)
        self.assertIn(type1, trek.poi_types)

    def test_delete_cascade(self):
        p1 = PathFactory.create()
        p2 = PathFactory.create()
        t = TrekFactory.create(no_path=True)
        t.add_path(p1)<commit_msg>Add tests override Published_by_langs<commit_after>from django.test import TestCase
from django.contrib.gis.geos import (LineString, Polygon, MultiPolygon,
                                     MultiLineString, MultiPoint, Point)
from django.core.exceptions import ValidationError
from django.conf import settings
from django.test.utils import override_settings

from bs4 import BeautifulSoup

from geotrek.common.tests import TranslationResetMixin
from geotrek.core.factories import PathFactory
from geotrek.zoning.factories import DistrictFactory, CityFactory
from geotrek.trekking.factories import (POIFactory, TrekFactory,
                                        TrekWithPOIsFactory, ServiceFactory)
from geotrek.trekking.models import Trek, OrderedTrekChild


class TrekTest(TranslationResetMixin, TestCase):
    def test_is_publishable(self):
        t = TrekFactory.create()
        t.geom = LineString((0, 0), (1, 1))
        self.assertTrue(t.has_geom_valid())

        t.description_teaser = ''
        self.assertFalse(t.is_complete())
        self.assertFalse(t.is_publishable())
        t.description_teaser = 'ba'
        t.departure = 'zin'
        t.arrival = 'ga'
        self.assertTrue(t.is_complete())
        self.assertTrue(t.is_publishable())

        t.geom = MultiLineString([LineString((0, 0), (1, 1)), LineString((2, 2), (3, 3))])
        self.assertFalse(t.has_geom_valid())
        self.assertFalse(t.is_publishable())

    def test_any_published_property(self):
        t = TrekFactory.create(published=False)
        t.published_fr = False
        t.published_it = False
        t.save()
        self.assertFalse(t.any_published)
        t.published_it = True
        t.save()
        self.assertTrue(t.any_published)

    @override_settings(PUBLISHED_BY_LANG=False)
    def test_any_published_without_published_by_lang(self):
        t = TrekFactory.create(published=False)
        t.published_fr = True
        t.save()
        self.assertFalse(t.any_published)

    def test_published_status(self):
        t = TrekFactory.create(published=False)
        t.published_fr = False
        t.published_it = True
        t.save()
        self.assertEqual(t.published_status, [
            {'lang': 'en', 'language': 'English', 'status': False},
            {'lang': 'es', 'language': 'Spanish', 'status': False},
            {'lang': 'fr', 'language': 'French', 'status': False},
            {'lang': 'it', 'language': 'Italian', 'status': True}])

    @override_settings(PUBLISHED_BY_LANG=False)
    def test_published_status_without_published_by_lang(self):
        t = TrekFactory.create(published=True)
        t.published_fr = False
        t.published_it = False
        t.save()
        self.assertEqual(t.published_status, [
            {'lang': 'en', 'language': 'English', 'status': True},
            {'lang': 'es', 'language': 'Spanish', 'status': True},
            {'lang': 'fr', 'language': 'French', 'status': True},
            {'lang': 'it', 'language': 'Italian', 'status': True}])

    @override_settings(PUBLISHED_BY_LANG=False)
    def test_published_langs_without_published_by_lang_not_published(self):
        t = TrekFactory.create(published=False)
        t.published_fr = True
        t.published_it = True
        t.save()
        self.assertEqual(t.published_langs, [])

    def test_kml_coordinates_should_be_3d(self):
        trek = TrekWithPOIsFactory.create()
        kml = trek.kml()
        parsed = BeautifulSoup(kml, 'lxml')
        for placemark in parsed.findAll('placemark'):
            coordinates = placemark.find('coordinates')
            tuples = [s.split(',') for s in coordinates.string.split(' ')]
            self.assertTrue(all([len(i) == 3 for i in tuples]))

    def test_pois_types(self):
        trek = TrekWithPOIsFactory.create()
        type0 = trek.pois[0].type
        type1 = trek.pois[1].type
        self.assertEqual(2, len(trek.poi_types))
        self.assertIn(type0, trek.poi_types)
        self.assertIn(type1, trek.poi_types)

    def test_delete_cascade(self):
        p1 = PathFactory.create()
        p2 = PathFactory.create()
        t = TrekFactory.create(no_path=True)
        t.add_path(p1)
{'fraction_non_alphanumeric': 0.12221144519883609, 'fraction_numerical': 0.0069280864625190525, 'mean_word_length': 2.792958486600105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731022', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2204, 'n_words': 427}