Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite application Tests using Factory-Boy #116

Merged
merged 13 commits into from
Apr 6, 2020
Merged

Rewrite application Tests using Factory-Boy #116

merged 13 commits into from
Apr 6, 2020

Conversation

chris48s
Copy link
Contributor

@chris48s chris48s commented Mar 25, 2020

Closes #48

Done so far:

  • Rewritten the tests in the userauth app to use the UserFactory (the user app tests generated by cookiecutter already had a UserFactory class) instead of fixtures
  • Written a CustomTagFactory class and sketched out what the TaggedItemsFactory needs to look like

The remaining tasks are:

  • Write a ResourceFactory class and finish off TaggedItemsFactory
  • Rewrite the tests for the resources app to use the tag/resource factories instead of fixtures.

We might also look at generating init data (#115) in this PR, or we could leave it for another PR.

@BethanyG - I'll mark up the diff with some notes explaining what I've done so far a bit more. I'll leave it with you to have a look at this PR, work on the ResourceFactory and resources tests and push some more commits to this branch.

We'll use our factories to seed the DB
with fake data as well as testing
lets declare our factories in the app dir
instead of under /tests
Copy link
Contributor Author

@chris48s chris48s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't leave a comment inine for this becuase there's no diff for /users/factories.py, but we don't need to define factory fields on fields that already have a default. Our class

class UserFactory(DjangoModelFactory):
    username = Faker("user_name")
    email = Faker("email")
    name = Faker("name")

    @post_generation
    def password(self, create: bool, extracted: Sequence[Any], **kwargs):
        # bla

is functionally the same as

class UserFactory(DjangoModelFactory):
    username = Faker("user_name")
    email = Faker("email")
    name = Faker("name")

    @post_generation
    def password(self, create: bool, extracted: Sequence[Any], **kwargs):
        # bla

    is_staff = False
    is_active = True
    date_joined = datetime.datetime.now()
    last_login = None

because of the model defaults on is_staff, etc. We can still explicitly overide them if we need to create an admin user, or fake a user who has already logged in, or whatever..

project/userauth/tests.py Show resolved Hide resolved
project/tagging/factories.py Show resolved Hide resolved
project/tagging/factories.py Outdated Show resolved Hide resolved
project/tagging/factories.py Show resolved Hide resolved
@mayela
Copy link
Contributor

mayela commented Mar 27, 2020

Hi @chris48s and @BethanyG. I'm Mar. I added the ResourceFactory, hope you can review it. Thanks in advance

Copy link
Contributor Author

@chris48s chris48s 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 working on this. I've left a couple of comments inline

project/resources/factories.py Outdated Show resolved Hide resolved
project/resources/factories.py Outdated Show resolved Hide resolved
project/resources/factories.py Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Mar 30, 2020

Codecov Report

Merging #116 into master will decrease coverage by 2.86%.
The diff coverage is 51.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #116      +/-   ##
==========================================
- Coverage   78.88%   76.02%   -2.87%     
==========================================
  Files          25       28       +3     
  Lines         341      392      +51     
==========================================
+ Hits          269      298      +29     
- Misses         72       94      +22     
Impacted Files Coverage Δ
project/tagging/factories.py 0.00% <0.00%> (ø)
project/users/factories.py 100.00% <ø> (ø)
project/resources/factories.py 100.00% <100.00%> (ø)
project/tagging/serializers.py 83.33% <0.00%> (-8.34%) ⬇️
project/resources/views.py 68.18% <0.00%> (-4.55%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 22b14e3...8cbfdac. Read the comment docs.

@mayela
Copy link
Contributor

mayela commented Mar 30, 2020

Thanks for your comments @chris48s. Today I'm going to review each one and resolve it. Nice day! 😊

@chris48s
Copy link
Contributor Author

chris48s commented Apr 2, 2020

There's one remaining task to finish off this PR, which is to rewrite the resources tests to use the ResourceFactory added by @mayela . Are you still planning to pick this up @BethanyG ?

@BethanyG
Copy link
Member

BethanyG commented Apr 2, 2020

@chris48s - I am. I have just been snowed under. Should be able to get to it before the weekend.

@chris48s
Copy link
Contributor Author

chris48s commented Apr 2, 2020

No pressure :)

@mayela
Copy link
Contributor

mayela commented Apr 2, 2020

@BethanyG I want to help with tests! How we can work together?

@mayela mayela closed this Apr 2, 2020
@chris48s chris48s reopened this Apr 3, 2020
@mayela
Copy link
Contributor

mayela commented Apr 3, 2020

@chris48s Sorry, I didn't notice I closed the PR! 😓

@BethanyG
Copy link
Member

BethanyG commented Apr 5, 2020

@mayela - We could always use more tests and more testing coverage!! I'll open an issue, and we can go from there.... also -- you can always open issues here in the repo for things you think we need to put in place, and if it's work you want to work on..just assign the issue to yourself. We can always discuss details/use cases as we go. 😄

I went ahead and re-wrote the existing (and sorta pathetic) resources tests so we can get this PR closed...but we can (and need to) add more tests and scenarios around user auth, resources, tagging, and of course, integration tests. You can also touch base with @chris48s and see if there are things on his mind you could help out with as well.

@BethanyG BethanyG requested a review from lpatmo April 5, 2020 01:59
@BethanyG
Copy link
Member

BethanyG commented Apr 5, 2020

@lpatmo - adding you here as a reviewer because I've just done a push..so need additional eyes on this. @chris48s - I cannot seem to add you as a reviewer, or I would have. 😉 .

@lpatmo
Copy link
Member

lpatmo commented Apr 5, 2020

Thanks for tagging me; I will take a look!

Copy link
Member

@lpatmo lpatmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much, @chris48s -- I admittedly had to look up https://factoryboy.readthedocs.io/en/latest/, but this LGTM!

Props to @BethanyG for filing #125.

Copy link
Contributor Author

@chris48s chris48s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly there on this one - team effort :)

