Skip to content

Refactoring and testing DeltaBot #42

@chrisuehlinger

Description

@chrisuehlinger

Hey! So I've been talking with PixelOrange about making this project easier to test. Given that many people are coming and going on this project, I think testing would be a great way to make sure new contributors don't accidentally step on old code or break something that was written a long time ago.

If you are currently working on adding a feature, that's fine, but try to follow these principles as much as possible:

-Write pure functions: If given the same input two times, the function should give the same output both times.
-If you have to make calls to praw, do them higher up and then pass the results into your new function.
-Don't log inside of your functions: rather, return a variable containing what you would have logged, along with the result of the function. (this is less important)

Here's a list of the pure and impure functions with their side effects.

Pure:
get_first_int
flair_sorter
skippable_line
str_contains_token
markdown_to_scoreboard
scoreboard_to_markdown
string_matches_message
is_comment_too_short
scan_mod_mail

Impure:
write_saved_id(uses logging and file I/O)
read_saved_id(uses logging and file I/O)
init(logging, praw calls)
send_first_time_message(praw calls)
get_message(random numbers)
award_points(logging, calls other impure functions)
get_this_months_scoreboard(praw calls)
update_monthly_scoreboard(logging, praw calls)
adjust_point_flair(praw calls)
already_replied(praw calls)
is_parent_commenter_author(praw calls)
points_already_awarded_to_ancestor(praw calls)
scan_comment(logging, praw calls)
scan_comments(logging, praw calls, calls to impure functions)
command_add(praw calls)
is_moderator(praw calls)
scan_message(logging, praw calls, system calls)
rescan_comment(praw calls)
rescan_comments(praw calls, calls to impure functions)
scan_comment_reply(logging, praw calls, calls to impure functions)
scan_inbox(logging, praw calls, calls to impure functions)
update_scoreboard(logging, praw calls, time sensitive)
get_top_ten_scores(praw calls)
get_top_ten_scores_this_month(praw calls, time sensitive)
update_wiki_tracker(logging, praw calls)
update_wiki_tracker(logging, praw calls, calls to impure functions)
go(logging, praw calls, calls to impure functions)

We don't need to purify every function, but if we can move all the side effects up to go(), init() or other high-up functions, it will make everything else MUCH easier to test.

Ideally, I'd like to pass scan_comments() a list of comments and other data, and get back a list of output actions that need to be performed. In the meantime, if someone could get a unit testing library set up to test the pure functions, that'd be awesome.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions