def testGetOffsetLink(self):
    mock_entity = MockEntity(6, 16)
    mock_entity.layer.auto_managed = False
    self.mox.StubOutWithMock(pages.template.django.template, 'resolve_variable')
    self.mox.StubOutWithMock(entities, '_GetEntitySiblings')
    self.mox.StubOutWithMock(entities, '_GetFlyToLink')
    dummy_siblings_list = [5, 6, 7, 8, 9, 10]

    pages.template.django.template.resolve_variable(
        '_cache', 'dummy-context').AndReturn('dummy-cache')
    pages.template.django.template.resolve_variable(
        '_link_template', 'dummy-context').AndReturn('dummy-template')
    entities._GetEntitySiblings(mock_entity, 'dummy-cache').AndReturn(
        dummy_siblings_list)
    entities._GetFlyToLink(8, 'balloon', 'dummy-template').AndReturn(
        'dummy-link1')

    pages.template.django.template.resolve_variable(
        '_cache', 'dummy-context').AndReturn('dummy-cache')
    pages.template.django.template.resolve_variable(
        '_link_template', 'dummy-context').AndReturn('dummy-template')
    entities._GetEntitySiblings(mock_entity, 'dummy-cache').AndReturn(
        dummy_siblings_list)
    entities._GetFlyToLink(10, 'balloon', 'dummy-template').AndReturn(
        'dummy-link2')

    self.mox.ReplayAll()

    link = entities._GetOffsetLink('dummy-context', mock_entity, 2, 'balloon')
    self.assertEqual(link, 'dummy-link1')

    link = entities._GetOffsetLink('dummy-context', mock_entity, -2, 'balloon')
    self.assertEqual(link, 'dummy-link2')

    mock_entity.layer.auto_managed = True
    self.assertRaises(ValueError, entities._GetOffsetLink,
                      'dummy-context', mock_entity, -2, 'balloon')
  def testGetEntitySiblings(self):
    layer = model.Layer(name='a', world='earth')
    layer_id = layer.put().id()
    other_layer = model.Layer(name='b', world='earth')
    other_layer.put()
    folder = model.Folder(layer=layer, name='c')
    folder_id = folder.put().id()
    schema = model.Schema(layer=layer, name='z')
    schema.put()
    template = model.Template(layer=layer, schema=schema, name='y', text='x')
    template_id = template.put().id()
    entity1 = model.Entity(layer=layer, name='c', template=template)
    entity1_id = entity1.put().id()
    entity2_id = model.Entity(layer=layer, name='e',
                              template=template).put().id()
    entity3_id = model.Entity(layer=other_layer, name='f',
                              template=template).put().id()
    entity4_id = model.Entity(layer=layer, name='g', template=template,
                              folder=folder, folder_index=2).put().id()
    entity5 = model.Entity(layer=layer, name='g', folder=folder, folder_index=1,
                           template=template)
    entity5_id = entity5.put().id()
    cache = {'entity_siblings': {}}

    self.assertEqual(entities._GetEntitySiblings(entity1, cache),
                     [entity1_id, entity2_id])
    self.assertEqual(cache, {'entity_siblings': {
        layer_id: [entity1_id, entity2_id]}
    })

    self.assertEqual(entities._GetEntitySiblings(entity5, cache),
                     [entity5_id, entity4_id])
    self.assertEqual(cache, {'entity_siblings': {
        layer_id: [entity1_id, entity2_id],
        folder_id: [entity5_id, entity4_id]
    }})