It looks like your build is failing with AssertionError: '' != 'WEB'

Is that due to #125 ?
If so and you don't want to address the bug in this PR, how about we comment the assertion or use the same technique as I did in 49eca65 and keep the test but mark it as a skip() or expectedFailure() for now?

response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['description'], data['description'])
self.assertEqual(response.data['tags'][0]["name"], data['tags'][0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to the assertions about tags here? (not necessarily wrong to bin them - its just not explained anywhere, I don't think)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. That's due to #125 😄 . I didn't even think to do a skip -- I'll add that in, I like how you did it with the issue ref in 49eca65 .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for binning the tags..I was solving an issue with the ordering being different coming back in data .. and then decided I didn't want to. But I should do at least one test case with tags, so I can add it back in.

url = '/api/v1/resources/96eec6e2-59f4-11ea-9149-dca9047779fe/'
records = create_batch(ResourceFactory, 10)
new_resource = create(ResourceFactory, guid='96f4dd16-59f4-11ea-9149-dca9047779fe')
url = f'/api/v1/resources/{new_resource.guid}/'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you're using new_resource.guid, we don't actually need to hard-code the guid. This could just be:

new_resource = create(ResourceFactory)
url = f'/api/v1/resources/{new_resource.guid}/'

Also, same comment for test_view_one_resource()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Dunno what I was thinking there...I did that elsewhere, but brain farted here. Fixed in the new push. Thank for catching!

@chris48s
Copy link
Contributor Author

chris48s commented Apr 5, 2020

I cannot seem to add you as a reviewer,

I think its because I opened the PR: I can't approve my own PR (even though 3 of us have now pushed to the branch)

@chris48s chris48s changed the title WIP: Rewrite application Tests using Factory-Boy Rewrite application Tests using Factory-Boy Apr 5, 2020
@chris48s chris48s marked this pull request as ready for review April 5, 2020 13:59
…ses, added tag test back into patch_one_resource.
@BethanyG
Copy link
Member

BethanyG commented Apr 6, 2020

@chris48s - do you wanna have one last look and then push the big, green button? Thank you so much for your patience and work on this!. 🌟

@chris48s
Copy link
Contributor Author

chris48s commented Apr 6, 2020

LGTM 👍
We can move on to #115 now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Testing]Rewrite Application Tests to Use Factory-Boy
4 participants