def testPrepareLayerForBakingSuccess(self):
    self.mox.StubOutWithMock(taskqueue, 'add')
    self.mox.StubOutWithMock(baker, '_GetBakerURL')
    mock_divisions = [self.mox.CreateMock(model.Division) for _ in xrange(2)]
    mock_division_query = self.mox.CreateMockAnything()
    mock_entity_query = self.mox.CreateMockAnything()
    mock_entities = [self.mox.CreateMock(model.Entity) for _ in xrange(2)]
    mock_entities[0].baked = True
    mock_entities[1].baked = True
    mock_layer = self.mox.CreateMockAnything()
    mock_layer.division_set = mock_division_query
    mock_layer.entity_set = mock_entity_query
    dummy_url = object()
    dummy_division_cursor = object()
    dummy_entity_cursor = object()

    mock_division_query.fetch(1000).AndReturn(mock_divisions[:1])
    mock_divisions[0].delete()
    mock_division_query.cursor().AndReturn(dummy_division_cursor)
    mock_division_query.with_cursor(dummy_division_cursor).AndReturn(
        mock_division_query)
    mock_division_query.fetch(1000).AndReturn(mock_divisions[1:])
    mock_divisions[1].delete()
    mock_division_query.cursor().AndReturn(dummy_division_cursor)
    mock_division_query.with_cursor(dummy_division_cursor).AndReturn(
        mock_division_query)
    mock_division_query.fetch(1000).AndReturn([])

    mock_entity_query.filter('baked', True).AndReturn(mock_entity_query)
    mock_entity_query.fetch(1000).AndReturn(mock_entities)
    mock_entities[0].put()
    mock_entities[1].put()
    mock_entity_query.cursor().AndReturn(dummy_entity_cursor)
    mock_entity_query.with_cursor(dummy_entity_cursor).AndReturn(
        mock_entity_query)
    mock_entity_query.fetch(1000).AndReturn([])

    baker._GetBakerURL(mock_layer).AndReturn(dummy_url)
    baker._GetBakerURL(mock_layer).AndReturn(dummy_url)
    taskqueue.add(url=dummy_url, params={
        'stage': 'subdivide',
        'north': 90,
        'south': -90,
        'east': 180,
        'west': -180
    })
    taskqueue.add(url=dummy_url, params={'stage': 'monitor'},
                  countdown=settings.BAKER_MONITOR_DELAY)

    self.mox.ReplayAll()

    baker._PrepareLayerForBaking(mock_layer)
    self.assertEqual(mock_entities[0].baked, None)
    self.assertEqual(mock_entities[1].baked, None)
  def testPrepareLayerForBakingInterrupt(self):
    self.mox.StubOutWithMock(taskqueue, 'add')
    self.mox.StubOutWithMock(baker, '_GetBakerURL')
    mock_layer = self.mox.CreateMockAnything()
    mock_layer.division_set = self.mox.CreateMockAnything()
    dummy_url = object()

    mock_layer.division_set.fetch(1000).AndRaise(runtime.DeadlineExceededError)
    baker._GetBakerURL(mock_layer).AndReturn(dummy_url)
    taskqueue.add(url=dummy_url, params={'stage': 'setup'})

    self.mox.ReplayAll()

    baker._PrepareLayerForBaking(mock_layer)
  def testUpdate(self):
    self.mox.StubOutWithMock(baker, '_PrepareLayerForBaking')
    self.mox.StubOutWithMock(baker, '_CheckIfLayerIsDone')
    self.mox.StubOutWithMock(baker, '_Subdivide')
    self.mox.StubOutWithMock(model.Division, 'get_by_id')
    handler = baker.BakerApprentice()
    dummy_layer = object()
    dummy_parent = object()
    dummy_retry_division = object()

    baker._PrepareLayerForBaking(dummy_layer)

    baker._CheckIfLayerIsDone(dummy_layer)

    model.Division.get_by_id(123).AndReturn(dummy_parent)
    model.Division.get_by_id(456).AndReturn(dummy_retry_division)
    baker._Subdivide(dummy_layer, 1.23, 4.56, 7.89, 0.36,
                     dummy_parent, dummy_retry_division, False)

    self.mox.ReplayAll()

    handler.request = {'stage': 'invalid'}
    self.assertRaises(util.BadRequest, handler.Update, dummy_layer)

    handler.request = {'stage': 'setup'}
    handler.Update(dummy_layer)

    handler.request = {'stage': 'monitor'}
    handler.Update(dummy_layer)

    handler.request = {
        'stage': 'subdivide',
        'north': '1.23',
        'south': '4.56',
        'east': '7.89',
        'west': '0.36',
        'parent': '123',
        'retry_division': '456',
        'retry_has_children': '',
    }
    handler.Update(dummy_layer)