def testCheckIfLayerIsDoneWhenLayerIsDone(self):
    mock_layer = self.mox.CreateMockAnything()
    mock_layer.entity_set = self.mox.CreateMockAnything()
    mock_query = self.mox.CreateMockAnything()

    mock_layer.entity_set.filter('baked', None).AndReturn(mock_query)
    mock_query.get().AndReturn(None)
    mock_layer.put()

    self.mox.ReplayAll()
    baker._CheckIfLayerIsDone(mock_layer)
    self.assertEqual(mock_layer.baked, True)
    self.assertEqual(mock_layer.busy, False)
  def testCheckIfLayerIsDoneWhenLayerIsNotDone(self):
    self.mox.StubOutWithMock(taskqueue, 'add')
    self.mox.StubOutWithMock(baker, '_GetBakerURL')
    mock_layer = self.mox.CreateMockAnything()
    mock_layer.entity_set = self.mox.CreateMockAnything()
    mock_query = self.mox.CreateMockAnything()
    dummy_url = object()

    mock_layer.entity_set.filter('baked', None).AndReturn(mock_query)
    mock_query.get().AndReturn(object())
    baker._GetBakerURL(mock_layer).AndReturn(dummy_url)
    taskqueue.add(url=dummy_url, params={'stage': 'monitor'},
                  countdown=settings.BAKER_MONITOR_DELAY)

    self.mox.ReplayAll()
    baker._CheckIfLayerIsDone(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)