<commit_before># -*- encoding: utf-8 -*-

import io
import json
import mock
import os

from django.test import TestCase
from django.core.management import call_command

from geotrek.common.models import Attachment, FileType
from geotrek.common.tests import TranslationResetMixin
from geotrek.tourism.factories import TouristicContentCategoryFactory, TouristicContentTypeFactory
from geotrek.tourism.models import TouristicContent, TouristicEvent
from geotrek.tourism.parsers import TouristicContentApidaeParser, EspritParcParser


class EauViveParser(TouristicContentApidaeParser):
    category = u"Eau vive"
    type1 = [u"Type A", u"Type B"]
    type2 = []


class EspritParc(EspritParcParser):
    category = u"Miels et produits de la ruche"
    type1 = [u"Miel", u"Pollen", u"Gelée royale, propolis et pollen"]
    type2 = [u"Hautes Alpes Naturellement", u"Bienvenue à la ferme", u"Agriculture biologique"]


class ParserTests(TranslationResetMixin, TestCase):
    @mock.patch('requests.get')
    def test_create_content_apidae(self, mocked):
        def mocked_json():
            filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json')
            with io.open(filename, 'r', encoding='utf8') as f:
                return json.load(f)
        mocked.return_value.status_code = 200
        mocked.return_value.json = mocked_json
        FileType.objects.create(type=u"Photographie")
        category = TouristicContentCategoryFactory(label=u"Eau vive")
        TouristicContentTypeFactory(label=u"Type A", in_list=1)
        TouristicContentTypeFactory(label=u"Type B", in_list=1)
        call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=0)
        self.assertEqual(TouristicContent.objects.count(), 1)
        content = TouristicContent.objects.get()
        self.assertEqual(content.eid, u"479743")
        self.assertEqual(content.name, u"<NAME>")
        self.assertEqual(content.description[:27], u"Au pied du château médiéval")
        self.assertEqual(content.description_teaser[:24], u"Des descentes familiales")
        self.assertEqual(content.contact[:24], u"Château Queyras<br>05350")
        self.assertEqual(content.email, u"<EMAIL>")
        self.assertEqual(content.website, u"http://www.queyraft.com")
        self.assertEqual(round(content.geom.x), 1000157)
        self.assertEqual(round(content.geom.y), 6413576)
        self.assertEqual(content.practical_info[:39], "<b>Ouverture:</b><br>Du 01/05 au 31/10.")
        self.assertTrue(u"<br><b>Capacité totale:</b><br>10<br>" in content.practical_info)
        self.assertTrue(u"><br><b>Services:</b><br>Test, Test2, Test3, Test4<br>" in content.practical_info)
        self.assertTrue(content.published)
        self.assertEqual(content.category, category)
        self.assertQuerysetEqual(
            content.type1.all(),
            ['<TouristicContentType: Type A>', '<TouristicContentType: Type B>']
        )
        self.assertQuerysetEqual(content.type2.all(), [])
        self.assertEqual(Attachment.objects.count(), 3)
        self.assertEqual(Attachment.objects.first().content_object, content)

    @mock.patch('requests.get')
    def test_create_event_apidae(self, mocked):
        def mocked_json():
            filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeEvent.json')
            with io.open(filename, 'r', encoding='utf8') as f:
                return json.load(f)
        mocked.return_value.status_code = 200
        mocked.return_value.json = mocked_json
        FileType.objects.create(type=u"Photographie")
        call_command('import', 'geotrek.tourism.parsers.TouristicEventApidaeParser', verbosity=0)
        self.assertEqual(TouristicEvent.objects.count(), 1)
        event = TouristicEvent.objects.get()
        self.assertEqual(event.eid, u"323154")
        self.assertEqual(event.name, u"Cols Réservés 2019 : <NAME> (Laragne)")
        self.assertEqual(event.description[:31], u"Le département des Hautes-Alpes")
        self.assertEqual(event.description_teaser[:18], u"Une des ascensions")
        self.assertEqual(event.contact[:21], u"Châteauneuf de Chabre")<commit_msg>Add test count touristic event is equal to 0<commit_after># -*- encoding: utf-8 -*-

import io
import json
import mock
import os

from django.test import TestCase
from django.core.management import call_command

