These are chat archives for praw-dev/praw

22nd
Mar 2018
bakonydraco
@bakonydraco
Mar 22 04:46
interesting
ah okay i see where those live, and jarhill0 was telling me about the cassettes
bakonydraco
@bakonydraco
Mar 22 22:35
@jarhill0 I get how betamax works for the most part now
can i set it to run and then just navigate from my browser, and record that?
or do i need to hard code the navigation from python
bakonydraco
@bakonydraco
Mar 22 22:46
or, more generally, how do i use oauth with betamax? it's not super clear from the betamax docs
ah wait i think i have it, it's through requests
You will need to be able to run your code locally. None of this involves your browser.
You will need a Reddit account with a script-type app set up for authentication.
Can you run your code locally?
bakonydraco
@bakonydraco
Mar 22 22:53
i can
is there an example of the code used to generate the cassette?
i can write it myself, but would be nice if i could borrow haha
Joe RH
@jarhill0
Mar 22 22:59

All that is needed to generate the cassette is

  1. A block of code that accesses the network inside a with statement along the lines of with self.recorder.use_cassette('TestClassName.test_method_name'):
  2. If you're going to do anything other than "read" from Reddit, set self.reddit.read_only = False
  3. Run the test locally with authentication (see link above)

As an example, this is a test that I wrote (the three important lines highlighted): https://github.com/praw-dev/praw/blob/36f32102b395e43a4a9da1d1c461bf4696f10686/tests/integration/models/test_preferences.py#L17-L19

The first line sets self.reddit.read_only = False because the action the test performs requires reading account-specific information. The second line tells Betamax to use the cassette, and the third line, inside the with block, makes requests to the network.

Betamax takes care of generating and using the cassettes automatically with the with block, which means that you don't have to do anything special to record a cassette other than running the test suite.
Does that help?
bakonydraco
@bakonydraco
Mar 22 23:00
whoa haha
it does
wait where are the oauth credentials set
ah wait no, this is to read the cassette, not ot write it
Joe RH
@jarhill0
Mar 22 23:01
It's both
bakonydraco
@bakonydraco
Mar 22 23:01
:O
so where does the authentication happen?
Joe RH
@jarhill0
Mar 22 23:03

When recording, instead of running tests as python setup.py test, run as

prawtest_client_id=whatever prawtest_client_secret=something prawtest_password=yourpassword prawtest_username=yourusername python setup.py test

(that's all one line)

The docs suggest an alternate method, which is to do

export prawtest_client_id=myclientid
export prawtest_client_secret=myclientsecret
export prawtest_password=mypassword
export prawtest_test_subreddit=reddit_api_test
export prawtest_username=myusername
export prawtest_user_agent=praw_pytest

And then just run as python setup.py test

They're both equally valid
bakonydraco
@bakonydraco
Mar 22 23:04
ahhhhh i get it
i was trying to write my own cassette generator
Joe RH
@jarhill0
Mar 22 23:06
Daring! :P
bakonydraco
@bakonydraco
Mar 22 23:07
oh this is awesome
sweet all the tests passed
but still not 100% coverage
so if i do something like
    with self.recorder.use_cassette('TestSubredditEmoji.iter'):
it'll create TestSubredditEmoji.iter
if not exists?
Joe RH
@jarhill0
Mar 22 23:09
yep
though you should name the cassette to match the pattern that the other cassettes are named in
That is [class name of test].[method name of test]
bakonydraco
@bakonydraco
Mar 22 23:12
      for emoji_name, emoji_data in subreddit.emoji:
tests/integration/models/reddit/test_subreddit.py:1207:
E prawcore.exceptions.NotFound: received 404 HTTP response
Joe RH
@jarhill0
Mar 22 23:16
It looks like you got a 404 error, which doesn't have anything to do with Betamax.
Are you calling the method correctly?
bakonydraco
@bakonydraco
Mar 22 23:17
probably not :D
I think the issue is the subreddit is set to
placeholder_test_subreddit
for which the test account i created is not a mod
Joe RH
@jarhill0
Mar 22 23:17
let me see
bakonydraco
@bakonydraco
Mar 22 23:17
ah which you told me about
i can fix that haha
Joe RH
@jarhill0
Mar 22 23:18
unfortunately I can't see which test you're trying to run since you haven't pushed that code, but if you need help let me know.
Also by the way @bakonydraco , you'll have to delete the newly created cassette before running tests again
So that it can be re-recorded
bakonydraco
@bakonydraco
Mar 22 23:18
oh interesting
does it record if there's an error?
here's my test
class TestSubredditEmoji(IntegrationTest):
@mock.patch('time.sleep', returnvalue=None)
def test__iter(self,
):
self.reddit.read_only = False
subreddit = self.reddit.subreddit(
pytest.placeholders.test_subreddit)
with self.recorder.use_cassette('TestSubredditEmoji.iter'):
count = 0
for emoji_name, emoji_data in subreddit.emoji:
assert isinstance(emoji, Emoji)
count += 1
assert count > 0
Joe RH
@jarhill0
Mar 22 23:19
Yep, it records even if there's an error.
bakonydraco
@bakonydraco
Mar 22 23:19
ah so if not exists: record
else: use existing?
Joe RH
@jarhill0
Mar 22 23:19
yep
bakonydraco
@bakonydraco
Mar 22 23:20
woo! that fixed the 404
now another bug :)
Joe RH
@jarhill0
Mar 22 23:20
For your test: for emoji_name, emoji_data in subreddit.emoji should error out because subreddit.emoji yields one item at a time
you probably meant for emoji in subreddit.emoji:
Making progress though!
bakonydraco
@bakonydraco
Mar 22 23:21
yes, whoops!
haha sorry for being dense on this
Joe RH
@jarhill0
Mar 22 23:21
No worries, I'm happy to help
I struggled with this stuff too and bboe helped me through it
bakonydraco
@bakonydraco
Mar 22 23:21
fixed that but now:
  response = self.subreddit.fullname._reddit.get(
        API_PATH['emoji_list'].format(subreddit=self.subreddit))
