rcxwhiz / django-hierarchical-models

MIT License
1 stars 0 forks source link

Create HierarchicalModel interface & determine structure #1

Closed rcxwhiz closed 6 months ago

rcxwhiz commented 6 months ago

Because I am creating several implementations of hierarchical models which I assume will have equal behavior, I need to make an interface for them. The interface should define user facing methods that will be called rather than whatever fields are used to implement the behavior in the models.

Part of this task is to double check the structure that should be used to deliver these models. I had originally thought that mixins might be most appropriate, but since these will need to subclass the django models.Model class, it might be best to just have them subclass that with class Meta: abstract = True.

rcxwhiz commented 6 months ago

Got the interface created and tests working, now just need to fill it out.

rcxwhiz commented 6 months ago

There are a couple things that still need to get sorted out.

The first is the testing structure. Since these models are all implementing the same interface, it would make sense to test them based on that interface. Right now I have that general test class with a model_class class member. The issue is that I cannot have HierarchicalModelTestInterface subclass TestCase because then it will be picked up itself as a test. This leads to a lot of type hint errors because I am calling TestCase methods from within HierarchicalModelTestInterface, which does not subclass TestCase directly. I have the concrete test classes subclass both of them. I would like for there to be a solution to this where I can write an interface test that makes more sense, is less hacky, and doesn't have type hint issues.

The next issue with the testing is not as big a deal. I need to make a concrete model class with each hierarchical model type to be used during testing. I have been doing just a person model with first name last name fields. It would make sense for this to be a standard mixin I use with the HierarchicalModelABC implementations, but when I tried that it wasn't happy. It seems like the fields need to be written in a class that subclasses models.Model, but I already have the HierarchicalModelABC subclassing models.Model. I don't think it's a good idea to have the person model mixin and the hierarchical model both subclass models.Model, but what do I know? If I can't get this figured out all it means is that I have to rewrite the fields each time, but if I could get the type hints working it would actually also be nice to be able to type hint the model class as something subclassing HierarchicalModelABC and the person model mixin. That would mean the interface test class would have type hint support for all relevant fields.

As far as adding features goes, I need to figure out how to return the children. I think I am going to create a result class like so:

class Child:
    instance: T
    children: list[Child[T]]

I'm not sure how the typing or implementation or so on is going to work out, but that is the basic idea. I also realized I probably not only want to allow the finding children to go until a certain depth, but I should also probably allow some cut off for total number children found. There's also the issue of ordering the siblings in the results, but oh well.

rcxwhiz commented 6 months ago

Got the PersonModelMixin moved out with the fields used for testing. Now just the first and third issues are remining.

rcxwhiz commented 6 months ago

First problem solved. Now just working on the third one.

rcxwhiz commented 6 months ago

Put in the children finding interface, with a default implementation for classes to potentially override.

rcxwhiz commented 6 months ago

Since I got the overall structure of this stuff worked out, I am going to PR and close this issue. The last (glaring) thing that needs to be done with this is a more robust test suite, but I think that belongs in the testing issue.