Permalink
Browse files

implement api methods for alerts

  • Loading branch information...
pyeremenko committed Feb 20, 2014
1 parent f9c3e1f commit 2058fc8311da9f3783d62efcb23de638ee115676
Showing with 63 additions and 0 deletions.
  1. +27 −0 iron_mq.py
  2. +36 −0 test.py
View
@@ -169,6 +169,29 @@ def delete_queue(self):
return response['body']
+ def add_alerts(self, *alerts):
+ url = "queues/%s/alerts" % self.name
+ body = json.dumps({'alerts': alerts})
+ response = self.client.post(url, body=body, headers={"Content-Type":"application/json"})
+ return response['body']
+
+ def update_alerts(self, *alerts):
+ url = "queues/%s/alerts" % self.name
+ body = json.dumps({'alerts': alerts})
+ response = self.client.put(url, body=body, headers={"Content-Type":"application/json"})
+ return response['body']
+
+ def remove_alerts(self, *alert_ids):
+ url = "queues/%s/alerts" % self.name
+ body = json.dumps(self._prepare_alert_ids(*alert_ids))
+ response = self.client.delete(url, body=body, headers={"Content-Type":"application/json"})
+ return response['body']
+
+ def remove_alert(self, alert_id):
+ url = "queues/%s/alerts/%s" % (self.name, alert_id)
+ response = self.client.delete(url, body={}, headers={"Content-Type":"application/json"})
+ return response['body']
+
def add_subscribers(self, *subscribers):
url = "queues/%s/subscribers" % self.name
body = json.dumps(self._prepare_subscribers(*subscribers))
@@ -201,6 +224,10 @@ def delete_message_push_status(self, message_id, subscriber_id):
return response['body']
+ def _prepare_alert_ids(self, *alert_ids):
+ alerts = [{'id': id} for id in alert_ids]
+ return {'alerts': alerts}
+
def _prepare_subscribers(self, *subscribers):
subscrs = [{'url': ss} for ss in subscribers]
View
36 test.py
@@ -1,19 +1,55 @@
from iron_mq import *
import unittest
+import random
import time
class TestIronMQ(unittest.TestCase):
def setUp(self):
self.mq = IronMQ()
+ self.random_number = str(int(random.random() * 10**10))
def test_postMessage(self):
q = self.mq.queue("test_queue")
old_size = q.size()
q.post("test message")
self.assertEqual(old_size, q.size() - 1)
+ def test_addAlerts(self):
+ q = self.mq.queue("test_queue" + self.random_number)
+ result = q.add_alerts({'type': 'fixed', 'direction': 'desc', 'trigger': 1000, 'queue': 'a_q'})
+ self.assertEqual(result['msg'], 'Updated')
+
+ def test_updateAlerts(self):
+ q = self.mq.queue("test_queue")
+ result = q.update_alerts({'type': 'fixed', 'direction': 'desc', 'trigger': 1000, 'queue': 'a_q'})
+ self.assertEqual(result['msg'], 'Updated')
+
+ def test_infoShouldReturnAlerts(self):
+ q = self.mq.queue("test_queue" + self.random_number)
+ q.add_alerts({'type': 'fixed', 'direction': 'desc', 'trigger': 1000, 'queue': 'a_q'})
+ info = q.info()
+ self.assertTrue('alerts' in info)
+ self.assertEqual(len(info['alerts']), 1)
+
+ def test_removeAlerts(self):
+ q = self.mq.queue("test_queue" + self.random_number)
+ q.add_alerts({'type': 'fixed', 'direction': 'desc', 'trigger': 1000, 'queue': 'a_q'}, {'type': 'fixed', 'direction': 'asc', 'trigger': 10000, 'queue': 'a_q'}, {'type': 'progressive', 'direction': 'asc', 'trigger': 500, 'queue': 'a_q'})
+ alerts = [alert['id'] for alert in q.info()['alerts']][0:2]
+ last_alert = q.info()['alerts'][2]
+ result = q.remove_alerts(*alerts)
+ self.assertEqual(result['msg'], 'Deleted')
+ self.assertEqual(len(q.info()['alerts']), 1)
+ self.assertEqual(q.info()['alerts'][0]['id'], last_alert['id'])
+
+ def test_removeAlert(self):
+ q = self.mq.queue("test_queue" + self.random_number)
+ q.add_alerts({'type': 'fixed', 'direction': 'desc', 'trigger': 1000, 'queue': 'a_q'}, {'type': 'fixed', 'direction': 'asc', 'trigger': 10000, 'queue': 'a_q'})
+ result = q.remove_alert(q.info()['alerts'][0]['id'])
+ self.assertEqual(result['msg'], 'Deleted')
+ self.assertEqual(len(q.info()['alerts']), 1)
+
def test_getMessage(self):
msg = "%s" % time.time()
q = self.mq.queue("test_queue")

0 comments on commit 2058fc8

Please sign in to comment.