← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-unstable-json into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-unstable-json into launchpad:master.

Commit message:
Don't assume stable JSON dict serialisation (take 2)

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/397873

Like 559e1e9c31c005200977039897a93d450f3cfc6b, but in some more tests.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-unstable-json into launchpad:master.
diff --git a/lib/lp/answers/browser/tests/test_questiontarget.py b/lib/lp/answers/browser/tests/test_questiontarget.py
index cd510b6..241a1a8 100644
--- a/lib/lp/answers/browser/tests/test_questiontarget.py
+++ b/lib/lp/answers/browser/tests/test_questiontarget.py
@@ -7,13 +7,13 @@ from __future__ import absolute_import, print_function, unicode_literals
 
 __metaclass__ = type
 
+import json
 import os
 
 from lazr.restful.interfaces import (
     IJSONRequestCache,
     IWebServiceClientRequest,
     )
-from simplejson import dumps
 import six
 from six.moves.urllib.parse import quote
 from zope.component import getUtility
@@ -276,7 +276,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
     def test_data_no_answer_contacts(self):
         question = self.factory.makeQuestion()
         view = create_view(question.target, '+portlet-answercontacts-details')
-        self.assertEqual(dumps([]), view.answercontact_data_js)
+        self.assertEqual([], json.loads(view.answercontact_data_js))
 
     def test_data_person_answercontact(self):
         # answercontact_data_js returns JSON string of a list
@@ -302,7 +302,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
                 }
             }
         self.assertEqual(
-            dumps([expected_result]), view.answercontact_data_js)
+            [expected_result], json.loads(view.answercontact_data_js))
 
     def test_data_team_answer_contact(self):
         # For a team answer contacts, answercontact_data_js has is_team set
@@ -329,7 +329,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
                 }
             }
         self.assertEqual(
-            dumps([expected_result]), view.answercontact_data_js)
+            [expected_result], json.loads(view.answercontact_data_js))
 
     def test_data_team_answercontact_owner_looks(self):
         # For a team subscription, answercontact_data_js has can_edit
@@ -357,7 +357,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
             }
         with person_logged_in(contact.teamowner):
             self.assertEqual(
-                dumps([expected_result]), view.answercontact_data_js)
+                [expected_result], json.loads(view.answercontact_data_js))
 
     def test_data_team_subscription_member_looks(self):
         # For a team subscription, answercontact_data_js has can_edit
@@ -387,7 +387,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
             }
         with person_logged_in(contact.teamowner):
             self.assertEqual(
-                dumps([expected_result]), view.answercontact_data_js)
+                [expected_result], json.loads(view.answercontact_data_js))
 
     def test_data_target_owner_answercontact_looks(self):
         # Answercontact_data_js has can_edit set to true for target owner.
@@ -413,7 +413,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
             }
         with person_logged_in(distro.owner):
             self.assertEqual(
-                dumps([expected_result]), view.answercontact_data_js)
+                [expected_result], json.loads(view.answercontact_data_js))
 
     def test_data_subscription_lp_admin(self):
         # For a subscription, answercontact_data_js has can_edit
@@ -443,7 +443,7 @@ class QuestionTargetPortletAnswerContactsWithDetailsTests(
         admin = getUtility(IPersonSet).find(ADMIN_EMAIL).any()
         with person_logged_in(admin):
             self.assertEqual(
-                dumps([expected_result]), view.answercontact_data_js)
+                [expected_result], json.loads(view.answercontact_data_js))
 
 
 class TestQuestionTargetPortletAnswerContacts(TestCaseWithFactory):
diff --git a/lib/lp/bugs/browser/tests/test_bugsubscription_views.py b/lib/lp/bugs/browser/tests/test_bugsubscription_views.py
index d6566dd..5dd87d0 100644
--- a/lib/lp/bugs/browser/tests/test_bugsubscription_views.py
+++ b/lib/lp/bugs/browser/tests/test_bugsubscription_views.py
@@ -5,8 +5,9 @@
 
 __metaclass__ = type
 
+import json
+
 from lazr.restful.interfaces import IWebServiceClientRequest
-from simplejson import dumps
 from storm.store import Store
 from testtools.matchers import Equals
 from zope.component import getUtility
@@ -528,7 +529,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
     def test_data_no_subscriptions(self):
         bug = self._makeBugWithNoSubscribers()
         harness = LaunchpadFormHarness(bug, BugPortletSubscribersWithDetails)
-        self.assertEqual(dumps([]), harness.view.subscriber_data_js)
+        self.assertEqual([], json.loads(harness.view.subscriber_data_js))
 
     def test_data_person_subscription(self):
         # subscriber_data_js returns JSON string of a list