E AttributeError: 'str' object has no attribute '_reddit'
oh wait you mentioned that :D :D :D
Joe RH
@jarhill0
Mar 22 23:22
Yep!
bakonydraco
@bakonydraco
Mar 22 23:25
woo fixed that bug!
          assert isinstance(emoji, Emoji)
E NameError: name 'Emoji' is not defined
Joe RH
@jarhill0
Mar 22 23:25
gotta import
:)
bakonydraco
@bakonydraco
Mar 22 23:25
got it :D
it passed!!!!!
Joe RH
@jarhill0
Mar 22 23:26
Yay!
bakonydraco
@bakonydraco
Mar 22 23:26
so now i upload the cassette to github and commit the test change?
Joe RH
@jarhill0
Mar 22 23:27
If you have git installed locally, it's probably easier to commit and push that way. But you can upload the cassette and commit on GH web also if you want.
bakonydraco
@bakonydraco
Mar 22 23:27
:thumbsup:
Joe RH
@jarhill0
Mar 22 23:28
beautiful cassette!
bakonydraco
@bakonydraco
Mar 22 23:29
with any luck, there's 100% coverage on subreddit now
so if that passes travis/coveralls i'll add it for emoji
and then I think it all works!
Joe RH
@jarhill0
Mar 22 23:30
Good work!
bakonydraco
@bakonydraco
Mar 22 23:32
hmmm
the test i added didn't actually add the coverage i expected...
praw-dev/praw@5ba288d
but i iterated through it?
Joe RH
@jarhill0
Mar 22 23:34
Are you looking at the right coveralls build?
bakonydraco
@bakonydraco
Mar 22 23:35
should i just try to commit something again to force a refresh?
Joe RH
@jarhill0
Mar 22 23:35
bakonydraco
@bakonydraco
Mar 22 23:36
oh whoa
sweet!
so i did it?
also what's going on with const
Joe RH
@jarhill0
Mar 22 23:36
You covered subreddit, nice work! Next you have to cover emoji
Don't worry about const — it has some weird stuff going on due to python 2/3 compatibility
bakonydraco
@bakonydraco
Mar 22 23:37
ah got it
saw your comment about test name
is .json still supposed to be at the end?
Joe RH
@jarhill0
Mar 22 23:39
yep
I have to go now, but I'll be back in an hour or two. Good work so far!
bakonydraco
@bakonydraco
Mar 22 23:40
done, thanks!