-
Notifications
You must be signed in to change notification settings - Fork 23
Make property name unique #153
Conversation
| response = self.client.post('/api/lease', json=self.valid_payload(create_tenant().id, create_property().id), headers=pm_header) | ||
| def test_pm_is_authorized_to_create(self, pm_header, create_tenant): | ||
| tenant = create_tenant() | ||
| response = self.client.post('/api/lease', json=self.valid_payload(tenant.id, tenant.propertyID), headers=pm_header) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tenant.propertyID
Technically, tenant's don't always have a property id. If this is a brand new lease then the tenant would not have a relationship with the property. Is there a reason why this is being changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it would've been more accurate for the existing test to do create_tenant(propertyID=None)
or
We can create a new fixture called create_unhoused_tenant()
I'm actually not a fan of having propertyID on the tenant model, but that's an issue for another day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here stems from the fact that firing off both of these functions leads to two properties with the same name being added. This happens because create_tenant() creates a property, and create_property() creates a property with the same name!
Like you said, maybe creating a tenant shouldn't create a property? I've changed things a bit so that we can do create_tenant(propertyID=None), but the request is now failing with code 400. I'm unsure what valid_payload() should do when it gets a None value for property id -- is that a problem? I'm totally unsure of what that request ends up looking like.
Also, I'm still understanding the database model here, but shouldn't all leases involve a tenant id and a property id? How could we add a lease without a property id? Just from the name, I assumed that leases were what bound tenants to properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I understand the reason for these changes now. Because in the original tests they were creating two properties. I should've read your commit comments! Sorry I'm not used to looking, because I don't usually see that. Thanks for adding those. Next time I will try to be better.
The problem here stems from the fact that firing off both of these functions leads to two properties with the same name being added
This is easily fixed. I'm actually going to be creating issues to implement faker on all of the factories. This will allow factories to be created with dynamic data. You can see an implementation of this in the User factory in #154 specifically on the email field.
Like you said, maybe creating a tenant shouldn't create a property? I've changed things a bit so that we can do create_tenant(propertyID=None), but the request is now failing with code 400. I'm unsure what valid_payload() should do when it gets a None value for property id -- is that a problem? I'm totally unsure of what that request ends up looking like.
Yeah, The factory is not setup to accept None. That could be tweaked. It could look something like this:
def create_tenant():
def _create_tenant(property=None):
tenant = TenantModel(
firstName="firstName",
lastName="lastName",
phone="phone",
propertyID=property.id if property else None,
staffIDs=[],
unitNum=3
)
tenant.save_to_db()
return tenant
yield _create_tenant
And then (unfortunately) the lease factory and then some of the tests would need to be updated. I would opt for creating an unhoused tenant factory and use the faker library in both the new and existing factory. The 400 is being returned because there is probably a validation error, hence the valid_payload being sent is not valid.
The request being sent is the valid payload. so it would like something like this:
{
'dateTimeStart': '11/25/2020 17:14:01',
'dateTimeEnd': '11/25/2021 17:14:20',
'tenantID': 1,
'propertyID': 1
}
Also, I'm still understanding the database model here, but shouldn't all leases involve a tenant id and a property id? How could we add a lease without a property id? Just from the name, I assumed that leases were what bound tenants to properties.
Yes you are absolutely correct here. That is why I'm NOT a fan of having a propertyID on the tenant model, because that creates a situation where we need to always update two columns in two separate tables. One propertyID on the lease, and one propertyID on the tenant, and they should always be the same. Leaving it this way could eventually cause our database to have erroneous data. So yes. You are absolutely correct and I agree with you here. The lease should bound the tenant to the property. If you would like to create an issue for this that would be outstanding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also fine with removing the propertyID on the tenant model in this PR too. If that is something you would like to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Strangely, stuff started working after I just rebased. Tenant model is still exactly the same. Let's get this merged and do the other stuff in other PRs. To be clear, the other change we're talking about is removing propertyID on tenant model? Is there also a change to the lease model?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: Created issue codeforpdx/dwellingly-app/issues/455 to address the double relationship issue. Thanks for bringing it up again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear, the other change we're talking about is removing propertyID on tenant model? Is there also a change to the lease model?
I think there was something else. I will need to jog my memory and I will get back to you. Thanks for getting this rebased. I will get it merged soon. Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@htharker42 recently updated the lease model. So I think we're all set there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue codeforpdx/dwellingly-app/issues/459 created to get Faker in use on all the factories. I was going to create an issue to update these tests afterwards, but I think these tests will actually end up being deleted in the future, because the lease endpoint is not being utilized by the FE UI.
|
Haha, that was YOUR catch, Nick :) you messaged me about it a while ago,
but the call you gave me isn't exactly right. I have to search through the
SQL alchemy documentation (I think?) to find the right one .
…On Thu, Nov 19, 2020, 1:16 AM Nick Schimek ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In models/property.py
<#153 (comment)>
:
> @@ -60,6 +60,7 @@ def json(self):
@classmethod
def find_by_name(cls, name):
+ # TODO Can we use a different call here, now that name is unique?
YES! I would prefer we do. Good catch.
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#153 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJFZANTNCHBGOA7LFVLZ6TSQTO6NANCNFSM4T2ZTZ4Q>
.
|
I read sql-alchemy docs and couldn't find anything. It seems this is the recommended way to do it. So no changes necessary. https://flask-sqlalchemy.palletsprojects.com/en/2.x/queries/#querying-records - apparently the select statement isn't fired until |
Originally, create_property was called multiple times; in the initialization of the test fixture by pytest (I guess this happens before the test is run), when create_tenant() was called, and then finally, once create_property() itself was called. Here we ensure it gets called only once.
This reverts commit 7aba65b.
c443ef0 to
e48303f
Compare
|
Just rebased, checks are passing now. |
NickSchimek
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for knocking this out @gussmith23 - Excellent work 💫
What issue is this solving?
Closes codeforpdx/dwellingly-app/issues/395. Makes the "name" field of a Property unique. Changes tests and seed data to fit this new requirement.
Any helpful knowledge/context for the reviewer?
Is a re-seeding of the database necessary? YES
Any new dependencies to install? ❌
Any special requirements to test? ❌
Please make sure you've attempted to meet the following coding standards