def test_subscriptions_post(self): pvnames = ("test:long1", "test:long2") modedefs = ({"mode": "Monitor", "delta": 1.1}, {"mode": "Scan", "period": 0.1}) self._subscribe_to_many(pvnames, modedefs) # read them back response = self.fetch('/subscriptions/') self.assertEqual(response.code, 200) self.assertFalse(response.error) res = [ {'pvname': pvname, 'mode': SubscriptionMode.parse(modedef)} \ for pvname, modedef in zip(pvnames, modedefs) ] expected = envelope(True, 200, 'ok', res) decodedbody = json_decode(response.body) self.assertEqual(decodedbody['status'], expected['status']) d = dict( zip(pvnames, modedefs) ) responses = decodedbody['response'] self.assertEqual( len(responses), len(pvnames) ) for pvname, pvdict in responses.iteritems(): self.assertEqual(pvname, pvdict['name']) self.assertIn(pvdict['name'], pvnames) m = SubscriptionMode.parse( d[pvdict['name']] ) self.assertEquals(pvdict['mode'], m)
def test_subscriptions_get(self): # this first fetch with a trailing slash response = self.fetch('/subscriptions/') self.assertEqual(response.code, 200, response.error) # empty expected = envelope(True, 200, 'ok', {}) decodedbody = json_decode(response.body) self.assertEqual(decodedbody, expected) # test alphabetical ordering of returned pvs pvnames = ("test:long1", "test:double1", "test:double2", "test:long3") modedefs = ({"mode": "Monitor", "delta": 1.1}, {"mode": "Scan", "period": 0.1}, {"mode": "Scan", "period": 0.2}, {"mode": "Scan", "period": 0.3}, ) self._subscribe_to_many(pvnames, modedefs) # second fetch WITHOUT a trailing slash response = self.fetch('/subscriptions') self.assertEqual(response.code, 200) self.assertFalse(response.error) responses = json_decode(response.body)['response'] self.assertEqual(len(responses), len(pvnames)) self.assertItemsEqual( responses.keys(), pvnames ) for pvname, modedef in zip(pvnames, modedefs): self.assertEqual(responses[pvname]['name'], pvname) self.assertEqual(responses[pvname]['mode'], SubscriptionMode.parse(modedef))