def test_is_valid(self): rec = TurboBeeMsg() rec.qid = 'foo' self.assertTrue(rec.is_valid()) rec.qid = 'unlimiteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeed' * 512 self.assertTrue(rec.is_valid()) rec.status = Status.new now = datetime.utcnow() rec.created = rec.get_timestamp(now) self.assertEqual(now, rec.get_datetime(rec.created)) rec.value = 'foobar' rec.ctype = rec.ContentType.text rec.ctype = 0 # unknown self.assertTrue(rec.is_valid()) rec.set_value(1) self.assertTrue(rec.is_valid()) rec.set_value(u'\ud789') rec.ctype = rec.ContentType.text self.assertEqual(rec.get_value(), u'\ud789'.encode('utf8')) rec.ctype = rec.ContentType.binary self.assertEqual(rec.get_value(), u'\ud789'.encode('utf8')) rec.set_value({'foo': u'\ud789'}, rec.ContentType.json) self.assertEqual(rec.get_value(), {'foo': u'\ud789'}) rec.set_value(u'\ud789', rec.ContentType.text) self.assertEqual(rec.get_value(), u'\ud789'.encode('utf8'))
def test_store_post(self): msg = TurboBeeMsg() now = dt.datetime.utcnow() msg.created = msg.get_timestamp(now) msg.updated = msg.get_timestamp(now) msg.expires = msg.get_timestamp(now) msg.eol = msg.get_timestamp(now) msg.set_value('hello world') msg.ctype = msg.ContentType.html msg.target = 'https:///some.com' msg.owner = 234 my_data = { 'file_field': (StringIO(msg.dump()[1]), 'turbobee_msg.proto') } r = self.client.post(url_for('turbobee_app.store', qid='asdf'), content_type='multipart/form-data', data=my_data) self.assertEqual(r.status_code, 200) assert len(r.json['created']) == 1 msg2 = msg.loads(*msg.dump()) msg.qid = r.json['created'][0] r = self.client.post(url_for('turbobee_app.store'), content_type='multipart/form-data', data={ 'foo': (StringIO(msg.dump()[1]), 'turbobee_msg.proto'), 'bar': (StringIO(msg2.dump()[1]), 'turbobee_msg.proto'), }) self.assertEqual(r.status_code, 200) assert len(r.json['created']) == 1 assert len(r.json['updated']) == 1 r = self.client.get(url_for('turbobee_app.store_get', qid=msg.qid)) self.assertEqual(r.status_code, 200) r = self.client.head(url_for('turbobee_app.store_get', qid=msg.qid)) self.assertEqual(r.status_code, 200) r = self.client.get(url_for('turbobee_app.store_get', qid='foo')) self.assertEqual(r.status_code, 404) r = self.client.head(url_for('turbobee_app.store_get', qid='foo')) self.assertEqual(r.status_code, 404)
def test_serializer(self): # check we are not movng dates (by loosing nanosec precision) rec = TurboBeeMsg() now = datetime.utcnow() rec.created = rec.get_timestamp(now) for i in xrange(10050): rec = rec.loads(*rec.dump()) self.assertEqual(rec.get_datetime(rec.created), now)
def test_set_get_pages(self): msg = TurboBeeMsg() now = datetime.utcnow() msg.created = msg.get_timestamp(now) msg.updated = msg.get_timestamp(now) msg.expires = msg.get_timestamp(now) msg.eol = msg.get_timestamp(now) msg.set_value('hello world') msg.ctype = msg.ContentType.html msg.target = 'https:///some.com' msg.owner = 234 r = self.app.set_pages([msg]) assert 'created' in r assert len(r['created']) ==1 pages = list(self.app.get_pages(r['created'])) expected = { 'id': 1, 'target': u'https:///some.com', 'content_type': u'application/html', 'content': 'hello world', 'created': get_date(now).isoformat(), 'updated': get_date(now).isoformat(), 'expires': get_date(now).isoformat(), 'lifetime': get_date(now).isoformat(), 'owner': 234, 'qid': pages[0]['qid'] } assert pages[0] == expected msg.qid = pages[0]['qid'] r = self.app.set_pages([msg]) assert 'updated' in r assert len(r['updated']) ==1 assert r['updated'][0] == expected['qid'] msg.status = Status.deleted r = self.app.set_pages([msg]) assert 'deleted' in r assert r['deleted'][0] == expected['qid'] r = self.app.set_pages([msg]) assert r['ignored-deleted'][0] == expected['qid'] assert len(list(self.app.get_pages(expected['qid']))) == 0 # insert it again msg.status = Status.active r = self.app.set_pages([msg]) assert r['created'][0] assert r['created'][0] != expected['qid'] l = list(self.app.get_pages(r['created'], fields=['foo', 'qid', 'content', 'created'])) assert l[0]['qid'] == r['created'][0] assert l[0]['created'] == expected['created'] assert l[0]['content'] == 'hello world' assert 'updated' not in l[0] assert 'foo' not in l[0] # set multiple objects at once msg.qid = r['created'][0] msg2 = msg.loads(*msg.dump()) msg2.qid = '' r = self.app.set_pages([msg, msg2]) assert r['created'][0] assert r['updated'][0] == msg.qid # update one by one msg2.qid = r['created'][0] r = self.app.set_pages([msg, msg2], one_by_one=True) assert msg.qid in r['updated'] assert msg2.qid in r['updated'] r = self.app.set_pages([msg, msg2, msg, msg, msg], one_by_one=True) assert set(r['updated']) == set([msg.qid, msg2.qid])