def testGetPredefinedMessage_OK(self): lab_name = "alab" predefined_message_entities = [ datastore_entities.PredefinedMessage( lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content="offline_reason1", used_count=2), datastore_entities.PredefinedMessage( lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content="recovery_action1", used_count=5), ] ndb.put_multi(predefined_message_entities) message_1 = note_manager.GetPredefinedMessage( api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, lab_name, "offline_reason1") self.assertEqual("offline_reason1", message_1.content) self.assertEqual(api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, message_1.type) message_2 = note_manager.GetPredefinedMessage( api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, lab_name, "recovery_action1") self.assertEqual("recovery_action1", message_2.content) self.assertEqual(api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, message_2.type)
def testListPredefinedMessages_filtersAndOrdering(self): """Test list PredefinedMessages.""" pred_msg_entities = [ datastore_entities.PredefinedMessage( lab_name='lab-name-1', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-1', used_count=2), datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-2', used_count=1), datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-3', used_count=3), datastore_entities.PredefinedMessage( lab_name='lab-name-4', type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content='content-4', used_count=3), ] ndb.put_multi(pred_msg_entities) api_request = { 'type': 'DEVICE_RECOVERY_ACTION', 'lab_name': 'lab-name-2' } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.ListPredefinedMessages', api_request) pred_msgs = protojson.decode_message( api_messages.PredefinedMessageCollection, api_response.body).predefined_messages # The results are filtered by type and lab name. self.assertEqual(2, len(pred_msgs)) # The results are sorted by count in descending order. self.assertEqual(pred_msg_entities[1].lab_name, pred_msgs[1].lab_name) self.assertEqual(pred_msg_entities[1].content, pred_msgs[1].content) self.assertEqual(pred_msg_entities[1].type, pred_msgs[1].type) self.assertEqual(pred_msg_entities[1].used_count, pred_msgs[1].used_count) self.assertEqual(pred_msg_entities[2].lab_name, pred_msgs[0].lab_name) self.assertEqual(pred_msg_entities[2].content, pred_msgs[0].content) self.assertEqual(pred_msg_entities[2].type, pred_msgs[0].type) self.assertEqual(pred_msg_entities[2].used_count, pred_msgs[0].used_count)
def testUpdatePredefinedMessage_failAlreadyExist(self): predefined_messages = [ datastore_entities.PredefinedMessage( lab_name='lab1', type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content='content-1'), datastore_entities.PredefinedMessage( lab_name='lab1', type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content='content-2'), ] keys = ndb.put_multi(predefined_messages) api_request = { 'id': keys[0].id(), # the id of the 1st message 'content': 'content-2', # the content of the 2nd message } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.UpdatePredefinedMessage', api_request, expect_errors=True) self.assertEqual('409 Conflict', api_response.status)
def testGetOrCreatePredefinedMessage_ExistingMessage(self): lab_name = "alab" content = "content1" message_id = 111 datastore_entities.PredefinedMessage( key=ndb.Key(datastore_entities.PredefinedMessage, message_id), lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content=content, used_count=2).put() message = note_manager.GetOrCreatePredefinedMessage( api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, lab_name, content) self.assertEqual(message_id, message.key.id())
def testPredefinedMessage(self): """Tests PredefinedMessage store entity operations.""" predefined_message = datastore_entities.PredefinedMessage( lab_name='lab-name-1', type=common.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-1', create_timestamp=TIMESTAMP_OLD, used_count=2) key = predefined_message.put() queried = key.get() self.assertEqual('lab-name-1', queried.lab_name) self.assertEqual(common.PredefinedMessageType.DEVICE_RECOVERY_ACTION, queried.type) self.assertEqual('content-1', queried.content) self.assertEqual(2, queried.used_count)
def testPredefinedMessageFromEntity(self): entity = datastore_entities.PredefinedMessage( key=ndb.Key(datastore_entities.PredefinedMessage, 123456789), lab_name='lab-name-01', type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content='device offline reason 1', create_timestamp=TIMESTAMP, used_count=4) msg = datastore_entities.ToMessage(entity) self.assertEqual(123456789, msg.id) self.assertEqual(entity.lab_name, msg.lab_name) self.assertEqual(entity.type, msg.type) self.assertEqual(entity.content, msg.content) self.assertEqual(entity.create_timestamp, msg.create_timestamp) self.assertEqual(entity.used_count, msg.used_count)
def testCreatePredefinedMessage_failConflict(self): lab_name = 'lab_1' content = 'device went down' datastore_entities.PredefinedMessage( lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content=content).put() api_request = { 'type': 'DEVICE_OFFLINE_REASON', 'lab_name': lab_name, 'content': content, } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.CreatePredefinedMessage', api_request, expect_errors=True) self.assertEqual('409 Conflict', api_response.status)
def testPreparePredefinedMessageForNote_withValidId(self): message_id = 111 lab_name = "alab" content = "content1" datastore_entities.PredefinedMessage( key=ndb.Key(datastore_entities.PredefinedMessage, message_id), lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content=content, used_count=2).put() message = note_manager.PreparePredefinedMessageForNote( api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, message_id=message_id) self.assertEqual(message_id, message.key.id()) self.assertEqual(lab_name, message.lab_name) self.assertEqual(content, message.content) self.assertEqual(3, message.used_count) # the used_count increases
def GetOrCreatePredefinedMessage(message_type, lab_name, content): """Get PredefinedMessage datastore entity or create it if not existing. Args: message_type: enum, common.PredefinedMessageType, type of PredefinedMessage. lab_name: str, the lab where the message is created. content: str, content of the message. Returns: An instance of datastore_entities.PredefinedMessage. """ exisiting_predefined_message_entity = GetPredefinedMessage( message_type=message_type, lab_name=lab_name, content=content) if exisiting_predefined_message_entity: return exisiting_predefined_message_entity else: return datastore_entities.PredefinedMessage( type=message_type, content=content, lab_name=lab_name, create_timestamp=datetime.datetime.utcnow())
def testDeletePredefinedMessage_succeed(self): content = 'some content' lab_name = 'lab_1' entity = datastore_entities.PredefinedMessage( lab_name=lab_name, type=api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, content=content) key = entity.put() api_request = { 'id': key.id(), } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.DeletePredefinedMessage', api_request) self.assertEqual('200 OK', api_response.status) created_predefined_message = protojson.decode_message( api_messages.PredefinedMessage, api_response.body) self.assertEqual( api_messages.PredefinedMessageType.DEVICE_OFFLINE_REASON, created_predefined_message.type) self.assertEqual(lab_name, created_predefined_message.lab_name) self.assertEqual(content, created_predefined_message.content)
def testListPredefinedMessages_countAndCursor(self): """Test list PredefinedMessages.""" pred_msg_entities = [ datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-1', used_count=4), datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-2', used_count=3), datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-3', used_count=2), datastore_entities.PredefinedMessage( lab_name='lab-name-2', type=api_messages.PredefinedMessageType.DEVICE_RECOVERY_ACTION, content='content-4', used_count=1), ] ndb.put_multi(pred_msg_entities) # look up the first page api_request = { 'type': 'DEVICE_RECOVERY_ACTION', 'lab_name': 'lab-name-2', 'count': 2, } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.ListPredefinedMessages', api_request) pred_msg_collection = protojson.decode_message( api_messages.PredefinedMessageCollection, api_response.body) pred_msgs = pred_msg_collection.predefined_messages self.assertEqual(2, len(pred_msgs)) self.assertEqual(pred_msg_entities[0].content, pred_msgs[0].content) self.assertEqual(pred_msg_entities[1].content, pred_msgs[1].content) # look up the second page with next_cursor api_request = { 'type': 'DEVICE_RECOVERY_ACTION', 'lab_name': 'lab-name-2', 'count': 2, 'cursor': pred_msg_collection.next_cursor, } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.ListPredefinedMessages', api_request) pred_msg_collection = protojson.decode_message( api_messages.PredefinedMessageCollection, api_response.body) pred_msgs = pred_msg_collection.predefined_messages self.assertEqual(2, len(pred_msgs)) self.assertEqual(pred_msg_entities[2].content, pred_msgs[0].content) self.assertEqual(pred_msg_entities[3].content, pred_msgs[1].content) # look up the first page again with prev_cursor of the second page api_request = { 'type': 'DEVICE_RECOVERY_ACTION', 'lab_name': 'lab-name-2', 'count': 2, 'cursor': pred_msg_collection.prev_cursor, 'backwards': True, } api_response = self.testapp.post_json( '/_ah/api/PredefinedMessageApi.ListPredefinedMessages', api_request) pred_msg_collection = protojson.decode_message( api_messages.PredefinedMessageCollection, api_response.body) pred_msgs = pred_msg_collection.predefined_messages self.assertEqual(2, len(pred_msgs)) self.assertEqual(pred_msg_entities[0].content, pred_msgs[0].content) self.assertEqual(pred_msg_entities[1].content, pred_msgs[1].content)