@@ -556,7 +557,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             'subscription_level': "Lifecycle",
             }
         self.assertEqual(
-            dumps([expected_result]), harness.view.subscriber_data_js)
+            [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_person_subscription_other_subscriber(self):
         # Ensure subscriber_data_js does the correct thing when the person who
@@ -586,7 +587,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             'subscription_level': "Lifecycle",
             }
         self.assertEqual(
-            dumps([expected_result]), harness.view.subscriber_data_js)
+            [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_person_subscription_other_subscriber_query_count(self):
         # All subscriber data should be retrieved with a single query.
@@ -634,7 +635,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             'subscription_level': "Lifecycle",
             }
         self.assertEqual(
-            dumps([expected_result]), harness.view.subscriber_data_js)
+            [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def _test_data_private_team_subscription(self, authenticated_user):
         # For a private team subscription, the team name and url are rendered
@@ -700,7 +701,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
                 'subscription_level': "Maybe",
                 }]
         self.assertEqual(
-            dumps(expected_result), view.subscriber_data_js)
+            expected_result, json.loads(view.subscriber_data_js))
 
     def test_data_private_team_subscription_authenticated_user(self):
         # For a logged in user, private team subscriptions are rendered.
@@ -740,7 +741,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             }
         with person_logged_in(subscriber.teamowner):
             self.assertEqual(
-                dumps([expected_result]), harness.view.subscriber_data_js)
+                [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_team_subscription_member_looks(self):
         # For a team subscription, subscriber_data_js has can_edit
@@ -774,7 +775,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             }
         with person_logged_in(subscriber.teamowner):
             self.assertEqual(
-                dumps([expected_result]), harness.view.subscriber_data_js)
+                [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_subscription_lp_admin(self):
         # For a subscription, subscriber_data_js has can_edit
@@ -807,7 +808,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
         admin = getUtility(IPersonSet).find(ADMIN_EMAIL).any()
         with person_logged_in(admin):
             self.assertEqual(
-                dumps([expected_result]), harness.view.subscriber_data_js)
+                [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_person_subscription_subscriber(self):
         # For a subscription, subscriber_data_js has can_edit
@@ -837,7 +838,7 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
             }
         with person_logged_in(subscribed_by):
             self.assertEqual(
-                dumps([expected_result]), harness.view.subscriber_data_js)
+                [expected_result], json.loads(harness.view.subscriber_data_js))
 
     def test_data_person_subscription_user_excluded(self):
         # With the subscriber logged in, they are not included in the results.
@@ -850,4 +851,4 @@ class BugPortletSubscribersWithDetailsTests(TestCaseWithFactory):
                           level=BugNotificationLevel.LIFECYCLE)
             harness = LaunchpadFormHarness(
                 bug, BugPortletSubscribersWithDetails)
-            self.assertEqual(dumps([]), harness.view.subscriber_data_js)
+            self.assertEqual([], json.loads(harness.view.subscriber_data_js))
diff --git a/lib/lp/services/webapp/tests/test_publisher.py b/lib/lp/services/webapp/tests/test_publisher.py
index 40e4fcd..728e228 100644
--- a/lib/lp/services/webapp/tests/test_publisher.py
+++ b/lib/lp/services/webapp/tests/test_publisher.py
@@ -6,13 +6,13 @@ from doctest import (
     ELLIPSIS,
     )
 import io
+import json
 from unittest import (
     TestLoader,
     TestSuite,
     )
 
 from lazr.restful.interfaces import IJSONRequestCache
-import simplejson
 from zope.component import getUtility
 from zope.interface import implementer
 
@@ -59,7 +59,8 @@ class TestLaunchpadView(TestCaseWithFactory):
 
     def test_getCacheJSON_non_resource_context(self):
         view = LaunchpadView(object(), LaunchpadTestRequest())
-        self.assertEqual('{"related_features": {}}', view.getCacheJSON())
+        self.assertEqual(
+            {"related_features": {}}, json.loads(view.getCacheJSON()))
 
     @staticmethod
     def getCanada():
@@ -78,7 +79,7 @@ class TestLaunchpadView(TestCaseWithFactory):
 
     def test_getCacheJSON_resource_context(self):
         view = LaunchpadView(self.getCanada(), LaunchpadTestRequest())
-        json_dict = simplejson.loads(view.getCacheJSON())['context']
+        json_dict = json.loads(view.getCacheJSON())['context']
         self.assertIsCanada(json_dict)
 
     def test_getCacheJSON_non_resource_object(self):
