def test_trek_child_as_parent(self): a = TrekFactory.create(name='A') b = TrekFactory.create(name='B', parent=a) c = TrekFactory.create(name='C', parent=b) self.assertRaisesMessage(ValidationError, u"Cannot use a a child trek as parent trek.", c.full_clean)
def test_gpx(self): trek = TrekFactory.create() trek = TrekFactory.create() url = reverse('trekking:trek_gpx_detail', kwargs={'pk': trek.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.content.count('<rte>'), 1) self.assertTrue(response.content.count('<rtept') >= 2)
def setUp(self): profile = UserProfileFactory.create(user__username='******', user__password='******', language='en') self.user = profile.user self.user.groups.add(Group.objects.get(name=u"Référents communication")) self.client.login(username='******', password='******') self.content1 = TrekFactory.create() structure = StructureFactory.create() self.content2 = TrekFactory.create(structure=structure)
def setUp(self): testutil._MAX_LENGTH = 10000 creation = make_aware(datetime.datetime(2014, 1, 1), utc) self.trek = TrekFactory.create(published=True) self.trek.date_insert = creation self.trek.save() self.poi = POIFactory.create(published=True) self.poi.date_insert = creation self.poi.save() TrekFactory.create(published=False) POIFactory.create(published=False)
def get_good_data(self): path = PathFactory.create() return { 'name_fr': 'Huhu', 'name_en': 'Hehe', 'departure_fr': '', 'departure_en': '', 'arrival_fr': '', 'arrival_en': '', 'published': '', 'difficulty': '', 'route': '', 'description_teaser_fr': '', 'description_teaser_en': '', 'description_fr': '', 'description_en': '', 'ambiance_fr': '', 'ambiance_en': '', 'access_fr': '', 'access_en': '', 'disabled_infrastructure_fr': '', 'disabled_infrastructure_en': '', 'duration': '0', 'is_park_centered': '', 'advised_parking': 'Very close', 'parking_location': 'POINT (1.0 1.0)', 'public_transport': 'huhu', 'advice_fr': '', 'advice_en': '', 'themes': ThemeFactory.create().pk, 'networks': TrekNetworkFactory.create().pk, 'practice': '', 'accessibilities': AccessibilityFactory.create().pk, 'web_links': WebLinkFactory.create().pk, 'information_desks': tourism_factories.InformationDeskFactory.create().pk, 'topology': '{"paths": [%s]}' % path.pk, 'trek_relationship_a-TOTAL_FORMS': '2', 'trek_relationship_a-INITIAL_FORMS': '0', 'trek_relationship_a-MAX_NUM_FORMS': '', 'trek_relationship_a-0-id': '', 'trek_relationship_a-0-trek_b': TrekFactory.create().pk, 'trek_relationship_a-0-has_common_edge': 'on', 'trek_relationship_a-0-has_common_departure': 'on', 'trek_relationship_a-0-is_circuit_step': '', 'trek_relationship_a-1-id': '', 'trek_relationship_a-1-trek_b': TrekFactory.create().pk, 'trek_relationship_a-1-has_common_edge': '', 'trek_relationship_a-1-has_common_departure': '', 'trek_relationship_a-1-is_circuit_step': 'on', }
def test_treks_are_sorted_by_name(self): TrekFactory.create(name='Cb') TrekFactory.create(name='Ca') TrekFactory.create(name='A') TrekFactory.create(name='B') self.assertEqual([u'A', u'B', u'Ca', u'Cb'], list(Trek.objects.all().values_list('name', flat=True)))
def test_treks_are_sorted_by_name(self): TrekFactory.create(name='Cb') TrekFactory.create(name='Ca') TrekFactory.create(name='A') TrekFactory.create(name='B') self.assertQuerysetEqual(Trek.objects.all(), [u'<Trek: A>', u'<Trek: B>', u'<Trek: Ca>', u'<Trek: Cb>'], ordered=False)
def test_json_detail(self): trek = TrekFactory.create() self.information_desk = InformationDeskFactory.create() trek.information_desks.add(self.information_desk) url = reverse('trekking:trek_json_detail', kwargs={'pk': trek.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200) detailjson = json.loads(response.content) self.assertDictEqual(detailjson['route'], {"id": trek.route.id, "pictogram": None, "label": trek.route.route}) self.assertDictEqual(detailjson['difficulty'], {"id": trek.difficulty.id, "pictogram": os.path.join(settings.MEDIA_URL, trek.difficulty.pictogram.name), "label": trek.difficulty.difficulty}) self.assertDictEqual(detailjson['information_desk'], detailjson['information_desks'][0]) self.assertDictEqual(detailjson['information_desks'][0], {u'description': u'<p>description 0</p>', u'email': u'*****@*****.**', u'latitude': -5.983593666147552, u'longitude': -1.3630761286186646, u'name': u'information desk name 0', u'phone': u'01 02 03 0', u'photo_url': self.information_desk.photo_url, u'postal_code': 28300, u'street': u'0 baker street', u'municipality': u"Bailleau L'évêque-0", u'website': u'http://makina-corpus.com/0'}) self.assertEqual(detailjson['information_desk_layer'], '/api/trek/%s/information_desks.geojson' % trek.pk) self.assertEqual(detailjson['filelist_url'], '/paperclip/get/trekking/trek/%s/' % trek.pk)
def setUp(self): call_command('update_permissions') self.trek = TrekFactory.create(published=False) self.user = User.objects.create_user('omer', password='******') self.user.user_permissions.add(Permission.objects.get(codename='add_trek')) self.user.user_permissions.add(Permission.objects.get(codename='change_trek')) self.client.login(username='******', password='******')
def test_poi_geojson_translation(self): # Create a Trek with a POI trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) poi = POIFactory.create(no_path=True) poi.name_fr = "Chapelle" poi.name_en = "Chapel" poi.name_it = "Capela" poi.published_fr = True poi.published_en = True poi.published_it = True poi.save() trek.add_path(p1, start=0.5) poi.add_path(p1, start=0.6, end=0.6) # Check that it applies to GeoJSON also : self.assertEqual(len(trek.pois), 1) poi = trek.pois[0] for lang, expected in [('fr-FR', poi.name_fr), ('it-IT', poi.name_it)]: url = reverse('trekking:trek_poi_geojson', kwargs={'pk': trek.pk}) self.login() response = self.client.get(url, HTTP_ACCEPT_LANGUAGE=lang) self.assertEqual(response.status_code, 200) obj = json.loads(response.content) jsonpoi = obj.get('features', [])[0] self.assertEqual(jsonpoi.get('properties', {}).get('name'), expected) self.client.logout() # Django 1.6 keeps language in session
def test_delete_show_topologies(self): self.login() path = PathFactory(name="PATH_AB", geom=LineString((0, 0), (4, 0))) poi = POIFactory.create(name='POI', no_path=True) poi.add_path(path, start=0.5, end=0.5) trail = TrailFactory.create(name='Trail', no_path=True) trail.add_path(path, start=0.1, end=0.2) trek = TrekFactory.create(name='Trek', no_path=True) trek.add_path(path, start=0.2, end=0.3) service = ServiceFactory.create(no_path=True, type__name='ServiceType') service.add_path(path, start=0.2, end=0.3) signage = SignageFactory.create(name='Signage', no_path=True) signage.add_path(path, start=0.2, end=0.2) infrastructure = InfrastructureFactory.create(name='Infrastructure', no_path=True) infrastructure.add_path(path, start=0.2, end=0.2) intervention1 = InterventionFactory.create(topology=signage, name='Intervention1') t = TopologyFactory.create(no_path=True) t.add_path(path, start=0.2, end=0.5) intervention2 = InterventionFactory.create(topology=t, name='Intervention2') response = self.client.get(path.get_delete_url()) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Different topologies are linked with this path') self.assertContains(response, '<a href="/poi/%d/">POI</a>' % poi.pk) self.assertContains(response, '<a href="/trail/%d/">Trail</a>' % trail.pk) self.assertContains(response, '<a href="/trek/%d/">Trek</a>' % trek.pk) self.assertContains(response, '<a href="/service/%d/">ServiceType</a>' % service.pk) self.assertContains(response, '<a href="/signage/%d/">Signage</a>' % signage.pk) self.assertContains(response, '<a href="/infrastructure/%d/">Infrastructure</a>' % infrastructure.pk) self.assertContains(response, '<a href="/intervention/%d/">Intervention1</a>' % intervention1.pk) self.assertContains(response, '<a href="/intervention/%d/">Intervention2</a>' % intervention2.pk)
def test_poi_geojson_translation(self): # Create a Trek with a POI trek = TrekFactory.create(no_path=True, published_fr=True, published_it=True) p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) poi = POIFactory.create(no_path=True) poi.name_fr = "Chapelle" poi.name_en = "Chapel" poi.name_it = "Capela" poi.published_fr = True poi.published_en = True poi.published_it = True poi.save() trek.add_path(p1, start=0.5) poi.add_path(p1, start=0.6, end=0.6) # Check that it applies to GeoJSON also : self.assertEqual(len(trek.pois), 1) poi = trek.pois[0] for lang, expected in [('fr', poi.name_fr), ('it', poi.name_it)]: url = '/api/{lang}/treks/{pk}/pois.geojson'.format(lang=lang, pk=trek.pk) self.login() response = self.client.get(url) self.assertEqual(response.status_code, 200) obj = json.loads(response.content) jsonpoi = obj.get('features', [])[0] self.assertEqual(jsonpoi.get('properties', {}).get('name'), expected) self.client.logout() # Django 1.6 keeps language in session
def test_helpers(self): trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) p2 = PathFactory.create(geom=LineString((4, 4), (8, 8))) poi = POIFactory.create(no_path=True) PathAggregationFactory.create(topo_object=trek, path=p1, start_position=0.5) PathAggregationFactory.create(topo_object=trek, path=p2) PathAggregationFactory.create(topo_object=poi, path=p1, start_position=0.6, end_position=0.6) # /!\ District are automatically linked to paths at DB level d1 = DistrictFactory.create(geom=MultiPolygon( Polygon(((-2, -2), (3, -2), (3, 3), (-2, 3), (-2, -2))))) # Ensure related objects are accessible self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(poi.treks, [trek]) self.assertItemsEqual(trek.districts, [d1]) # Ensure there is no duplicates PathAggregationFactory.create(topo_object=trek, path=p1, end_position=0.5) self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(poi.treks, [trek]) d2 = DistrictFactory.create(geom=MultiPolygon( Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3))))) self.assertItemsEqual(trek.districts, [d1, d2])
def test_delete_cascade(self): p1 = PathFactory.create() p2 = PathFactory.create() t = TrekFactory.create(no_path=True) t.add_path(p1) t.add_path(p2) # Everything should be all right before delete self.assertTrue(t.published) self.assertFalse(t.deleted) self.assertEqual(t.aggregations.count(), 2) # When a path is deleted p1.delete() t = Trek.objects.get(pk=t.pk) self.assertFalse(t.published) self.assertFalse(t.deleted) self.assertEqual(t.aggregations.count(), 1) # Reset published status t.published = True t.save() # When all paths are deleted p2.delete() t = Trek.objects.get(pk=t.pk) self.assertFalse(t.published) self.assertTrue(t.deleted) self.assertEqual(t.aggregations.count(), 0)
def setUp(self): self.trek = TrekFactory.create() self.information_desk1 = tourism_factories.InformationDeskFactory.create() self.information_desk2 = tourism_factories.InformationDeskFactory.create(photo=None) self.information_desk3 = tourism_factories.InformationDeskFactory.create() self.trek.information_desks.add(self.information_desk1) self.trek.information_desks.add(self.information_desk2) self.url = reverse('trekking:trek_information_desk_geojson', kwargs={'pk': self.trek.pk})
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_deleted_pois(self): p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) trek = TrekFactory.create(no_path=True) trek.add_path(p1) poi = POIFactory.create(no_path=True) poi.add_path(p1, start=0.6, end=0.6) self.assertItemsEqual(trek.pois, [poi]) poi.delete() self.assertItemsEqual(trek.pois, [])
def test_geojson_translation(self): trek = TrekFactory.create(name='Voie lactee') trek.name_it = 'Via Lattea' trek.save() url = reverse('trekking:trek_layer') # Test with another language response = self.client.get(url, HTTP_ACCEPT_LANGUAGE='it-IT') obj = json.loads(response.content) self.assertEqual(obj['features'][0]['properties']['name'], trek.name_it)
def test_trek_itself_as_parent(self): """ Test if a trek it is its own parent """ trek1 = TrekFactory.create(name='trek1') OrderedTrekChild.objects.create(parent=trek1, child=trek1) self.assertRaisesMessage(ValidationError, u"Cannot use itself as child trek.", trek1.full_clean)
def setUp(self): self.trek = TrekFactory.build() self.trek.name_fr = 'Voie lactee' self.trek.name_en = 'Milky way' self.trek.name_it = 'Via Lattea' self.trek.published_fr = True self.trek.published_it = False self.trek.save()
def setUp(self): self.login() polygon = 'SRID=%s;MULTIPOLYGON(((0 0, 0 3, 3 3, 3 0, 0 0)))' % settings.SRID self.city = CityFactory(geom=polygon) self.district = DistrictFactory(geom=polygon) self.trek = TrekFactory.create( points_reference=MultiPoint([Point(0, 0), Point(1, 1)], srid=settings.SRID), parking_location=Point(0, 0, srid=settings.SRID) ) self.attachment = AttachmentFactory.create(obj=self.trek, attachment_file=get_dummy_uploaded_image()) self.information_desk = tourism_factories.InformationDeskFactory.create() self.trek.information_desks.add(self.information_desk) self.usage = UsageFactory.create() self.trek.usages.add(self.usage) self.theme = ThemeFactory.create() self.trek.themes.add(self.theme) self.network = TrekNetworkFactory.create() self.trek.networks.add(self.network) self.weblink = WebLinkFactory.create() self.trek.web_links.add(self.weblink) self.trek_b = TrekFactory.create() TrekRelationshipFactory.create(has_common_departure=True, has_common_edge=False, is_circuit_step=True, trek_a=self.trek, trek_b=self.trek_b) self.touristic_content = tourism_factories.TouristicContentFactory(geom='SRID=%s;POINT(1 1)' % settings.SRID) self.touristic_event = tourism_factories.TouristicEventFactory(geom='SRID=%s;POINT(2 2)' % settings.SRID) self.pk = self.trek.pk url = '/api/treks/%s/' % self.pk self.response = self.client.get(url) self.result = json.loads(self.response.content)
def test_get_elevation_chart_none(self): trek = TrekFactory.create(no_path=True, published=True) response = self.client.get('/media/profiles/trek-%s.png' % trek.pk) self.assertEqual(response.status_code, 200) # In PDF trek.get_elevation_chart_path() basefolder = os.path.join(settings.MEDIA_ROOT, 'profiles') self.assertTrue(os.listdir(basefolder)) directory = os.listdir(basefolder) self.assertIn('%s-%s-%s.png' % (Trek._meta.model_name, str(trek.pk), get_language()), directory)
def test_city_departure_nds(self): trek = TrekFactory.create(geom=LineString((0, 0), (5, 5))) self.assertEqual(trek.city_departure, '') city1 = CityFactory.create(geom=MultiPolygon(Polygon(((-1, -1), (3, -1), (3, 3), (-1, 3), (-1, -1))))) city2 = CityFactory.create(geom=MultiPolygon(Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3))))) self.assertEqual([city for city in trek.cities], [city1, city2]) self.assertEqual(trek.city_departure, str(city1))
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}])
def test_deleted_services(self): p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) trek = TrekFactory.create(no_path=True) trek.add_path(p1) service = ServiceFactory.create(no_path=True) service.type.practices.add(trek.practice) service.add_path(p1, start=0.6, end=0.6) self.assertItemsEqual(trek.services, [service]) service.delete() self.assertItemsEqual(trek.services, [])
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 setUp(self): self.source_a = RecordSourceFactory() self.source_b = RecordSourceFactory() self.portal_a = TargetPortalFactory() self.portal_b = TargetPortalFactory() information_desks = InformationDeskFactory.create() self.trek_1 = TrekWithPublishedPOIsFactory.create(sources=(self.source_a, ), portals=(self.portal_b,), published=True) self.trek_1.information_desks.add(information_desks) self.attachment_1 = AttachmentFactory.create(content_object=self.trek_1, attachment_file=get_dummy_uploaded_image()) self.trek_2 = TrekFactory.create(sources=(self.source_b,), published=True) self.trek_3 = TrekFactory.create(portals=(self.portal_b, self.portal_a), published=True) self.trek_4 = TrekFactory.create(portals=(self.portal_a,), published=True) self.poi_1 = trek_models.POI.objects.first() self.attachment_poi_image_1 = AttachmentFactory.create(content_object=self.poi_1, attachment_file=get_dummy_uploaded_image()) self.attachment_poi_image_2 = AttachmentFactory.create(content_object=self.poi_1, attachment_file=get_dummy_uploaded_image()) self.attachment_poi_file = AttachmentFactory.create(content_object=self.poi_1, attachment_file=get_dummy_uploaded_file()) infrastructure = InfrastructureFactory.create(no_path=True, name="INFRA_1") infrastructure.add_path(self.trek_1.paths.first(), start=0, end=0) signage = SignageFactory.create(no_path=True, name="SIGNA_1") signage.add_path(self.trek_1.paths.first(), start=0, end=0) SensitiveAreaFactory.create(published=True) self.touristic_content = TouristicContentFactory( geom='SRID=%s;POINT(700001 6600001)' % settings.SRID, published=True) self.touristic_event = TouristicEventFactory( geom='SRID=%s;POINT(700001 6600001)' % settings.SRID, published=True) self.attachment_touristic_content = AttachmentFactory.create(content_object=self.touristic_content, attachment_file=get_dummy_uploaded_image()) self.attachment_touristic_event = AttachmentFactory.create(content_object=self.touristic_event, attachment_file=get_dummy_uploaded_image())
def test_picture(self): trek = TrekFactory.create() AttachmentFactory.create(obj=trek) self.assertEqual(len(trek.attachments), 1) self.assertEqual(trek.thumbnail, None) self.assertEqual(trek.pictures, []) AttachmentFactory.create(obj=trek, attachment_file=get_dummy_uploaded_image()) self.assertEqual(len(trek.attachments), 2) self.assertEqual(len(trek.pictures), 1) self.assertNotEqual(trek.thumbnail, None)
def test_next_previous(self): trekA = TrekFactory(name="A") trekB = TrekFactory(name="B") trekC = TrekFactory(name="C") trekD = TrekFactory(name="D") OrderedTrekChild(parent=trekC, child=trekA, order=42).save() OrderedTrekChild(parent=trekC, child=trekB, order=15).save() OrderedTrekChild(parent=trekD, child=trekA, order=1).save() self.assertEqual(list(trekA.children_id), []) self.assertEqual(list(trekB.children_id), []) self.assertEqual(list(trekC.children_id), [trekB.id, trekA.id]) self.assertEqual(list(trekD.children_id), [trekA.id]) self.assertEqual(trekA.next_id, {trekC.id: None, trekD.id: None}) self.assertEqual(trekB.next_id, {trekC.id: trekA.id}) self.assertEqual(trekC.next_id, {}) self.assertEqual(trekD.next_id, {}) self.assertEqual(trekA.previous_id, {trekC.id: trekB.id, trekD.id: None}) self.assertEqual(trekB.previous_id, {trekC.id: None}) self.assertEqual(trekC.previous_id, {}) self.assertEqual(trekD.previous_id, {})
def test_city_departure(self): trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0), (5, 5))) trek.add_path(p1) self.assertEqual(trek.city_departure, '') city1 = CityFactory.create(geom=MultiPolygon(Polygon(((-1, -1), (3, -1), (3, 3), (-1, 3), (-1, -1))))) city2 = CityFactory.create(geom=MultiPolygon(Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3))))) self.assertEqual(trek.cities, [city1, city2]) self.assertEqual(trek.city_departure, unicode(city1))
def test_get_elevation_chart_none(self): trek = TrekFactory.create(no_path=True, published=True) response = self.client.get('/media/profiles/trek-%s.png' % trek.pk) self.assertEqual(response.status_code, 200) # In PDF trek.get_elevation_chart_path() basefolder = os.path.join(settings.MEDIA_ROOT, 'profiles') self.assertTrue(os.listdir(basefolder)) directory = os.listdir(basefolder) self.assertIn( '%s-%s-%s.png' % (Trek._meta.model_name, str(trek.pk), get_language()), directory)
def test_overriden_document(self): trek = TrekFactory.create() # Will have to mock screenshot, though. with open(trek.get_map_image_path(), 'w') as f: f.write('***' * 1000) response = self.client.get(trek.get_document_public_url()) self.assertEqual(response.status_code, 200) self.assertTrue(len(response.content) > 1000) AttachmentFactory.create(obj=trek, title="docprint", attachment_file=get_dummy_uploaded_document(size=100)) response = self.client.get(trek.get_document_public_url()) self.assertEqual(response.status_code, 200) self.assertTrue(len(response.content) < 1000)
def setUp(self): self.user = SuperUserFactory.create(password='******') self.theme = ThemeFactory.create(label="Theme 1") self.theme_2 = ThemeFactory.create(label="Theme 2") self.theme_other = ThemeFactory.create(label="Autre theme") self.difficulty_1 = DifficultyLevelFactory.create(difficulty="Dif 1") self.difficulty_2 = DifficultyLevelFactory.create(difficulty="Dif 2") self.difficulty_3 = DifficultyLevelFactory.create(difficulty="Dif 3") self.trek = TrekFactory.create( geom='SRID=%s;LINESTRING(0 0, 1 0, 2 0)' % settings.SRID, difficulty=self.difficulty_2) self.trek.themes.add(self.theme, self.theme_2)
def test_pois_should_be_ordered_by_progression(self): p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) p2 = PathFactory.create(geom=LineString((4, 4), (8, 8))) self.trek = TrekFactory.create(no_path=True) self.trek.add_path(p1) self.trek.add_path(p2, order=1) self.trek_reverse = TrekFactory.create(no_path=True) self.trek_reverse.add_path(p2, start=0.8, end=0, order=0) self.trek_reverse.add_path(p1, start=1, end=0.2, order=1) self.poi1 = POIFactory.create(no_path=True) self.poi1.add_path(p1, start=0.8, end=0.8) self.poi2 = POIFactory.create(no_path=True) self.poi2.add_path(p1, start=0.3, end=0.3) self.poi3 = POIFactory.create(no_path=True) self.poi3.add_path(p2, start=0.5, end=0.5) pois = self.trek.pois self.assertEqual([self.poi2, self.poi1, self.poi3], list(pois)) pois = self.trek_reverse.pois self.assertEqual([self.poi3, self.poi1, self.poi2], list(pois))
def test_tiles_with_treks(self, mock_tileslist, mock_tiles, mock_prepare): output = StringIO() trek = TrekFactory.create(published=True) management.call_command('sync_rando', 'tmp', url='http://localhost:8000', verbosity=2, languages='en', stdout=output) zfile = zipfile.ZipFile(os.path.join('tmp', 'zip', 'tiles', 'global.zip')) for finfo in zfile.infolist(): ifile = zfile.open(finfo) self.assertEqual(ifile.read(), b'I am a png') self.assertIn("zip/tiles/global.zip", output.getvalue()) zfile_trek = zipfile.ZipFile(os.path.join('tmp', 'zip', 'tiles', '{pk}.zip'.format(pk=trek.pk))) for finfo in zfile_trek.infolist(): ifile_trek = zfile_trek.open(finfo) self.assertEqual(ifile_trek.read(), b'I am a png') self.assertIn("zip/tiles/{pk}.zip".format(pk=trek.pk), output.getvalue())
def test_sync_settings_with_picto_svg(self): output = StringIO() practice = PracticeFactory.create( pictogram=get_dummy_uploaded_image_svg()) TrekFactory.create(practice=practice) information_desk_type = InformationDeskTypeFactory.create( pictogram=get_dummy_uploaded_image()) InformationDeskFactory.create(type=information_desk_type) pictogram_png = practice.pictogram.url.replace('.svg', '.png') pictogram_desk_png = information_desk_type.pictogram.url management.call_command('sync_mobile', 'var/tmp', url='http://localhost:8000', skip_tiles=True, verbosity=2, stdout=output) for lang in settings.MODELTRANSLATION_LANGUAGES: with open(os.path.join('var/tmp', lang, 'settings.json'), 'r') as f: settings_json = json.load(f) self.assertEqual(len(settings_json), 2) self.assertEqual(len(settings_json['data']), 16) self.assertEqual( settings_json['data'][4]['values'][0]['pictogram'], pictogram_png) self.assertEqual( settings_json['data'][9]['values'][0]['pictogram'], pictogram_desk_png) image_practice = Image.open( os.path.join('var/tmp/nolang', pictogram_png[1:])) image_desk = Image.open( os.path.join('var/tmp/nolang', pictogram_desk_png[1:])) self.assertEqual(image_practice.size, (32, 32)) self.assertEqual(image_desk.size, (32, 32)) self.assertIn('en/settings.json', output.getvalue())
def setUp(self): self.user = SuperUserFactory.create(password='******') self.content = POIFactory.create(geom='SRID=%s;POINT(1 1)' % settings.SRID) self.picture = AttachmentFactory( content_object=self.content, attachment_file=get_dummy_uploaded_image(), ) self.trek = TrekFactory.create( geom='SRID=%s;LINESTRING(0 0, 1 0, 2 0)' % settings.SRID) self.picture_2 = AttachmentFactory( content_object=self.trek, attachment_file=get_dummy_uploaded_image(), )
def test_trek_sensitive_area(self): """ The area intersects 6 times the trek : we should get only one time this area. issue #2010 """ SensitiveAreaFactory.create() SensitiveAreaFactory.create( geom="Polygon ((700000 6600000, 699994.87812128441873938 6600014.35493460204452276, 700021.84601664706133306" " 6600008.61177170090377331, 700013.10642092768102884 6600028.83769322279840708, 700044.81866825232282281" " 6600017.85077288933098316, 700030.83531510119792074 6600042.32164090406149626, 700061.79845422133803368" " 6600043.07074910867959261, 700061.04934601683635265 6600069.78894173633307219, 700075.78180737234652042" " 6600056.55469678994268179, 700072.53567181946709752 6600088.01724137924611568, 700092.26218787173274904" " 6600081.27526753861457109, 700090.01486325822770596 6600097.75564803835004568, 700100 6600100, " "700100 6600000, 700000 6600000))") trek = TrekFactory.create() self.assertEqual(trek.published_sensitive_areas.count(), 2)
def test_pois_geojson(self): trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0, 0), (4, 4, 2))) poi = POIFactory(no_path=True) trek.add_path(p1, start=0.5, end=1.0) poi.add_path(p1, start=0.6, end=0.6) AttachmentFactory.create(obj=poi, attachment_file=get_dummy_uploaded_image()) self.assertNotEqual(poi.thumbnail, None) self.assertEqual(len(trek.pois), 1) url = reverse('trekking:trek_poi_geojson', kwargs={'pk': trek.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200) poislayer = json.loads(response.content) poifeature = poislayer['features'][0] self.assertTrue('serializable_thumbnail' in poifeature['properties'])
def test_save_with_multiple_fields_exclusion(self): new_trek = TrekFactory.create() new_trek.description_en = 'Description Test update' new_trek.ambiance = 'Very special ambiance, for test purposes.' new_trek.save(update_fields=['description_en']) db_trek = Trek.objects.get(pk=new_trek.pk) self.assertTrue(db_trek.pk) self.assertEqual(db_trek.description_en, 'Description Test update') self.assertNotEqual(db_trek.ambiance, 'Very special ambiance, for test purposes.') new_trek.save(update_fields=['ambiance_en']) db_trek = Trek.objects.get(pk=new_trek.pk) self.assertEqual(db_trek.ambiance_en, 'Very special ambiance, for test purposes.')
def test_is_publishable(self): t = TrekFactory.create() t.geom = LineString((0, 0, 0), (1, 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_delete_child(self): trekA = TrekFactory(name=u"A") trekB = TrekFactory(name=u"B") trekC = TrekFactory(name=u"C") OrderedTrekChild(parent=trekA, child=trekB, order=1).save() OrderedTrekChild(parent=trekA, child=trekC, order=2).save() self.assertTrue(OrderedTrekChild.objects.filter(child=trekB).exists()) self.assertQuerysetEqual(trekA.children, ['<Trek: B>', '<Trek: C>']) self.assertQuerysetEqual(trekB.parents, ['<Trek: A>']) self.assertQuerysetEqual(trekC.parents, ['<Trek: A>']) self.assertEqual(trekA.children_id, [trekB.id, trekC.id]) self.assertEqual(trekB.parents_id, [trekA.id]) self.assertEqual(trekC.parents_id, [trekA.id]) trekB.delete() self.assertEqual(trekC.previous_id_for(trekA), None) self.assertEqual(trekC.next_id_for(trekA), None) self.assertEqual(trekC.next_id, {trekA.id: None}) self.assertEqual(trekC.previous_id, {trekA.id: None}) self.assertFalse(OrderedTrekChild.objects.filter(child=trekB).exists()) self.assertQuerysetEqual(trekA.children, ['<Trek: C>']) self.assertQuerysetEqual(trekC.parents, ['<Trek: A>']) self.assertEqual(trekA.children_id, [trekC.id]) self.assertEqual(trekC.parents_id, [trekA.id])
def setUp(self): self.user = SuperUserFactory.create(password='******') self.content = POIFactory.create(geom='SRID=%s;POINT(1 1)' % settings.SRID) self.picture = AttachmentFactory( content_object=self.content, title='img1', attachment_file=get_dummy_uploaded_image()) self.trek = TrekFactory.create( geom='SRID=%s;LINESTRING(0 0, 1 0, 2 0)' % settings.SRID) self.picture_2 = AttachmentFactory( content_object=self.trek, title='img2', attachment_file=get_dummy_uploaded_image()) if os.path.exists(settings.MEDIA_ROOT): shutil.rmtree(settings.MEDIA_ROOT) os.makedirs(settings.MEDIA_ROOT)
def test_json_detail(self): trek = TrekFactory.create() self.information_desk = InformationDeskFactory.create() trek.information_desks.add(self.information_desk) url = reverse('trekking:trek_json_detail', kwargs={'pk': trek.pk}) response = self.client.get(url) self.assertEqual(response.status_code, 200) detailjson = json.loads(response.content) self.assertDictEqual(detailjson['route'], { "id": trek.route.id, "pictogram": None, "label": trek.route.route }) self.assertDictEqual( detailjson['difficulty'], { "id": trek.difficulty.id, "pictogram": os.path.join(settings.MEDIA_URL, trek.difficulty.pictogram.name), "label": trek.difficulty.difficulty }) self.assertDictEqual(detailjson['information_desk'], detailjson['information_desks'][0]) self.assertDictEqual( detailjson['information_desks'][0], { u'description': u'<p>description 0</p>', u'email': u'*****@*****.**', u'latitude': -5.983593666147552, u'longitude': -1.3630761286186646, u'name': u'information desk name 0', u'phone': u'01 02 03 0', u'photo_url': self.information_desk.photo_url, u'postal_code': u'28300', u'street': u'0 baker street', u'municipality': u"Bailleau L'évêque-0", u'website': u'http://makina-corpus.com/0' }) self.assertEqual(detailjson['information_desk_layer'], '/api/trek/%s/information_desks.geojson' % trek.pk) self.assertEqual(detailjson['filelist_url'], '/paperclip/get/trekking/trek/%s/' % trek.pk)
def test_delete_show_topologies(self): self.login() path = PathFactory(name="PATH_AB", geom=LineString((0, 0), (4, 0))) poi = POIFactory.create(name='POI', no_path=True) poi.add_path(path, start=0.5, end=0.5) trail = TrailFactory.create(name='Trail', no_path=True) trail.add_path(path, start=0.1, end=0.2) trek = TrekFactory.create(name='Trek', no_path=True) trek.add_path(path, start=0.2, end=0.3) service = ServiceFactory.create(no_path=True, type__name='ServiceType') service.add_path(path, start=0.2, end=0.3) signage = SignageFactory.create(name='Signage', no_path=True) signage.add_path(path, start=0.2, end=0.2) infrastructure = InfrastructureFactory.create(name='Infrastructure', no_path=True) infrastructure.add_path(path, start=0.2, end=0.2) intervention1 = InterventionFactory.create(topology=signage, name='Intervention1') t = TopologyFactory.create(no_path=True) t.add_path(path, start=0.2, end=0.5) intervention2 = InterventionFactory.create(topology=t, name='Intervention2') response = self.client.get(path.get_delete_url()) self.assertEqual(response.status_code, 200) self.assertContains(response, 'Different topologies are linked with this path') self.assertContains(response, '<a href="/poi/%d/">POI</a>' % poi.pk) self.assertContains(response, '<a href="/trail/%d/">Trail</a>' % trail.pk) self.assertContains(response, '<a href="/trek/%d/">Trek</a>' % trek.pk) self.assertContains( response, '<a href="/service/%d/">ServiceType</a>' % service.pk) self.assertContains(response, '<a href="/signage/%d/">Signage</a>' % signage.pk) self.assertContains( response, '<a href="/infrastructure/%d/">Infrastructure</a>' % infrastructure.pk) self.assertContains( response, '<a href="/intervention/%d/">Intervention1</a>' % intervention1.pk) self.assertContains( response, '<a href="/intervention/%d/">Intervention2</a>' % intervention2.pk)
def test_helpers_nds(self): trek = TrekFactory.create(geom=LineString((2, 2), (8, 8))) poi = POIFactory.create(geom=Point(2.4, 2.4)) poi2 = POIFactory.create(geom=Point(2.4, 2.4)) service = ServiceFactory.create(geom=Point(2.8, 2.8)) service.type.practices.add(trek.practice) trek.pois_excluded.add(poi2.pk) # /!\ District are automatically linked to paths at DB level d1 = DistrictFactory.create(geom=MultiPolygon( Polygon(((-2, -2), (3, -2), (3, 3), (-2, 3), (-2, -2))))) # Ensure related objects are accessible self.assertCountEqual(trek.pois_excluded.all(), [poi2]) self.assertCountEqual(trek.all_pois, [poi, poi2]) self.assertCountEqual(trek.pois, [poi]) self.assertCountEqual(trek.services, [service]) self.assertCountEqual(poi.treks, [trek]) self.assertCountEqual(service.treks, [trek]) self.assertCountEqual(trek.districts, [d1])
def setUpClass(cls): super(SyncMobileTreksTest, cls).setUpClass() cls.portal_a = TargetPortalFactory() cls.portal_b = TargetPortalFactory() picto_desk = get_dummy_uploaded_image() information_desk_type = InformationDeskTypeFactory.create(pictogram=picto_desk) info_desk = InformationDeskFactory.create(type=information_desk_type) info_desk_no_picture = InformationDeskFactory.create(photo=None) cls.trek_1 = TrekWithPublishedPOIsFactory.create() cls.trek_1.information_desks = (info_desk, info_desk_no_picture) cls.trek_2 = TrekWithPublishedPOIsFactory.create(portals=(cls.portal_a,)) cls.trek_3 = TrekWithPublishedPOIsFactory.create(portals=(cls.portal_b,)) cls.trek_4 = TrekFactory.create() OrderedTrekChild.objects.create(parent=cls.trek_1, child=cls.trek_4, order=1) cls.desk = InformationDeskFactory.create() cls.trek_4.information_desks.add(cls.desk) cls.attachment_1 = AttachmentFactory.create(content_object=cls.trek_1, attachment_file=get_dummy_uploaded_image()) AttachmentFactory.create(content_object=cls.trek_1, attachment_file=get_dummy_uploaded_image()) cls.poi_1 = POI.objects.first() cls.attachment_poi_image_1 = AttachmentFactory.create(content_object=cls.poi_1, attachment_file=get_dummy_uploaded_image()) cls.attachment_poi_image_2 = AttachmentFactory.create(content_object=cls.poi_1, attachment_file=get_dummy_uploaded_image()) cls.attachment_poi_file = AttachmentFactory.create(content_object=cls.poi_1, attachment_file=get_dummy_uploaded_file()) cls.attachment_trek_image = AttachmentFactory.create(content_object=cls.trek_4, attachment_file=get_dummy_uploaded_image()) cls.touristic_content = TouristicContentFactory(geom='SRID=%s;POINT(700001 6600001)' % settings.SRID, published=True) cls.touristic_event = TouristicEventFactory(geom='SRID=%s;POINT(700001 6600001)' % settings.SRID, published=True) cls.attachment_content_1 = AttachmentFactory.create(content_object=cls.touristic_content, attachment_file=get_dummy_uploaded_image()) cls.attachment_event_1 = AttachmentFactory.create(content_object=cls.touristic_event, attachment_file=get_dummy_uploaded_image()) translation.deactivate()
def test_helpers(self): trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) p2 = PathFactory.create(geom=LineString((4, 4), (8, 8))) poi = POIFactory.create(no_path=True) service = ServiceFactory.create(no_path=True) service.type.practices.add(trek.practice) PathAggregationFactory.create(topo_object=trek, path=p1, start_position=0.5) PathAggregationFactory.create(topo_object=trek, path=p2) PathAggregationFactory.create(topo_object=poi, path=p1, start_position=0.6, end_position=0.6) PathAggregationFactory.create(topo_object=service, path=p1, start_position=0.7, end_position=0.7) # /!\ District are automatically linked to paths at DB level d1 = DistrictFactory.create(geom=MultiPolygon( Polygon(((-2, -2), (3, -2), (3, 3), (-2, 3), (-2, -2))))) # Ensure related objects are accessible self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(trek.services, [service]) self.assertItemsEqual(poi.treks, [trek]) self.assertItemsEqual(service.treks, [trek]) self.assertItemsEqual(trek.districts, [d1]) # Ensure there is no duplicates PathAggregationFactory.create(topo_object=trek, path=p1, end_position=0.5) self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(trek.services, [service]) self.assertItemsEqual(poi.treks, [trek]) self.assertItemsEqual(service.treks, [trek]) d2 = DistrictFactory.create( geom=MultiPolygon(Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3))))) self.assertItemsEqual(trek.districts, [d1, d2])
def test_overriden_document(self, get_attributes_html): trek = TrekFactory.create() get_attributes_html.return_value = '<p>mock</p>' with open(trek.get_map_image_path(), 'w') as f: f.write('***' * 1000) with open(trek.get_elevation_chart_path(), 'w') as f: f.write('***' * 1000) response = self.client.get(trek.get_document_public_url()) self.assertEqual(response.status_code, 200) self.assertTrue(len(response.content) > 1000) AttachmentFactory.create( obj=trek, title="docprint", attachment_file=get_dummy_uploaded_document(size=100)) response = self.client.get(trek.get_document_public_url()) self.assertEqual(response.status_code, 200) self.assertTrue(len(response.content) < 1000)
def test_helpers(self): trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0), (4, 4))) p2 = PathFactory.create(geom=LineString((4, 4), (8, 8))) poi = POIFactory.create(no_path=True) poi2 = POIFactory.create(no_path=True) service = ServiceFactory.create(no_path=True) service.type.practices.add(trek.practice) trek.add_path(path=p1, start=0.5, end=1) trek.add_path(path=p2, start=0, end=1) poi.add_path(path=p1, start=0.6, end=0.6) poi2.add_path(path=p1, start=0.6, end=0.6) service.add_path(path=p1, start=0.7, end=0.7) trek.pois_excluded.add(poi2.pk) # /!\ District are automatically linked to paths at DB level d1 = DistrictFactory.create(geom=MultiPolygon( Polygon(((-2, -2), (3, -2), (3, 3), (-2, 3), (-2, -2))))) # Ensure related objects are accessible self.assertItemsEqual(trek.pois_excluded.all(), [poi2]) self.assertItemsEqual(trek.all_pois, [poi, poi2]) self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(trek.services, [service]) self.assertItemsEqual(poi.treks, [trek]) self.assertItemsEqual(service.treks, [trek]) self.assertItemsEqual(trek.districts, [d1]) # Ensure there is no duplicates trek.add_path(path=p1, start=0.5, end=1) self.assertItemsEqual(trek.pois_excluded.all(), [poi2]) self.assertItemsEqual(trek.all_pois, [poi, poi2]) self.assertItemsEqual(trek.pois, [poi]) self.assertItemsEqual(trek.services, [service]) self.assertItemsEqual(poi.treks, [trek]) self.assertItemsEqual(service.treks, [trek]) d2 = DistrictFactory.create( geom=MultiPolygon(Polygon(((3, 3), (9, 3), (9, 9), (3, 9), (3, 3))))) self.assertItemsEqual(trek.districts, [d1, d2])
def test_poi_geojson_translation(self): # Create a Trek with a POI trek = TrekFactory.create(no_path=True) p1 = PathFactory.create(geom=LineString((0, 0, 0), (4, 4, 2))) poi = POIFactory.create(no_path=True) poi.name_fr = "Chapelle" poi.name_it = "Capela" poi.save() PathAggregationFactory.create(topo_object=trek, path=p1, start_position=0.5) PathAggregationFactory.create(topo_object=poi, path=p1, start_position=0.6, end_position=0.6) # Check that it applies to GeoJSON also : self.assertEqual(len(trek.pois), 1) poi = trek.pois[0] for lang, expected in [('fr-FR', poi.name_fr), ('it-IT', poi.name_it)]: url = reverse('trekking:trek_poi_geojson', kwargs={'pk': trek.pk}) response = self.client.get(url, HTTP_ACCEPT_LANGUAGE=lang) obj = json.loads(response.content) jsonpoi = obj.get('features', [])[0] self.assertEqual(jsonpoi.get('properties', {}).get('name'), expected)
def test_json_translation(self): trek = TrekFactory.build() trek.name_fr = 'Voie lactee' trek.name_en = 'Milky way' trek.name_it = 'Via Lattea' trek.save() url = reverse('trekking:trek_json_detail', kwargs={'pk': trek.pk}) # Test default case response = self.client.get(url) obj = json.loads(response.content) self.assertEqual(obj['name'], trek.name) # Test with another language response = self.client.get(url, HTTP_ACCEPT_LANGUAGE='it-IT') obj = json.loads(response.content) self.assertEqual(obj['name'], trek.name_it) # Test with yet another language response = self.client.get(url, HTTP_ACCEPT_LANGUAGE='fr-FR') obj = json.loads(response.content) self.assertEqual(obj['name'], trek.name_fr)
def test_only_external_public_pdf(self, trek): output = StringIO() trek = TrekFactory.create(published=True, ) filetype_topoguide = FileTypeFactory.create(type='Topoguide') AttachmentFactory.create(content_object=trek, attachment_file=get_dummy_uploaded_image(), filetype=filetype_topoguide) management.call_command('sync_rando', os.path.join('var', 'tmp'), url='http://localhost:8000', verbosity=2, skip_pdf=False, skip_tiles=True, stdout=output) self.assertFalse( os.path.exists( os.path.join('var', 'tmp', 'api', 'en', 'treks', str(self.trek.pk), '%s.pdf' % self.trek.slug))) self.assertTrue( os.path.exists( os.path.join('var', 'tmp', 'api', 'en', 'treks', str(trek.pk), '%s.pdf' % trek.slug)))
def test_tiles_with_treks(self, mock_tiles, mock_prepare, mock_tileslist): output = StringIO() portal_a = TargetPortalFactory() portal_b = TargetPortalFactory() trek = TrekWithPublishedPOIsFactory.create(published=True) trek_not_same_portal = TrekWithPublishedPOIsFactory.create( published=True, portals=(portal_a, )) p = PathFactory.create(geom=LineString((0, 0), (0, 10))) trek_multi = TrekFactory.create(published=True, paths=[(p, 0, 0.1), (p, 0.2, 0.3)]) management.call_command('sync_mobile', 'var/tmp', url='http://localhost:8000', verbosity=2, stdout=output, portal=portal_b.name) zfile_global = zipfile.ZipFile('var/tmp/nolang/global.zip') for finfo in zfile_global.infolist(): ifile_global = zfile_global.open(finfo) if ifile_global.name.startswith('tiles/'): self.assertEqual(ifile_global.readline(), b'I am a png') zfile_trek = zipfile.ZipFile('var/tmp/nolang/{}.zip'.format(trek.pk)) for finfo in zfile_trek.infolist(): ifile_trek = zfile_trek.open(finfo) if ifile_trek.name.startswith('tiles/'): self.assertEqual(ifile_trek.readline(), b'I am a png') self.assertIn("nolang/global.zip", output.getvalue()) self.assertIn("nolang/{pk}.zip".format(pk=trek.pk), output.getvalue()) self.assertFalse( os.path.exists( os.path.join('var/tmp', 'nolang', '{}.zip'.format(trek_not_same_portal.pk)))) self.assertTrue( os.path.exists( os.path.join('var/tmp', 'nolang', '{}.zip'.format(trek_multi.pk))))