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

refactored tests to use mocks #65

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ prev_id.txt
/.project
/.pydevproject
*.out

*.db
36 changes: 7 additions & 29 deletions config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"subreddit": "SUBREDDIT",
"subreddit": "subreddit",

"database": "./awards.db",

"account": {
"username": "USERNAME",
"password": "PASSWORD"
"username": "username",
"password": "password"
},

"test_account": {
Expand All @@ -12,27 +14,7 @@
},

"sleep_time": 60,

"private_message": "Congratulations; you've earned your first delta!\n\nAs you may already know, a delta (∆) is given when a comment has changed someone's view. For a more detailed explanation of the delta system, [see here](http://www.reddit.com/r/changemyview/wiki/deltabot).\n\n/u/DeltaBot has updated your user flair and created [your own wiki page](/r/%s/wiki/user/%s) which will be updated every time you earn a delta. If you do well, you may find yourself on our [leaderboards](http://www.reddit.com/r/changemyview/wiki/leaderboards) (the monthly one is also featured in our sidebar).\n\nGood luck, and happy CMVing!\n\n_____\n\n*[^I ^am ^a ^bot](https://github.com/alexames/DeltaBot)^, ^and ^this ^action ^was ^performed ^automatically. ^Please [^contact ^the ^moderators ^of ^CMV](http://www.reddit.com/message/compose?to=/r/changemyview) ^if ^you ^have ^any ^further ^questions ^or ^concerns.*",

"messages": {

"append_to_all_messages": "\n\n^[[Wiki](http://www.reddit.com/r/changemyview/wiki/deltabot)][[Code](https://github.com/alexames/DeltaBot)][/r/DeltaBot]",

"confirmation": [
"Confirmed: 1 delta awarded to /u/%s. ^[[History](/r/%s/wiki/user/%s)]"
],
"already_awarded": [
"You have already awarded /u/%s a delta in this comment tree."
],
"too_little_text": [
"This delta is currently disallowed as your comment contains either no or little text ([comment rule 4](http://www.reddit.com/r/changemyview/wiki/rules#wiki_rule_4)). Please include an explanation for how /u/%s changed your view. If you edit this in, replying to my comment will make me rescan yours."
],
"broken_rule": [
"You cannot award OP a delta as the moderators feel that allowing so would send the wrong message. If you were trying show the OP how to award a delta, please do so without using the delta symbol unless it's included in a reddit quote."
]
},


"tokens": ["∆", "∆", "Δ"],

"flair": {
Expand All @@ -46,9 +28,5 @@

"minimum_comment_length": 100,

"scoreboard": {
"table_head": "\n\n| Rank | Username | Deltas |\n| :------: | ------ | :------: |",
"table_leader_entry": "\n| **1** | **/u/%s** | [**%s**](/r/%s/wiki/user/%s \"Delta history.\") |",
"table_entry": "\n| %s | /u/%s | %s ([%s](/r/%s/wiki/user/%s \"Delta history.\")) |"
}
"days_to_rescan": 10
}
11 changes: 11 additions & 0 deletions config/templates/first_award_message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Congratulations; you've earned your first ∆!

As you may already know, a ∆ is given when a comment has changed someone's view. For a more detailed explanation of the ∆ system, [see here](http://www.reddit.com/r/changemyview/wiki/deltabot).

