コード例 #1
0
  def testCreateEntityAndGeometry(self):
    layer = model.Layer(name='a', world='earth')
    layer_id = layer.put().id()
    schema = model.Schema(layer=layer, name='abc')
    schema.put()
    template = model.Template(schema=schema, name='def', text='', parent=schema)
    template_id = template.put().id()
    fields = {'name': 'ghi', 'view_location': db.GeoPt(1, 2),
              'template': template, 'field_x': 'y'}
    geometries = [
        {'type': model.Point, 'fields': {'location': db.GeoPt(3, 4)}},
        {'type': model.LineString, 'fields': {
            'points': [db.GeoPt(5, 6), db.GeoPt(7, 8)]
        }}
    ]

    entity_id = entity._CreateEntityAndGeometry(layer, fields, geometries)
    result = model.Entity.get_by_id(entity_id)
    self.assertEqual(result.layer.key().id(), layer_id)
    self.assertEqual(result.name, 'ghi')
    self.assertEqual(result.view_location, db.GeoPt(1, 2))
    self.assertEqual(result.template.key().id(), template_id)
    self.assertEqual(result.field_x, 'y')
    self.assertEqual(len(result.geometries), 2)
    point = model.Geometry.get_by_id(result.geometries[0], parent=result)
    line_string = model.Geometry.get_by_id(result.geometries[1], parent=result)
    self.assertEqual(point.location, db.GeoPt(3, 4))
    self.assertEqual(line_string.points, [db.GeoPt(5, 6), db.GeoPt(7, 8)])
    self.assertEqual(result.location, db.GeoPt(3, 4))
コード例 #2
0
  def testCreate(self):
    self.mox.StubOutWithMock(entity, '_ValidateEntityArguments')
    self.mox.StubOutWithMock(entity, '_CreateEntityAndGeometry')
    self.mox.StubOutWithMock(model, 'Entity', use_mock_anything=True)
    handler = entity.EntityHandler()
    request = {'a': 'b', 'c': 'd', 'e': 'f'}
    handler.request = self.mox.CreateMockAnything()
    handler.request.get = request.get
    handler.request.arguments = request.keys
    handler.response = self.mox.CreateMockAnything()
    handler.response.out = self.mox.CreateMockAnything()
    mock_entity = self.mox.CreateMockAnything()
    mock_layer = self.mox.CreateMockAnything()
    dummy_fields = object()
    dummy_geometries = object()
    dummy_id = object()

    # Success.
    entity._ValidateEntityArguments(mock_layer, request, False).AndReturn((
        dummy_fields, dummy_geometries))
    mock_layer.ClearCache()
    entity._CreateEntityAndGeometry(
        mock_layer, dummy_fields, dummy_geometries).AndReturn(dummy_id)
    handler.response.out.write(dummy_id)
    model.Entity.get_by_id(dummy_id).AndReturn(mock_entity)
    mock_entity.GenerateKML()

    # Failure during validation.
    entity._ValidateEntityArguments(mock_layer, request, False).AndRaise(
        util.BadRequest)

    # Failure during creation.
    entity._ValidateEntityArguments(mock_layer, request, False).AndReturn((
        dummy_fields, dummy_geometries))
    mock_layer.ClearCache()
    entity._CreateEntityAndGeometry(
        mock_layer, dummy_fields, dummy_geometries).AndRaise(util.BadRequest)

    self.mox.ReplayAll()
    handler.Create(mock_layer)
    self.assertRaises(util.BadRequest, handler.Create, mock_layer)
    self.assertRaises(util.BadRequest, handler.Create, mock_layer)