Exemple #1
0
    def test_add_exit_status(self):
        """Usage logger record run and then exit and verify they are added."""
        from natcap.invest.ui import usage_logger

        logging_server = usage_logger.LoggingServer()

        # set up a sample dict whose values are identical to its keys
        # this makes for an easy expected result
        sample_data = dict(
            (key_field, key_field)
            for key_field in usage_logger.LoggingServer._LOG_FIELD_NAMES)

        with mock.patch(
                'natcap.invest.ui.usage_logger.urllib2.urlopen') as mock_obj:
            logging_server.log_invest_run(sample_data, 'log')
        mock_obj.assert_called_once()
        sample_data['ip_address'] = 'local'
        self.assertEqual(
            sorted(mock_obj.call_args[0][0].get_data().split('&')),
            sorted(urllib.urlencode(sample_data).split('&')))

        exit_sample_data = dict(
            (key_field, key_field)
            for key_field in usage_logger.LoggingServer._EXIT_LOG_FIELD_NAMES)
        with mock.patch(
                'natcap.invest.ui.usage_logger.urllib2.urlopen') as mock_obj:
            logging_server.log_invest_run(exit_sample_data, 'exit')
        mock_obj.assert_called_once()
        exit_sample_data['ip_address'] = 'local'
        self.assertEqual(
            sorted(mock_obj.call_args[0][0].get_data().split('&')),
            sorted(urllib.urlencode(exit_sample_data).split('&')))
    def test_unknown_mode(self):
        """Usage logger test that an unknown mode raises an exception."""
        from natcap.invest.ui import usage_logger

        logging_server = usage_logger.LoggingServer()

        sample_data = dict(
            (key_field, key_field)
            for key_field in usage_logger.LoggingServer._LOG_FIELD_NAMES)

        with self.assertRaises(ValueError):
            logging_server.log_invest_run(sample_data, 'bad_mode')
    def test_add_exit_status(self):
        """Usage logger record run and then exit and verify they are added."""
        from natcap.invest.ui import usage_logger

        logging_server = usage_logger.LoggingServer()

        # set up a sample dict whose values are identical to its keys
        # this makes for an easy expected result
        sample_data = dict(
            (key_field, key_field)
            for key_field in usage_logger.LoggingServer._LOG_FIELD_NAMES)

        # This mock needs only to return a valid json string with the expected
        # key-value pairs.
        json_string = str(
            '{"START": "http://foo.bar", "FINISH": "http://foo.bar"}')
        with mock.patch('natcap.invest.ui.usage_logger.urlopen',
                        return_value=StringIO(json_string)) as mock_obj:
            logging_server.log_invest_run(sample_data, 'log')
        self.assertEqual(mock_obj.call_count, 2)
        sample_data['ip_address'] = 'local'
        self.assertEqual(
            sorted(mock_obj.call_args[0][0].data.decode('utf-8').split('&')),
            sorted(urlencode(sample_data).split('&')))

        exit_sample_data = dict(
            (key_field, key_field)
            for key_field in usage_logger.LoggingServer._EXIT_LOG_FIELD_NAMES)
        with mock.patch('natcap.invest.ui.usage_logger.urlopen',
                        return_value=StringIO(json_string)) as mock_obj:
            logging_server.log_invest_run(exit_sample_data, 'exit')
        self.assertEqual(mock_obj.call_count, 2)
        exit_sample_data['ip_address'] = 'local'
        self.assertEqual(
            sorted(mock_obj.call_args[0][0].data.decode('utf-8').split('&')),
            sorted(urlencode(exit_sample_data).split('&')))