/u/DeltaBot has updated your user flair and created [your own wiki page](/r/{{ config.subreddit }}/wiki/user/{{ awardee }}) which will be updated every time you earn a delta. If you do well, you may find yourself on our [leaderboards](http://www.reddit.com/r/changemyview/wiki/leaderboards) (the monthly one is also featured in our sidebar).

Good luck, and happy CMVing!

_____

*[^I ^am ^a ^bot](https://github.com/alexames/DeltaBot)^, ^and ^this ^action ^was ^performed ^automatically. ^Please [^contact ^the ^moderators ^of ^CMV](http://www.reddit.com/message/compose?to=/r/changemyview) ^if ^you ^have ^any ^further ^questions ^or ^concerns.*",
8 changes: 8 additions & 0 deletions config/templates/monthly_scoreboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% for awardee, awards in awardee_awards.items() %}
## {{ awardee }} ({{ awards|length}})

{% for award in awards %}
* [{{ award.submission_title }}]({{ award.submission_url }})

{% endfor %}
{% endfor %}
1 change: 1 addition & 0 deletions config/templates/replies/already_awarded.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You have already awarded /u/{{ parent_comment.author.name }} a delta in this comment tree.
1 change: 1 addition & 0 deletions config/templates/replies/already_awarded_in_this_tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You have already awarded /u/{{ parent_comment.author.name }} a delta in this comment tree.
3 changes: 3 additions & 0 deletions config/templates/replies/append_to_all_messages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


^[[Wiki](http://www.reddit.com/r/changemyview/wiki/deltabot)][[Code](https://github.com/alexames/DeltaBot)][/r/DeltaBot]
1 change: 1 addition & 0 deletions config/templates/replies/author_awarded_self.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You can not award a ∆ to yourself.
1 change: 1 addition & 0 deletions config/templates/replies/awarded_op.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You cannot award OP a delta as the moderators feel that allowing so would send the wrong message. If you were trying to show the OP how to award a delta, please do so without using the delta symbol unless it's included in a reddit quote.
1 change: 1 addition & 0 deletions config/templates/replies/confirmed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Confirmed: 1 ∆ awarded to /u/{{parent_comment.author.name}}. ^[[History](/r/{{ config.subreddit }}/wiki/user/{{ parent_comment.author.name }})]
1 change: 1 addition & 0 deletions config/templates/replies/parent_author_is_me.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
That's very kind of you, but I already have infinite ∆s!
1 change: 1 addition & 0 deletions config/templates/replies/too_little_text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This ∆ is currently disallowed as your comment contains little or no text ([comment rule 4](http://www.reddit.com/r/changemyview/wiki/rules#wiki_rule_4)). Please include an explanation of how /u/{{ parent_comment.author.name }} changed your view. I will attempt to re-scan your comment for the next {{ config.days_to_rescan }} days, and if you edit your comment to make it long enough within that time I will award the ∆.
14 changes: 14 additions & 0 deletions config/templates/scoreboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


# Top Ten View Changers ({{ month }})


| Rank | Username | Deltas |
| :------: | ------ | :------: |
{%- for leader in leaders %}
{%- if loop.index == 1 %}
| **1** | **/u/{{ leader[0] }}** | [**{{ leader[1] }}**](/r/stahlous/wiki/user/{{ leader[0] }} \"Delta history\") |
{%- else %}
| {{ loop.index }} | /u/{{ leader[0] }} | [{{ leader[1] }}](/r/stahlous/wiki/user/{{ leader[0] }} \"Delta history\") |
{%- endif %}
{%- endfor %}
14 changes: 14 additions & 0 deletions config/templates/sidebar_scoreboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


# Top Ten View Changers ({{ month }})


| Rank | Username | Deltas |
| :------: | ------ | :------: |
{%- for leader in leaders %}
{%- if loop.index == 1 %}
| **1** | **/u/{{ leader['awardee'] }}** | [**{{ leader['num_awards'] }}**](/r/stahlous/wiki/user/{{ leader['awardee'] }} \"Delta history\") |
{%- else %}
| {{ loop.index }} | /u/{{ leader['awardee'] }} | [{{ leader['num_awards'] }}](/r/stahlous/wiki/user/{{ leader['awardee'] }} \"Delta history\") |
{%- endif %}
{%- endfor %}
10 changes: 10 additions & 0 deletions config/templates/user_wiki_page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ awardee }} has received {{ num_awards }} delta{% if num_awards > 1 %}s{% endif %} for the following comment{% if num_awards > 1 %}s{% endif %}:

{% for awarded_comment in awarded_comments %}
* [{{ awarded_comment.submission_title }}]({{ awarded_comment.submission_url }}) ({{ awarded_comment.awarding_comments|length }})
{% for awarding_comment in awarded_comment.awarding_comments %}
{{ loop.index }}. [Awarded by /u/{{ awarding_comment.author}}]({{ awarding_comment.url }}) on {{ dt.fromtimestamp(awarding_comment.time).strftime('%d %b %Y %H:%M') }}
{% endfor %}


{% endfor %}
3 changes: 2 additions & 1 deletion deltabot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def __init__(self, config_file):
if isinstance(config_file, dict):
self.attrs = config_file
elif os.path.isfile(config_file):
self.attrs = json.load(open(config_file))
with open(config_file, 'r', encoding='utf-8') as f:
self.attrs = json.load(f)
else:
self.attrs = json.loads(config_file)

Expand Down
105 changes: 105 additions & 0 deletions deltabot/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from sqlite3 import connect, Row
from datetime import datetime, timedelta

class DatabaseManager():
def __init__(self, filepath):
self.db = connect(filepath)
self.db.row_factory = Row
with self.db as conn:
conn.execute("""CREATE TABLE IF NOT EXISTS awards
(submission_id TEXT, submission_title TEXT, submission_self_text TEXT,
submission_author TEXT, submission_url TEXT, submission_time ,
awarded_comment_id TEXT, awarded_comment_text TEXT, awarded_comment_author TEXT,
awarded_comment_url TEXT, awarded_comment_time REAL,
awarding_comment_id TEXT, awarding_comment_text TEXT, awarding_comment_author TEXT,
awarding_comment_url TEXT, awarding_comment_time REAL)""")

conn.execute("""CREATE TABLE IF NOT EXISTS dispo_log
(comment_id TEXT PRIMARY KEY, dispo INT, reply_id TEXT, comment_time REAL)""")


def award_point(self, awarded_comment, awarding_comment):
submission = awarded_comment.submission
with self.db as conn:
conn.execute("""INSERT INTO awards VALUES
(
?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?, ?
)""",
(
submission.id, submission.title, submission.selftext,
submission.author.name, submission.permalink, submission.created_utc,
awarded_comment.id, awarded_comment.body, awarded_comment.author.name,
awarded_comment.permalink, awarded_comment.created_utc,
awarding_comment.id, awarding_comment.body, awarding_comment.author.name,
awarding_comment.permalink, awarding_comment.created_utc
)
)

def previous_awards_in_submission(self, awarded_comment, awarding_comment):
with self.db:
cur = self.db.cursor()
cur.execute('''SELECT awarding_comment_id FROM awards where submission_id = ?
and awarding_comment_author = ? and awarded_comment_author = ?''',
(awarded_comment.submission.id, awarding_comment.author.name,
awarded_comment.author.name))
previous_awards = cur.fetchall()
return previous_awards

def already_awarded_by_bot(self, awarding_comment):
with self.db:
cur = self.db.cursor()
cur.execute('SELECT * FROM awards WHERE awarding_comment_id = ?', (awarding_comment.id,))
rows = cur.fetchall()
return bool(rows)

def fetch_awards_by_month(self, year, month):
next_month = (month + 1) if (month < 12) else 1
next_year = year if (next_month > 1) else (year + 1)
with self.db:
cur = self.db.cursor()
cur.execute('''SELECT * FROM awards WHERE
(awarding_comment_time >= ?
AND awarding_comment_time < ?)''',
(datetime.timestamp(datetime(year, month, 1, 0, 0, 0)),
datetime.timestamp(datetime(next_year, next_month, 1, 0, 0, 0))))
awards = cur.fetchall()
return [dict(award) for award in awards]


def fetch_awards_by_awardee(self, awardee):
with self.db:
cur = self.db.cursor()
cur.execute('''SELECT * FROM awards WHERE
awarded_comment_author = ?''', (awardee,))
awards = cur.fetchall()
return [dict(award) for award in awards]

def log_dispo(self, comment, dispo, reply):
with self.db as conn:
conn.execute('''INSERT OR REPLACE INTO dispo_log
(comment_id, dispo, reply_id, comment_time)
VALUES (?, ?, ?, ?)''',
(comment.id, dispo, reply.id, comment.created_utc))

def fetch_dispo_log_by_comment(self, comment):
with self.db:
cur = self.db.cursor()
cur.execute('SELECT * FROM dispo_log WHERE comment_id = ?', (comment.id,))
row = cur.fetchone()
return dict(row) if row else None

def fetch_recent_dispo_logs(self, days):
with self.db:
cur = self.db.cursor()
cur.execute('SELECT * FROM dispo_log WHERE comment_time > ?',
(datetime.timestamp(datetime.utcnow() - timedelta(days=days)),))
rows = cur.fetchall()
return [dict(row) for row in rows]

def delete_dispo_log(self, comment):
with self.db as conn:
conn.execute('DELETE FROM dispo_log WHERE comment_id = ?', (comment.id,))


Loading