def test_message_serialization(self): msg_id = rand_string() creation_time_utc = rand_date_utc() expire_at_utc = rand_date_utc() producer = rand_string() topic = rand_string() actual = self._get_object(Message, { 'msg_id': msg_id, 'creation_time_utc': creation_time_utc, 'expire_at_utc': expire_at_utc, 'producer': producer, 'topic':topic, }) expected = { 'mime_type': PUB_SUB.DEFAULT_MIME_TYPE, 'msg_id': msg_id, 'topic': topic, 'expiration': PUB_SUB.DEFAULT_EXPIRATION, 'producer': producer, 'creation_time_utc': creation_time_utc.isoformat(), 'priority': PUB_SUB.DEFAULT_PRIORITY, 'expire_at_utc': expire_at_utc.isoformat() } # Dicts must be equal .. assert_equal(actual.to_dict(), expected) # .. as well as JSON. json = actual.to_json() self.assertIsInstance(json, str) unjsonified = loads(json) assert_equal(unjsonified, expected)
def test_message_serialization(self): msg_id = rand_string() creation_time_utc = rand_date_utc() expire_at_utc = rand_date_utc() producer = rand_string() topic = rand_string() actual = self._get_object( Message, { 'msg_id': msg_id, 'creation_time_utc': creation_time_utc, 'expire_at_utc': expire_at_utc, 'producer': producer, 'topic': topic, }) expected = { 'mime_type': PUB_SUB.DEFAULT_MIME_TYPE, 'msg_id': msg_id, 'topic': topic, 'expiration': PUB_SUB.DEFAULT_EXPIRATION, 'producer': producer, 'creation_time_utc': creation_time_utc.isoformat(), 'priority': PUB_SUB.DEFAULT_PRIORITY, 'expire_at_utc': expire_at_utc.isoformat() } # Dicts must be equal .. assert_equal(actual.to_dict(), expected) # .. as well as JSON. json = actual.to_json() self.assertIsInstance(json, str) unjsonified = loads(json) assert_equal(unjsonified, expected)
def get_job(name=None, interval_in_seconds=None, start_time=None, max_repeats=None, callback=None, prefix='job'): name = name or '{}-{}'.format(prefix, rand_string()) interval_in_seconds = interval_in_seconds or rand_int() start_time = start_time or rand_date_utc() callback = callback or dummy_callback return Job(rand_int(), name, SCHEDULER.JOB_TYPE.INTERVAL_BASED, Interval(in_seconds=interval_in_seconds), start_time, callback, max_repeats=max_repeats)
def get_job(name=None, interval_in_seconds=None, start_time=None, max_repeats=None, callback=None): name = name or rand_string() interval_in_seconds = interval_in_seconds or rand_int() start_time = start_time or rand_date_utc() callback = callback or dummy_callback return Job(rand_int(), name, SCHEDULER.JOB_TYPE.INTERVAL_BASED, Interval(in_seconds=interval_in_seconds), start_time, callback, max_repeats=max_repeats)
def test_get_context(self): id = rand_int() name = rand_string() start_time = rand_date_utc() interval_in_seconds = rand_int() max_repeats_reached = rand_bool() current_run, max_repeats = rand_int(count=2) cb_kwargs = {rand_string(): rand_string()} for job_type in SCHEDULER.JOB_TYPE.INTERVAL_BASED, SCHEDULER.JOB_TYPE.CRON_STYLE: interval=Interval(in_seconds=interval_in_seconds) if \ job_type == SCHEDULER.JOB_TYPE.INTERVAL_BASED else CronTab(DEFAULT_CRON_DEFINITION) job = Job(id, name, job_type, cb_kwargs=cb_kwargs, interval=interval) job.start_time = start_time job.current_run = current_run job.max_repeats = max_repeats job.max_repeats_reached = max_repeats_reached if job_type == SCHEDULER.JOB_TYPE.CRON_STYLE: job.cron_definition = DEFAULT_CRON_DEFINITION ctx = job.get_context() cid = ctx.pop('cid') self.assertTrue(is_like_cid(cid)) expected = { 'current_run': current_run, 'id': id, 'name': name, 'start_time': start_time.isoformat(), 'max_repeats': max_repeats, 'max_repeats_reached': max_repeats_reached, 'cb_kwargs': cb_kwargs, 'type': job_type, } if job_type == SCHEDULER.JOB_TYPE.CRON_STYLE: expected['cron_definition'] = job.cron_definition else: expected['interval_in_seconds'] = job.interval.in_seconds self.assertDictEqual(ctx, expected)
def test_message_serialization(self): msg_id = rand_string() creation_time_utc = rand_date_utc() expire_at_utc = rand_date_utc() producer = rand_string() topic = rand_string() actual = self._get_object( Message, { "msg_id": msg_id, "creation_time_utc": creation_time_utc, "expire_at_utc": expire_at_utc, "producer": producer, "topic": topic, }, ) expected = { "mime_type": PUB_SUB.DEFAULT_MIME_TYPE, "msg_id": msg_id, "topic": topic, "expiration": PUB_SUB.DEFAULT_EXPIRATION, "producer": producer, "creation_time_utc": creation_time_utc.isoformat(), "priority": PUB_SUB.DEFAULT_PRIORITY, "expire_at_utc": expire_at_utc.isoformat(), } # Dicts must be equal .. assert_equal(actual.to_dict(), expected) # .. as well as JSON. json = actual.to_json() self.assertIsInstance(json, str) unjsonified = loads(json) assert_equal(unjsonified, expected)
def test_get_context(self): id = rand_int() name = rand_string() start_time = rand_date_utc() interval_in_seconds = rand_int() max_repeats_reached = rand_bool() current_run, max_repeats = rand_int(count=2) cb_kwargs = {rand_string():rand_string()} for job_type in SCHEDULER.JOB_TYPE.INTERVAL_BASED, SCHEDULER.JOB_TYPE.CRON_STYLE: interval=Interval(in_seconds=interval_in_seconds) if \ job_type == SCHEDULER.JOB_TYPE.INTERVAL_BASED else CronTab(DEFAULT_CRON_DEFINITION) job = Job(id, name, job_type, cb_kwargs=cb_kwargs, interval=interval) job.start_time = start_time job.current_run = current_run job.max_repeats = max_repeats job.max_repeats_reached = max_repeats_reached if job_type == SCHEDULER.JOB_TYPE.CRON_STYLE: job.cron_definition = DEFAULT_CRON_DEFINITION ctx = job.get_context() cid = ctx.pop('cid') self.assertTrue(is_like_cid(cid)) expected = { 'current_run': current_run, 'id': id, 'name': name, 'start_time': start_time.isoformat(), 'max_repeats': max_repeats, 'max_repeats_reached': max_repeats_reached, 'cb_kwargs': cb_kwargs, 'type': job_type, } if job_type == SCHEDULER.JOB_TYPE.CRON_STYLE: expected['cron_definition'] = job.cron_definition else: expected['interval_in_seconds'] = job.interval.in_seconds self.assertDictEqual(ctx, expected)
def _rand_date_utc(): return rand_date_utc(True)