@@ -87,15 +88,15 @@ class TestLaunchpadView(TestCaseWithFactory):
         IJSONRequestCache(request).objects['my_bool'] = True
         with person_logged_in(self.factory.makePerson()):
             self.assertEqual(
-                '{"related_features": {}, "my_bool": true}',
-                view.getCacheJSON())
+                {"related_features": {}, "my_bool": True},
+                json.loads(view.getCacheJSON()))
 
     def test_getCacheJSON_resource_object(self):
         request = LaunchpadTestRequest()
         view = LaunchpadView(object(), request)
         IJSONRequestCache(request).objects['country'] = self.getCanada()
         with person_logged_in(self.factory.makePerson()):
-            json_dict = simplejson.loads(view.getCacheJSON())['country']
+            json_dict = json.loads(view.getCacheJSON())['country']
         self.assertIsCanada(json_dict)
 
     def test_getCacheJSON_context_overrides_objects(self):
@@ -103,7 +104,7 @@ class TestLaunchpadView(TestCaseWithFactory):
         view = LaunchpadView(self.getCanada(), request)
         IJSONRequestCache(request).objects['context'] = True
         with person_logged_in(self.factory.makePerson()):
-            json_dict = simplejson.loads(view.getCacheJSON())['context']
+            json_dict = json.loads(view.getCacheJSON())['context']
         self.assertIsCanada(json_dict)
 
     def test_getCache_anonymous(self):
@@ -111,7 +112,7 @@ class TestLaunchpadView(TestCaseWithFactory):
         view = LaunchpadView(self.getCanada(), request)
         self.assertIs(None, view.user)
         IJSONRequestCache(request).objects['my_bool'] = True
-        json_dict = simplejson.loads(view.getCacheJSON())
+        json_dict = json.loads(view.getCacheJSON())
         self.assertIsCanada(json_dict['context'])
         self.assertIn('my_bool', json_dict)
 
@@ -127,7 +128,7 @@ class TestLaunchpadView(TestCaseWithFactory):
         # A redirection view by default provides no json cache data.
         request = LaunchpadTestRequest()
         view = RedirectionView(None, request)
-        json_dict = simplejson.loads(view.getCacheJSON())
+        json_dict = json.loads(view.getCacheJSON())
         self.assertEqual({}, json_dict)
 
     def test_getCache_redirected_view(self):
@@ -141,7 +142,7 @@ class TestLaunchpadView(TestCaseWithFactory):
         test_view = TestView(self.getCanada(), request)
         view = RedirectionView(None, request, cache_view=test_view)
         IJSONRequestCache(request).objects['my_bool'] = True
-        json_dict = simplejson.loads(view.getCacheJSON())
+        json_dict = json.loads(view.getCacheJSON())
         self.assertIsCanada(json_dict['context'])
         self.assertIn('my_bool', json_dict)
 
@@ -327,13 +328,17 @@ class TestLaunchpadView(TestCaseWithFactory):
         view = TestView(object(), request)
         with person_logged_in(self.factory.makePerson()):
             self.assertEqual(
-                '{"related_features": {"test_feature": {'
-                '"url": "http://wiki.lp.dev/LEP/sample";, '
-                '"is_beta": true, '
-                '"value": "on", '
-                '"title": "title"'
-                '}}}',
-                view.getCacheJSON())
+                {
+                    "related_features": {
+                        "test_feature": {
+                            "is_beta": True,
+                            "title": "title",
+                            "url": "http://wiki.lp.dev/LEP/sample";,
+                            "value": "on",
+                            },
+                        },
+                    },
+                json.loads(view.getCacheJSON()))
 
     def test_json_cache_collects_related_features_from_all_views(self):
         # A typical page includes data from more than one view,
@@ -353,12 +358,23 @@ class TestLaunchpadView(TestCaseWithFactory):
         TestView2(object(), request)
         with person_logged_in(self.factory.makePerson()):
             self.assertEqual(
-                '{"related_features": '
-                '{"test_feature_2": {"url": "http://wiki.lp.dev/LEP/sample2";,'
-                ' "is_beta": true, "value": "on", "title": "title"}, '
-                '"test_feature": {"url": "http://wiki.lp.dev/LEP/sample";, '
-                '"is_beta": true, "value": "on", "title": "title"}}}',
-                view.getCacheJSON())
+                {
+                    "related_features": {
+                        "test_feature": {
+                            "is_beta": True,
+                            "title": "title",
+                            "url": "http://wiki.lp.dev/LEP/sample";,
+                            "value": "on",
+                            },
+                        "test_feature_2": {
+                            "is_beta": True,
+                            "title": "title",
+                            "url": "http://wiki.lp.dev/LEP/sample2";,
+                            "value": "on",
+                            },
+                        },
+                    },
+                json.loads(view.getCacheJSON()))
 
     def test_view_creation_with_fake_or_none_request(self):
         # LaunchpadView.__init__() does not crash with a FakeRequest.