コード例 #1
0
    def test_trigger_when_data_file_is_not_exist_should_create_logfile_and_not_add_fail_adjust_log(
            self, get_logs_mock, log_mock, get_today_adjust_mock,
            adjust_price_mock):
        adjusts = []
        ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
        adjusts.append(ad_acb)
        ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
        adjusts.append(ad_vnd)
        get_today_adjust_mock.return_value = adjusts
        adjust_price_mock.side_effect = [None, RuntimeError('')]
        get_logs_mock.side_effect = RuntimeError()

        self.assertRaises(RuntimeError,
                          self.adjust_price_service.adjust_for_today)

        get_today_adjust_mock.assert_called_once()

        call_args_list = adjust_price_mock.call_args_list
        self.assertEqual(2, len(call_args_list))
        (args, kwargs) = call_args_list[0]
        self.assertEqual(ad_acb.symbol, kwargs['symbol'])
        self.assertEqual(ad_acb.ratio, kwargs['ratio'])
        (args, kwargs) = call_args_list[1]
        self.assertEqual(ad_vnd.symbol, kwargs['symbol'])
        self.assertEqual(ad_vnd.ratio, kwargs['ratio'])

        call_args_list = log_mock.call_args_list
        self.assertEqual(1, len(call_args_list))
        (args, kwargs) = call_args_list[0]
        self.assertEqual(ad_acb, kwargs['adjust_price'])
コード例 #2
0
    def test_trigger_when_adjusted_in_log_file_should_not_adjust(
            self, get_logs_mock, log_mock, get_today_adjust_mock,
            adjust_price_mock):
        adjusts = []
        ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
        adjusts.append(ad_acb)
        ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
        adjusts.append(ad_vnd)
        get_today_adjust_mock.return_value = adjusts
        adjust_price_mock.return_value = None
        get_logs_mock.return_value = [ad_acb]

        adjusted = self.adjust_price_service.adjust_for_today()

        # then
        self.assertEqual(1, len(adjusted))
        self.assertIn(ad_vnd, adjusted)

        get_today_adjust_mock.assert_called_once()

        call_args_list = adjust_price_mock.call_args_list
        self.assertEqual(1, len(call_args_list))
        (args, kwargs) = call_args_list[0]
        self.assertEqual(ad_vnd.symbol, kwargs['symbol'])
        self.assertEqual(ad_vnd.ratio, kwargs['ratio'])

        call_args_list = log_mock.call_args_list
        self.assertEqual(1, len(call_args_list))
        (args, kwargs) = call_args_list[0]
        self.assertEqual(ad_vnd, kwargs['adjust_price'])
コード例 #3
0
 def test_adjust_info_to_json(self):
     ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
     _json = ad_acb.to_json()
     self.assertEqual({
         "symbol": "ACB",
         "ratio": 0.98,
         "date": "2018-09-14"
     }, _json)
コード例 #4
0
ファイル: test_log.py プロジェクト: hungvm90/flask_tdd
 def test_get_logs_when_file_not_empty(self):
     self.delete_log()
     ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
     ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
     self._log.log(ad_acb)
     self._log.log(ad_vnd)
     adjusted = self._log.get_logs()
     self.assertIsNotNone(adjusted)
     self.assertEqual(2, len(adjusted))
     self.assertTrue(ad_vnd in adjusted)
     self.assertTrue(ad_acb in adjusted)
コード例 #5
0
ファイル: test_log.py プロジェクト: hungvm90/flask_tdd
    def test_when_file_not_exist_should_create_new(self):
        self.delete_log()
        ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
        self._log.log(ad_acb)

        self.assertTrue(os.path.exists(self._file_path))
        f = open(self._file_path)
        lines = f.readlines()
        lines = [line.strip() for line in lines if line]
        self.assertEqual(1, len(lines))
        self.assertEqual(ad_acb, AdjustInfo.from_json(json.loads(lines[0])))
コード例 #6
0
    def test_trigger_when_adjust_price_exception_should_throw(
            self, get_logs_mock, log_mock, get_today_adjust_mock,
            adjust_price_mock):
        adjusts = []
        ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
        adjusts.append(ad_acb)
        ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
        adjusts.append(ad_vnd)
        get_today_adjust_mock.return_value = adjusts
        adjust_price_mock.side_effect = RuntimeError('')
        get_logs_mock.return_value = []

        self.assertRaises(RuntimeError,
                          self.adjust_price_service.adjust_for_today)
コード例 #7
0
 def get_logs(self):
     if os.path.exists(self.file_path):
         adjusted = []
         with open(self.file_path) as f:
             for line in f:
                 adjusted.append(AdjustInfo.from_json(json.loads(line)))
         return adjusted
     else:
         raise RuntimeError('Log file not exist')
コード例 #8
0
 def test_trigger_when_success_should_return_adjusted(
         self, mock_adjust_price_service):
     adjusts = []
     ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
     adjusts.append(ad_acb)
     ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
     adjusts.append(ad_vnd)
     mock_adjust_price_service.adjust_for_today = Mock()
     mock_adjust_price_service.adjust_for_today.return_value = adjusts
     response = self.client.post('/api/adjust/trigger', headers={})
     self.assertEqual(response.status_code, 200)
     json_response = json.loads(response.get_data(as_text=True))
     self.assertIsNotNone(json_response.get('adjusts'))
     self.assertTrue(ad_acb.to_json() in json_response.get('adjusts'))
     self.assertTrue(ad_vnd.to_json() in json_response.get('adjusts'))
     mock_adjust_price_service.adjust_for_today.assert_called_once()
コード例 #9
0
ファイル: test_log.py プロジェクト: hungvm90/flask_tdd
    def test_when_file_exist_should_append(self):
        self.delete_log()
        ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
        ad_vnd = AdjustInfo("VND", 0.88, "2018-09-14")
        with open(self._file_path, mode='w') as f:
            f.write(json.dumps(ad_acb.to_json()))
            f.write(os.linesep)
        self._log.log(ad_vnd)

        self.assertTrue(os.path.exists(self._file_path))
        f = open(self._file_path)
        lines = f.readlines()
        lines = [line.strip() for line in lines if line]
        self.assertEqual(2, len(lines))
        self.assertEqual(ad_acb, AdjustInfo.from_json(json.loads(lines[0])))
        self.assertEqual(ad_vnd, AdjustInfo.from_json(json.loads(lines[1])))
コード例 #10
0
 def test_adjust_info_repr(self):
     ad_acb = AdjustInfo("ACB", 0.98, "2018-09-14")
     s = "{}".format(ad_acb)
     self.assertEqual(
         '{"symbol": "ACB", "ratio": 0.98, "date": "2018-09-14"}', s)