Repository navigation
Add initial set of tests for composite primary and foreign keys - #138
tillspeicher wants to merge 1 commit into
Conversation
| database = database | ||
|
|
||
| id: int = ormar.Integer(primary_key=True) | ||
| assigned_by: Optional[User] = ormar.ForeignKey(User) |
There was a problem hiding this comment.
Right now you cannot declare relationship in Through model -> that's one of the things I wanted to add gradually, but for now ormar creates the fields during expanding_reverse_relations, relations are not copied in inheritance, and explicitly stating any relation on through Model will raise exception.
The key is that you can have multiple relations between two models, and ormar wouldn't know if the relation you created is the one that should be used for m2m relation or it should create other field for this relation (relation field name would have to be passed to ManyToMany probably).
|
Ok, in general the tests look good, as you said those test are responsible for checks regarding Model declaration, especially getting/ setting pks. Since pk is such a crucial thing that is used almost everywhere (among others: order by, joins, filters, relations etc.), tests for this have to pretty much cover everything. Some of the following tests have common parts, but for clarity I listed what should be tested. That includes, but is not limited to:
Maybe something else will come to my mind. As you can see there is a vast amount of areas that needs to be checked to ensure proper working of |
|
Thanks for taking the time to go over the tests and creating the list of requirements! Mypy on the tests is producing errors for now, but that's expected since e.g. |
|
No, the lines length should be 88 if I remember correctly (that's also my pycharm and flake8 setting). Yeah, that's the problem with pks -> they are used all over the place -> relations need to be resolved to them in joins, it's a default sort order if you do not provide additional one, they cannot be excluded in fields/exclude_fields etc. that's why a lot of tests are needed with this functionality to properly check all aspects on all db backends. I'm ok with changing some of the tests (or preferably adding new models), but not all since then you loose tests for simple one column pks, and like they say never too much tests. I will help you with tests as that may be also an opportunity to clean them up a little, maybe divide into subpackages as it's starting to be a lot of files to grasp. |
|
@tillspeicher are you gonna continue this work or should I close this PR? |
|
I'm sorry for going silent on the PR. |
I started this PR for the composite PK and FK implementation, please don't merge it yet.
So far, I only added a set of tests that covers the interfaces and behavior that we discussed in #137 . The tests likely contain errors, because there is no implementation of the actual functionality yet, so I couldn't run the code.
There are probably also some missing corner cases that should be tested as well.
For now, this is meant primarily as a specification for the composite PK and FK feature and to document the requirements.
In particular, the tests demonstrate and check composite PK- and FK-based CRUD operations, preventing direct access to FK columns, checking that reverse and many-to-many relationships are created correctly, that composite FKs are compatible with
pydanticand that thepkproperty works as discussed.In addition to what we discussed before, I added a test to ensure that composite PKs cannot use FK columns directly, as that would expose them via the
pkproperty. So they need to use the names of the FK relations instead.Please let me know if you would like anything to be changed or added @collerek . Otherwise I'll go ahead with the implementation.