Delete fields from schema, what is the right way to do this in a test?

I have a contenttype and hundreds of objects of this type. Now i must refactor this type (delete some fields). I can delete the field definitions in the code base and clean the objects in an upgrade step. But how can i test this scenario?

I found a piece of code in plone.restapi, but i don't know where this adapter is definied, for better understanding what happen.

def delete_field(context, request, name):
    field = context.publishTraverse(request, name)
    delete = queryMultiAdapter((field, request), name="delete")
    delete()

Here is my Test snippet to construct the scenario:

class IBaseDummySchema(model.Schema):
  prop1 = schema.TextLine(
    title="Prop 1",
    default=None
  )
  prop2 = schema.TextLine(
    title="Prop 2",
    default=None
  )

class IDummyTypeSchema(IBaseDummySchema):
  pass

class TestInterfaceUpgrade(BaseFunctionalTest):
    
  def setUp(self):
    import transaction
    
    self.app = self.layer["app"]
    self.portal = self.layer["portal"]
    setRoles(self.portal, TEST_USER_ID, ["Manager"])
        
    fti = DexterityFTI("dummytype")
    self.portal.portal_types._setObject("dummytype", fti)
    fti.klass = "plone.dexterity.content.Item"
    fti.schema = "my.addon.tests.test_interface_update.IDummyTypeSchema"
    fti.global_allow = True

    self.portal.portal_types['Plone Site'].allowed_content_types = ("Dummytype",)
    
    self.portal.invokeFactory(
      "dummytype",
      id="dummy-object",
      title="Dummy Object",
    )
    
    transaction.commit()

  def test_remove_field_from_schema(self):
    # field = self.portal.object1.publishTraverse(self.request, "prop2")
    # delete = queryMultiAdapter((field, self.request), name="delete")
    breakpoint()
    
    

You want to test that the upgrade step works? You can do that manually, or creating an object, running the upgrade step and checking that, indeed, the fields are gone :smiley:

Do I miss the point?

the schema definition in the interface is changed, i have no old contenttype objects in the tests. if test machinery starts, it reads the type info with the actually schema definition

fti.schema = "my.addon.tests.test_interface_update.IDummyTypeSchema"

what i can do is:

before upgrade step, i can create content with this schema :
fti.schema = "my.addon.tests.test_interface_update.IDummyTypeSchemaOld"

in the upgrade step, i can change the schema to:
fti.schema = "my.addon.tests.test_interface_update.IDummyTypeSchemaNew"

and then i can test the "deleted" fields and props on the contentobject.

but this is not the same like remove fields from schema interface definition, right?

Actually, as objects are python anyway, you can assign any attribute to an object:

my_obj = api.portal.get().restrictedTraverse('path/to/my-object')
my_obj.foo = 'bar'

So creating the objects, manually assigning the already deleted fields from the interface, running the upgrade step and checking that getattr(my_obj, 'foo', None) is None would be enough IMHO.

Thanks for hint. Yes, this is the cleaner solution.