Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 10th, 2012  |  syntax: Python  |  size: 16.27 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # 6.00
  2. # Problem Set 5 Test Suite
  3. import unittest
  4. import ps5
  5.  
  6. score = 0.0
  7. potential_score = 0.0
  8.  
  9. def add_potential_points(points):
  10.     global potential_score
  11.     potential_score += points
  12. def add_points(points):
  13.     global score
  14.     score += points
  15.  
  16. class ProblemSet5NewsStory(unittest.TestCase):
  17.     def setUp(self):
  18.         pass
  19.     def testNewsStoryConstructor(self):
  20.         add_potential_points(1)
  21.         story = NewsStory('', '', '', '', '')
  22.         add_points(1)
  23.     def testNewsStoryGetGuid(self):
  24.         add_potential_points(1)
  25.         story = NewsStory('test guid', 'test title', 'test subject',
  26.                           'test summary', 'test link')
  27.         self.assertEquals(story.get_guid(), 'test guid')
  28.         add_points(1)
  29.     def testNewsStoryGetTitle(self):
  30.         add_potential_points(1)
  31.         story = NewsStory('test guid', 'test title', 'test subject',
  32.                           'test summary', 'test link')
  33.         self.assertEquals(story.get_title(), 'test title')
  34.         add_points(1)
  35.     def testNewsStoryGetSubject(self):
  36.         add_potential_points(1)
  37.         story = NewsStory('test guid', 'test title', 'test subject',
  38.                           'test summary', 'test link')
  39.         self.assertEquals(story.get_subject(), 'test subject')
  40.         add_points(1)
  41.     def testNewsStoryGetSummary(self):
  42.         add_potential_points(1)
  43.         story = NewsStory('test guid', 'test title', 'test subject',
  44.                           'test summary', 'test link')
  45.         self.assertEquals(story.get_summary(), 'test summary')
  46.         add_points(1)
  47.     def testNewsStoryGetLink(self):
  48.         add_potential_points(1)
  49.         story = NewsStory('test guid', 'test title', 'test subject',
  50.                           'test summary', 'test link')
  51.         self.assertEquals(story.get_link(), 'test link')
  52.         add_points(1)
  53.  
  54. class ProblemSet5(unittest.TestCase):
  55.     def setUp(self):
  56.         class TrueTrigger:
  57.             def evaluate(self, story): return True
  58.  
  59.         class FalseTrigger:
  60.             def evaluate(self, story): return False
  61.  
  62.         self.tt = TrueTrigger()
  63.         self.tt2 = TrueTrigger()
  64.         self.ft = FalseTrigger()
  65.         self.ft2 = FalseTrigger()
  66.  
  67.     def test1TitleTrigger(self):
  68.         add_potential_points(5)
  69.         koala     = NewsStory('', 'Koala bears are soft and cuddly', '', '', '')
  70.         pillow    = NewsStory('', 'I prefer pillows that are soft.', '', '', '')
  71.         soda      = NewsStory('', 'Soft drinks are great', '', '', '')
  72.         pink      = NewsStory('', "Soft's the new pink!", '', '', '')
  73.         football  = NewsStory('', '"Soft!" he exclaimed as he threw the football', '', '', '')
  74.         microsoft = NewsStory('', 'Microsoft announced today that pillows are bad', '', '', '')
  75.         nothing   = NewsStory('', 'Reuters reports something really boring', '', '' ,'')
  76.         caps      = NewsStory('', 'soft things are soft', '', '', '')
  77.  
  78.         s1 = TitleTrigger('SOFT')
  79.         s2  = TitleTrigger('soft')
  80.         for trig in [s1, s2]:
  81.             self.assertTrue(trig.evaluate(koala), "TitleTrigger failed to fire when the word appeared in the title")
  82.             self.assertTrue(trig.evaluate(pillow), "TitleTrigger failed to fire when the word had punctuation on it")
  83.             self.assertTrue(trig.evaluate(soda), "TitleTrigger failed to fire when the case was different")
  84.             self.assertTrue(trig.evaluate(pink), "TitleTrigger failed to fire when the word had an apostrophe on it")
  85.             self.assertTrue(trig.evaluate(football), "TitleTrigger failed to fire in the presence of lots of punctuation")
  86.             self.assertTrue(trig.evaluate(caps), "TitleTrigger is case-sensitive and shouldn't be")
  87.            
  88.             self.assertFalse(trig.evaluate(microsoft), "TitleTrigger fired when the word was present, but not as its own word (e.g. 'soft' and 'Microsoft)'")
  89.             self.assertFalse(trig.evaluate(nothing), "TitleTrigger fired when the word wasn't really present in the title")
  90.         add_points(5)
  91.  
  92.   #   def test2SubjectTrigger(self):
  93. #         add_potential_points(5)
  94. #         koala     = NewsStory('', '', 'Koala bears are soft and cuddly', '', '')
  95. #         pillow    = NewsStory('', '', 'I prefer pillows that are soft.', '', '')
  96. #         soda      = NewsStory('', '', 'Soft drinks are great', '', '')
  97. #         pink      = NewsStory('', '', "Soft's the new pink!", '', '')
  98. #         football  = NewsStory('', '', '"Soft!" he exclaimed as he threw the football', '', '')
  99. #         microsoft = NewsStory('', '', 'Microsoft announced today that pillows are bad', '', '')
  100. #         nothing   = NewsStory('', '', 'Reuters reports something really boring', '', '')
  101. #         caps      = NewsStory('', '', 'soft things are soft', '', '')
  102. #
  103. #         s1 = SubjectTrigger('SOFT')
  104. #         s2  = SubjectTrigger('soft')
  105. #         for trig in [s1, s2]:
  106. #             self.assertTrue(trig.evaluate(koala), "SubjectTrigger failed to fire when the word appeared in the subject")
  107. #             self.assertTrue(trig.evaluate(pillow), "SubjectTrigger failed to fire when the word had punctuation on it")
  108. #             self.assertTrue(trig.evaluate(soda), "SubjectTrigger failed to fire when the case was different")
  109. #             self.assertTrue(trig.evaluate(pink), "SubjectTrigger failed to fire when the word had an apostrophe on it")
  110. #             self.assertTrue(trig.evaluate(football), "SubjectTrigger failed to fire in the presence of lots of punctuation")
  111. #             self.assertTrue(trig.evaluate(caps), "SubjectTrigger is case-sensitive and shouldn't be")
  112. #            
  113. #             self.assertFalse(trig.evaluate(microsoft), "SubjectTrigger fired when the word was present, but not as its own word (e.g. 'soft' and 'Microsoft)'")
  114. #             self.assertFalse(trig.evaluate(nothing), "SubjectTrigger fired when the word wasn't really present in the subject")
  115. #         add_points(5)
  116. #
  117. #     def test3SummaryTrigger(self):
  118. #         add_potential_points(5)
  119. #         koala     = NewsStory('', '', '', 'Koala bears are soft and cuddly', '')
  120. #         pillow    = NewsStory('', '', '', 'I prefer pillows that are soft.', '')
  121. #         soda      = NewsStory('', '', '', 'Soft drinks are great', '')
  122. #         pink      = NewsStory('', '', '', "Soft's the new pink!", '')
  123. #         football  = NewsStory('', '', '', '"Soft!" he exclaimed as he threw the football', '')
  124. #         microsoft = NewsStory('', '', '', 'Microsoft announced today that pillows are bad', '')
  125. #         nothing   = NewsStory('', '', '', 'Reuters reports something really boring', '')
  126. #         caps      = NewsStory('', '', '', 'soft things are soft', '')
  127. #
  128. #         s1 = SummaryTrigger('SOFT')
  129. #         s2  = SummaryTrigger('soft')
  130. #         for trig in [s1, s2]:
  131. #             self.assertTrue(trig.evaluate(koala), "SummaryTrigger failed to fire when the word appeared in the summary.")
  132. #             self.assertTrue(trig.evaluate(pillow), "SummaryTrigger failed to fire when the word had punctuation on it")
  133. #             self.assertTrue(trig.evaluate(soda), "SummaryTrigger failed to fire when the case was different")
  134. #             self.assertTrue(trig.evaluate(pink), "SummaryTrigger failed to fire when the word had an apostrophe on it")
  135. #             self.assertTrue(trig.evaluate(football), "SummaryTrigger failed to fire in the presence of lots of punctuation")
  136. #             self.assertTrue(trig.evaluate(caps), "SummaryTrigger is case-sensitive and shouldn't be")
  137. #            
  138. #             self.assertFalse(trig.evaluate(microsoft), "SummaryTrigger fired when the word was present, but not as its own word (e.g. 'soft' and 'Microsoft)'")
  139. #             self.assertFalse(trig.evaluate(nothing), "SummaryTrigger fired when the word wasn't really present in the summary")
  140. #         add_points(5)
  141. #
  142. #     def test4NotTrigger(self):
  143. #         add_potential_points(5)
  144. #         n = NotTrigger(self.tt)
  145. #         b = NewsStory("guid", "title", "subj", "summary", "link")
  146. #
  147. #         self.assertFalse(n.evaluate(b), "A NOT trigger applied to 'always true' DID NOT return false")
  148. #
  149. #         y = NotTrigger(self.ft)
  150. #         self.assertTrue(y.evaluate(b), "A NOT trigger applied to 'always false' DID NOT return true")
  151. #         add_points(5)
  152. #
  153. #     def test5AndTrigger(self):
  154. #         add_potential_points(5)
  155. #         yy = AndTrigger(self.tt, self.tt2)
  156. #         yn = AndTrigger(self.tt, self.ft)
  157. #         ny = AndTrigger(self.ft, self.tt)
  158. #         nn = AndTrigger(self.ft, self.ft2)
  159. #         b = NewsStory("guid", "title", "subj", "summary", "link")
  160. #
  161. #         self.assertTrue(yy.evaluate(b), "AND of 'always true' and 'always true' should be true")
  162. #         self.assertFalse(yn.evaluate(b), "AND of 'always true' and 'always false' should be false")
  163. #         self.assertFalse(ny.evaluate(b), "AND of 'always false' and 'always true' should be false")
  164. #         self.assertFalse(nn.evaluate(b), "AND of 'always false' and 'always false' should be false")
  165. #         add_points(5)
  166. #
  167. #     def test6OrTrigger(self):
  168. #         add_potential_points(5)
  169. #         yy = OrTrigger(self.tt, self.tt2)
  170. #         yn = OrTrigger(self.tt, self.ft)
  171. #         ny = OrTrigger(self.ft, self.tt)
  172. #         nn = OrTrigger(self.ft, self.ft2)
  173. #         b = NewsStory("guid", "title", "subj", "summary", "link")
  174. #
  175. #         self.assertTrue(yy.evaluate(b), "OR of 'always true' and 'always true' should be true")
  176. #         self.assertTrue(yn.evaluate(b), "OR of 'always true' and 'always false' should be true")
  177. #         self.assertTrue(ny.evaluate(b), "OR of 'always false' and 'always true' should be true")
  178. #         self.assertFalse(nn.evaluate(b), "OR of 'always false' and 'always false' should be false")
  179. #         add_points(5)
  180. #
  181. #     def test7PhraseTrigger(self):
  182. #         add_potential_points(5)
  183. #         pt = PhraseTrigger("New York City")
  184. #         a = NewsStory('', "asfdNew York Cityasfdasdfasdf", '', '', '')
  185. #         b = NewsStory('', '', "asdfasfdNew York Cityasfdasdfasdf", '', '')
  186. #         c = NewsStory('', '', '', "asdfasfdNew York Cityasfdasdfasdf", '')
  187. #         noa = NewsStory('', "something something new york city", '', '', '')
  188. #         nob = NewsStory('', '', "something something new york city", '', '')
  189. #         noc = NewsStory('', '', '', "something something new york city", '')
  190. #
  191. #
  192. #         self.assertTrue(pt.evaluate(a), "PhraseTrigger doesn't find phrase in title")
  193. #         self.assertTrue(pt.evaluate(b), "PhraseTrigger doesn't find phrase in subject")
  194. #         self.assertTrue(pt.evaluate(c), "PhraseTrigger doesn't find phrase in summary")
  195. #    
  196. #         for s in [noa, nob, noc]:
  197. #             self.assertFalse(pt.evaluate(s), "PhraseTrigger is case-insensitive, and shouldn't be")
  198. #         add_points(5)
  199. #
  200. #     def test8FilterStories(self):
  201. #         add_potential_points(5)
  202. #         pt = PhraseTrigger("New York City")
  203. #         a = NewsStory('', "asfdNew York Cityasfdasdfasdf", '', '', '')
  204. #         b = NewsStory('', '', "asdfasfdNew York Cityasfdasdfasdf", '', '')
  205. #         c = NewsStory('', '', '', "asdfasfdNew York Cityasfdasdfasdf", '')
  206. #         noa = NewsStory('', "something something new york city", '', '', '')
  207. #         nob = NewsStory('', '', "something something new york city", '', '')
  208. #         noc = NewsStory('', '', '', "something something new york city", '')
  209. #
  210. #         triggers = [pt, self.tt, self.ft]
  211. #         stories = [a, b, c, noa, nob, noc]
  212. #         filtered_stories = filter_stories(stories, triggers)
  213. #         for story in stories:
  214. #             self.assertTrue(story in filtered_stories)
  215. #         filtered_stories = filter_stories(stories, [self.ft])
  216. #         self.assertEquals(len(filtered_stories), 0)
  217. #         add_points(5)
  218. #
  219. #     def test8FilterStories2(self):
  220. #         add_potential_points(5)
  221. #         pt = PhraseTrigger("New York City")
  222. #         a = NewsStory('', "asfdNew York Cityasfdasdfasdf", '', '', '')
  223. #         b = NewsStory('', '', "asdfasfdNew York Cityasfdasdfasdf", '', '')
  224. #         c = NewsStory('', '', '', "asdfasfdNew York Cityasfdasdfasdf", '')
  225. #         noa = NewsStory('', "something something new york city", '', '', '')
  226. #         nob = NewsStory('', '', "something something new york city", '', '')
  227. #         noc = NewsStory('', '', '', "something something new york city", '')
  228. #
  229. #         class MatchTrigger(Trigger):
  230. #             def __init__(self, story):
  231. #                 self.story = story
  232. #             def evaluate(self, story):
  233. #                 return story == self.story
  234. #         triggers = [MatchTrigger(a), MatchTrigger(nob)]
  235. #         stories = [a, b, c, noa, nob, noc]
  236. #         filtered_stories = filter_stories(stories, triggers)
  237. #         self.assertTrue(a in filtered_stories)
  238. #         self.assertTrue(nob in filtered_stories)
  239. #         self.assertEquals(2, len(filtered_stories))
  240. #         add_points(5)
  241.  
  242. '''
  243.    def test9UserTriggers(self):
  244.        # Dependency Injection! (the nasty way):
  245.        triggers = \
  246.                'class Trigger(object):\n   def evaluate():\n       return False\nclass WordTrigger(Trigger):\n            def __init__(self, word):\n                self.word = word.lower()\n                print "Injected!"\n            def hash(self):\n                raise NotImplementedError\nclass SubjectTrigger(Trigger):\n    def __init__(self, word):\n        self.word = word.lower()\n        print "Injected Subject!"\n    def hash(self):\n        return "SUBJECT-" + self.word\nclass TitleTrigger(WordTrigger):\n    def hash(self):\n        return "TITLE-" + self.word\nclass SummaryTrigger(WordTrigger):\n    def hash(self):\n        return "SUMMARY-" + self.word\nclass NotTrigger(Trigger):\n    def __init__(self, trigger):\n        self.trigger = trigger\n        print "Injected!"\n    def hash(self):\n        return "NOT-" + self.trigger.hash()\nclass PhraseTrigger(Trigger):\n    def __init__(self, phrase):\n        self.phrase = phrase\n        print "Injected!"\n    def hash(self):\n        return "PHRASE-" + self.phrase\nclass CompositeTrigger(Trigger):\n    def __init__(self, trigger1, trigger2):\n        self.trigger1 = trigger1\n        self.trigger2 = trigger2\nclass AndTrigger(CompositeTrigger):\n    def hash(self):\n        t1_hash = self.trigger1.hash()\n        t2_hash = self.trigger2.hash()\n        if t1_hash > t2_hash:\n            return "AND-" + t1_hash + "-" + t2_hash\n        else:\n            return "AND-" + t2_hash + "-" + t1_hash\nclass OrTrigger(CompositeTrigger):\n    def hash(self):\n        t1_hash = self.trigger1.hash()\n        t2_hash = self.trigger2.hash()\n        if t1_hash > t2_hash:\n            return "OR-" + t1_hash + "-" + t2_hash\n        else:\n            return "OR-" + t2_hash + "-" + t1_hash\n\n\n'
  247.        import compiler
  248.        f = open('ps5.py')
  249.  
  250.        add_potential_points(5)
  251.        src = ""
  252.        for line in f:
  253.            if line == "if __name__ == '__main__':\n":
  254.                print "found line!"
  255.                break
  256.            src += line
  257.        src += triggers
  258.        print src
  259.        compiled = compiler.compile(src, 'compile-errors', 'exec')
  260.        exec compiled in locals(), globals()
  261.        t1 = SubjectTrigger('sports')
  262.        t2 = SummaryTrigger('Obama')
  263.        t3 = PhraseTrigger('Hillary Clinton')
  264.        t4 = OrTrigger(t2, t3)
  265.        trigger_map = {t1.hash() : False,
  266.                       t4.hash() : False}
  267.        print trigger_map
  268.  
  269.        trigger_list = readTriggerConfig("triggers.txt")
  270.        for trigger in trigger_list:
  271.            hash = trigger.hash()
  272.            self.assertTrue(hash in trigger_map)
  273.            self.assertFalse(trigger_map[hash])
  274.            trigger_map[hash] = True
  275.        for found in trigger_map.values():
  276.            self.assertTrue(found, "Missing trigger")
  277.        add_points(5)
  278. '''
  279.  
  280. # if __name__ == "__main__":
  281. #     suite = unittest.TestSuite()
  282. #     suite.addTest(unittest.makeSuite(ProblemSet5NewsStory))
  283. #     suite.addTest(unittest.makeSuite(ProblemSet5))
  284. #     #import t2
  285. #     #suite.addTest(unittest.makeSuite(t2.ProblemSet5TriggerFile))
  286. #     unittest.TextTestRunner(verbosity=2).run(suite)
  287. #     #print "t2 score", t2.score
  288. #     #print "t2 potential score", t2.potential_score
  289. #     #score += t2.score
  290. #     #potential_score += t2.potential_score
  291. #     #print "Total score: ", float(score) / float(potential_score) * 4.0
  292. #