def setup_for_test(self):
        #Fale alerter
        self.alerter = FakeAlerter()

        #The class under test
        self.monitor = UriMonitor(self.cur_session, [self.alerter])

        #Patch out urlopen
        self.fake_url_reader = FakeUrlReader()
        self.patched_openurl = urllib2.urlopen

        urllib2.urlopen = self.fake_url_reader.open

        #patch out hashcheck class
        self.old_hash_check = daemonAlertMe.monitor.HashCheck
        daemonAlertMe.monitor.HashCheck = FakeCheck
class UriMonitorTests(DbTest):
    def setup_for_test(self):
        #Fale alerter
        self.alerter = FakeAlerter()

        #The class under test
        self.monitor = UriMonitor(self.cur_session, [self.alerter])

        #Patch out urlopen
        self.fake_url_reader = FakeUrlReader()
        self.patched_openurl = urllib2.urlopen

        urllib2.urlopen = self.fake_url_reader.open

        #patch out hashcheck class
        self.old_hash_check = daemonAlertMe.monitor.HashCheck
        daemonAlertMe.monitor.HashCheck = FakeCheck

    def clean_up_after_test(self):
        #Restore hashcheck class
        daemonAlertMe.monitor.HashCheck = self.old_hash_check

        #Restore urlopen from patch
        urllib2.urlopen = self.patched_openurl

    def test_run_all_1_check_no_alerts_no_url_opened(self):
        self.add_with_one_uri_check_with_id_and_no_alerts()

        self.monitor.run_all()

        self.assertFalse(self.fake_url_reader.called_open)

    def test_run_all_1_check_with_alert_url_opened(self):
        self.add_with_one_uri_check_with_id_and_no_alerts()
        self.add_alert_for_uri_check_with_id()

        self.monitor.run_all()

        self.assertTrue(self.fake_url_reader.called_open)
        self.assertEquals('google.com', self.fake_url_reader.opened_url[0])

    def test_run_all_2_check_with_1_alert_url_opened(self):
        expected_url = 'open_me'
        self.add_with_one_uri_check_with_id_and_no_alerts()
        self.add_with_one_uri_check_with_id_and_no_alerts(aid = 2,
                                                          url=expected_url)
        self.add_alert_for_uri_check_with_id(uri_check_id = 2)

        self.monitor.run_all()

        self.assertTrue(self.fake_url_reader.open_call_count == 1)
        self.assertEquals(expected_url, self.fake_url_reader.opened_url[0])

    def test_run_all_1_check_with_alert_hash_check_called(self):
        self.add_with_one_uri_check_with_id_and_no_alerts()
        self.add_alert_for_uri_check_with_id(uri_check_id = 1)

        self.monitor.run_all()

        self.assertTrue(FakeCheck.last_instance.has_changes_called)

    def test_run_all_1_check_with_alert_check_changed_alert_sent(self):
        expected_check_id = 1
        expected_url = 'google.com'
        self.add_with_one_uri_check_with_id_and_no_alerts(
                                aid = expected_check_id, url=expected_url)
        self.add_alert_for_uri_check_with_id(uri_check_id = expected_check_id)
        FakeCheck.change_return_value = True

        self.monitor.run_all()

        self.assertTrue(self.alerter.alert_called)
        self.assertEqual(self.alerter.alert_for_id, expected_check_id)
        self.assertEqual(self.alerter.alert_url, expected_url)

    def test_run_all_1_check_with_hash_change_new_hash_and_time_saved(self):
        expected_check_id = 1
        expected_url = 'google.com'
        self.add_with_one_uri_check_with_id_and_no_alerts(
                                aid = expected_check_id, url=expected_url)
        self.add_alert_for_uri_check_with_id(uri_check_id = expected_check_id)
        FakeCheck.change_return_value = True
        FakeCheck.set_hash = 'updated_hash'

        self.monitor.run_all()

        check = self.cur_session.query(UriCheck)\
                .filter_by(check_id = expected_check_id).one() 
        
        assert 'updated_hash' == check.check_options

    def test_run_all_1_check_with_alert_check_no_changed_alert_not_sent(self):
        expected_check_id = 1
        expected_url = 'google.com'
        self.add_with_one_uri_check_with_id_and_no_alerts(
                                                aid = expected_check_id,
                                                url=expected_url)
        self.add_alert_for_uri_check_with_id(uri_check_id = expected_check_id)
        FakeCheck.change_return_value = False

        self.monitor.run_all()

        self.assertFalse(self.alerter.alert_called)