from geotrek.common.models import Attachment, FileType
from geotrek.common.tests import TranslationResetMixin
from geotrek.tourism.factories import TouristicContentCategoryFactory, TouristicContentTypeFactory
from geotrek.tourism.models import TouristicContent, TouristicEvent
from geotrek.tourism.parsers import TouristicContentApidaeParser, EspritParcParser


class EauViveParser(TouristicContentApidaeParser):
    category = u"Eau vive"
    type1 = [u"Type A", u"Type B"]
    type2 = []


class EspritParc(EspritParcParser):
    category = u"Miels et produits de la ruche"
    type1 = [u"Miel", u"Pollen", u"Gelée royale, propolis et pollen"]
    type2 = [u"Hautes <NAME>", u"Bienvenue à la ferme", u"Agriculture biologique"]


class ParserTests(TranslationResetMixin, TestCase):
    @mock.patch('requests.get')
    def test_create_content_apidae(self, mocked):
        def mocked_json():
            filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeContent.json')
            with io.open(filename, 'r', encoding='utf8') as f:
                return json.load(f)
        mocked.return_value.status_code = 200
        mocked.return_value.json = mocked_json
        FileType.objects.create(type=u"Photographie")
        category = TouristicContentCategoryFactory(label=u"Eau vive")
        TouristicContentTypeFactory(label=u"Type A", in_list=1)
        TouristicContentTypeFactory(label=u"Type B", in_list=1)
        call_command('import', 'geotrek.tourism.tests.test_parsers.EauViveParser', verbosity=0)
        self.assertEqual(TouristicContent.objects.count(), 1)
        content = TouristicContent.objects.get()
        self.assertEqual(content.eid, u"479743")
        self.assertEqual(content.name, u"Quey' Raft")
        self.assertEqual(content.description[:27], u"Au pied du château médiéval")
        self.assertEqual(content.description_teaser[:24], u"Des descentes familiales")
        self.assertEqual(content.contact[:24], u"Château Queyras<br>05350")
        self.assertEqual(content.email, u"<EMAIL>")
        self.assertEqual(content.website, u"http://www.queyraft.com")
        self.assertEqual(round(content.geom.x), 1000157)
        self.assertEqual(round(content.geom.y), 6413576)
        self.assertEqual(content.practical_info[:39], "<b>Ouverture:</b><br>Du 01/05 au 31/10.")
        self.assertTrue(u"<br><b>Capacité totale:</b><br>10<br>" in content.practical_info)
        self.assertTrue(u"><br><b>Services:</b><br>Test, Test2, Test3, Test4<br>" in content.practical_info)
        self.assertTrue(content.published)
        self.assertEqual(content.category, category)
        self.assertQuerysetEqual(
            content.type1.all(),
            ['<TouristicContentType: Type A>', '<TouristicContentType: Type B>']
        )
        self.assertQuerysetEqual(content.type2.all(), [])
        self.assertEqual(Attachment.objects.count(), 3)
        self.assertEqual(Attachment.objects.first().content_object, content)

    @mock.patch('requests.get')
    def test_create_event_apidae(self, mocked):
        def mocked_json():
            filename = os.path.join(os.path.dirname(__file__), 'data', 'apidaeEvent.json')
            with io.open(filename, 'r', encoding='utf8') as f:
                return json.load(f)
        mocked.return_value.status_code = 200
        mocked.return_value.json = mocked_json
        FileType.objects.create(type=u"Photographie")
        self.assertEqual(TouristicEvent.objects.count(), 0)
        call_command('import', 'geotrek.tourism.parsers.TouristicEventApidaeParser', verbosity=0)
        self.assertEqual(TouristicEvent.objects.count(), 1)
        event = TouristicEvent.objects.get()
        self.assertEqual(event.eid, u"323154")
        self.assertEqual(event.name, u"Cols Réservés 2019 : <NAME> (Laragne)")
        self.assertEqual(event.description[:31], u"Le département des Hautes-Alpes")
        self.assertEqual(event.description_teaser[:18], u"Une des ascensions")
        self.assertEqual(event.contact[:21], u"<NAME>re")
{'fraction_non_alphanumeric': 0.12450851900393185, 'fraction_numerical': 0.02025497438341475, 'mean_word_length': 3.8185993111366248, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4136261', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2623, 'n_words': 467}