From ee13222f5eb4bb4f9ddddce97f12973c68225f78 Mon Sep 17 00:00:00 2001 From: Matt Griswold Date: Tue, 8 Oct 2019 10:30:52 -0500 Subject: [PATCH] fix issue with unicode encoding in geosync command output, (#582) add --commit option to geosync command to be in line with other peeringdb commands, add tests for running geosync command --- .../management/commands/pdb_geosync.py | 19 ++++++++++---- tests/django_init.py | 1 + tests/test_geocode.py | 25 ++++++++++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/peeringdb_server/management/commands/pdb_geosync.py b/peeringdb_server/management/commands/pdb_geosync.py index 8bf826ed..a44e701a 100644 --- a/peeringdb_server/management/commands/pdb_geosync.py +++ b/peeringdb_server/management/commands/pdb_geosync.py @@ -20,11 +20,18 @@ class Command(BaseCommand): parser.add_argument( "--limit", type=int, default=0, help="limit how many rows are synced, useful for testing") + parser.add_argument( + '--commit', action='store_true', + help="commit changes, otherwise run in pretend mode") def log(self, msg): - print(msg) + if not self.commit: + self.stdout.write(u"[pretend] {}".format(msg)) + else: + self.stdout.write(msg) def handle(self, *args, **options): + self.commit = options.get("commit", False) reftag = options.get("reftag") limit = options.get("limit") if reftag.find(".") > -1: @@ -38,7 +45,7 @@ class Command(BaseCommand): def sync(self, reftag, _id, limit=0): model = models.REFTAG_MAP.get(reftag) if not model: - raise ValueError("Unknown reftag: %s" % reftag) + raise ValueError(u"Unknown reftag: {}".format(reftag)) if not hasattr(model, "geocode_status"): raise TypeError( "Can only geosync models containing GeocodeBaseMixin") @@ -53,6 +60,8 @@ class Command(BaseCommand): if entity.geocode_status: continue i += 1 - self.log("Syncing %s [%s %d/%d ID:%s]" % (entity, reftag, i, count, - entity.id)) - entity.geocode(self.gmaps) + self.log(u"Syncing {} [{} {}/{} ID:{}]".format(entity.name, reftag, i, + count, entity.id)) + + if self.commit: + entity.geocode(self.gmaps) diff --git a/tests/django_init.py b/tests/django_init.py index cc474af5..7952dc61 100644 --- a/tests/django_init.py +++ b/tests/django_init.py @@ -165,6 +165,7 @@ settings.configure( SITE_ID=1, IXF_POSTMORTEM_LIMIT=250, ABSTRACT_ONLY=True, + GOOGLE_GEOLOC_API_KEY="AIzatest", RATELIMITS={ "view_affiliate_to_org_POST": "100/m", "resend_confirmation_mail": "2/m", diff --git a/tests/test_geocode.py b/tests/test_geocode.py index 2f4a6640..c4bcc420 100644 --- a/tests/test_geocode.py +++ b/tests/test_geocode.py @@ -1,11 +1,15 @@ +# -*- coding: utf-8 -*- import pytest import json import uuid import re +import StringIO from django.test import Client, TestCase, RequestFactory from django.contrib.auth.models import Group, AnonymousUser from django.contrib.auth import get_user +from django.core.management import call_command + import django_namespace_perms as nsp import peeringdb_server.models as models @@ -25,7 +29,7 @@ class ViewTestCase(TestCase): cls.facilities = dict( (k, models.Facility.objects.create( - name="Geocode Fac %s" % k, status="ok", org=cls.organizations[ + name=u"Geocode Fac {}".format(k), status="ok", org=cls.organizations[ k], address1="Some street", address2=k, city="Chicago", country="US", state="IL", zipcode="1234", latitude=1.23, longitude=-1.23, geocode_status=True)) @@ -50,3 +54,22 @@ class ViewTestCase(TestCase): self.facilities["d"].website = 'http://www.test.com' self.facilities["d"].save() self.assertEqual(self.facilities["d"].geocode_status, True) + + def test_command(self): + self.assertEqual(self.facilities["a"].geocode_status, True) + + # change address to flag facility for geocoding + + self.facilities["a"].address1 = "Another street a" + + # test unicode output from command by adding special characters + # to the new address + + self.facilities["a"].name = "sdílených služeb" + self.facilities["a"].save() + + out = StringIO.StringIO() + call_command("pdb_geosync", "fac", limit=1, stdout=out) + out = out.getvalue() + + assert out.find("[fac 1/1 ID:1]") > -1