Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 19, 2021. It is now read-only.

Conversation

@gussmith23
Copy link
Contributor

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

  • Code builds successfully
  • Code has been tested and does not produce errors
  • Code is readable and formatted
  • There isn't any unnecessary commented-out code

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)
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@NickSchimek NickSchimek Nov 25, 2020

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.

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

@NickSchimek NickSchimek Dec 3, 2020

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

Copy link
Member

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.

Copy link
Member

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.

models/property.py Outdated Show resolved Hide resolved
@gussmith23
Copy link
Contributor Author

gussmith23 commented Nov 19, 2020 via email

@NickSchimek
Copy link
Member

NickSchimek commented Nov 20, 2020

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 .

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 first() is called; otherwise it returns a query object. I wonder if there is a way to show what sql statements are being executed in the dev environment?

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.
@gussmith23 gussmith23 force-pushed the make-property-name-unique branch from c443ef0 to e48303f Compare December 2, 2020 22:39
@gussmith23
Copy link
Contributor Author

Just rebased, checks are passing now.

Copy link
Member

@NickSchimek NickSchimek left a 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 💫

@NickSchimek NickSchimek merged commit 97dbc37 into development Dec 4, 2020
@NickSchimek NickSchimek deleted the make-property-name-unique branch December 4, 2020 06:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix routes properties/name to use an unique identifier

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.