Skip to content

Commit

Permalink
Added a unit test file.
Browse files Browse the repository at this point in the history
* Currently just tests one function: calculate_csat()
* Confirms old & new algorithm for a couple of test cases
* Baked in an exception that gets raised for pages with no yes or no
votes.
* added `.pyc` and `__pycache__` to `.gitignore` in order to avoid
synching unreadable content to git.
  • Loading branch information
Will Cross authored and jseldess committed Feb 28, 2020
1 parent 2e3208b commit b9f37b4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*~
.*.swp
*.pyc
__pycache__
.DS_Store
generate/generate
Railroad.jar
Expand Down
4 changes: 2 additions & 2 deletions scripts/docs_csat.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def create_dict(response, dict):
weighted_csats = dict()


def calculate_csat(no_votes, yes_votes, pageviews, blacklist):
def calculate_csat(no_votes, yes_votes, pageviews):

# Remove blacklisted pages.
for p in blacklist:
Expand Down Expand Up @@ -257,7 +257,7 @@ def main():
print()
print(
'Docs CSAT (basic, weighted):',
calculate_csat(no_votes, yes_votes, pageviews, blacklist)
calculate_csat(no_votes, yes_votes, pageviews)
)
print()

Expand Down
28 changes: 28 additions & 0 deletions scripts/test_docs_csat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python

import unittest
import docs_csat as dc


class TestDocsCSAT(unittest.TestCase):

def test_calculate_csat(self):
no_votes = {"page1": 1, "page2": 0, "page3": 1, "page4": 10}
yes_votes = {"page2": 10, "page3": 10, "page5": 1, "page6": 0}
pageviews = {"page2": 1000, "page4": 10}
# Raises ZeroDivisionError from wegihted CSAT section, line
# `average_csats[k] = yes / total`
self.assertRaises(ZeroDivisionError, dc.calculate_csat, no_votes,
yes_votes, pageviews)

no_votes = {"page1": 1, "page2": 0, "page3": 1, "page4": 10} # 12
yes_votes = {"page2": 10, "page3": 10, "page5": 1, "page6": 1} # 22
pageviews = {"page2": 1000, "page4": 10} # 1000 yes, 10 no
basic_csat, weighted_csat = dc.calculate_csat(no_votes, yes_votes,
pageviews)
self.assertEqual(basic_csat, 22 / (12 + 22))
self.assertEqual(weighted_csat, 1000 / (1000 + 10))


if __name__ == '__main__':
unittest.main()

0 comments on commit b9f37b4

Please sign in to comment.