-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from alexgolec/more-tests
More tests
- Loading branch information
Showing
10 changed files
with
2,097 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import atexit | ||
import io | ||
import json | ||
import logging | ||
import schwab | ||
import unittest | ||
|
||
from schwab.client import Client | ||
from .utils import MockResponse, no_duplicates | ||
from unittest.mock import Mock, patch | ||
|
||
|
||
class RedactorTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.redactor = schwab.debug.LogRedactor() | ||
|
||
@no_duplicates | ||
def test_no_redactions(self): | ||
self.assertEqual('test message', self.redactor.redact('test message')) | ||
|
||
@no_duplicates | ||
def test_simple_redaction(self): | ||
self.redactor.register('secret', 'SECRET') | ||
|
||
self.assertEqual( | ||
'<REDACTED SECRET> message', | ||
self.redactor.redact('secret message')) | ||
|
||
@no_duplicates | ||
def test_multiple_registrations_same_string(self): | ||
self.redactor.register('secret', 'SECRET') | ||
self.redactor.register('secret', 'SECRET') | ||
|
||
self.assertEqual( | ||
'<REDACTED SECRET> message', | ||
self.redactor.redact('secret message')) | ||
|
||
@no_duplicates | ||
def test_multiple_registrations_same_string_different_label(self): | ||
self.redactor.register('secret-A', 'SECRET') | ||
self.redactor.register('secret-B', 'SECRET') | ||
|
||
self.assertEqual( | ||
'<REDACTED SECRET-1> message <REDACTED SECRET-2>', | ||
self.redactor.redact('secret-A message secret-B')) | ||
|
||
|
||
class RegisterRedactionsTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.captured = io.StringIO() | ||
self.logger = logging.getLogger('test') | ||
self.dump_logs = schwab.debug._enable_bug_report_logging( | ||
output=self.captured, loggers=[self.logger]) | ||
schwab.LOG_REDACTOR = schwab.debug.LogRedactor() | ||
|
||
@no_duplicates | ||
def test_empty_string(self): | ||
schwab.debug.register_redactions('') | ||
|
||
@no_duplicates | ||
def test_empty_dict(self): | ||
schwab.debug.register_redactions({}) | ||
|
||
@no_duplicates | ||
def test_empty_list(self): | ||
schwab.debug.register_redactions([]) | ||
|
||
@no_duplicates | ||
def test_dict(self): | ||
schwab.debug.register_redactions( | ||
{'BadNumber': '100001'}, | ||
bad_patterns=['bad']) | ||
schwab.debug.register_redactions( | ||
{'OtherBadNumber': '200002'}, | ||
bad_patterns=['bad']) | ||
|
||
self.logger.info('Bad Number: 100001') | ||
self.logger.info('Other Bad Number: 200002') | ||
|
||
self.dump_logs() | ||
self.assertRegex( | ||
self.captured.getvalue(), | ||
r'\[.*\] Bad Number: <REDACTED BadNumber>\n' + | ||
r'\[.*\] Other Bad Number: <REDACTED OtherBadNumber>\n') | ||
|
||
@no_duplicates | ||
def test_list_of_dict(self): | ||
schwab.debug.register_redactions( | ||
[{'GoodNumber': '900009'}, | ||
{'BadNumber': '100001'}, | ||
{'OtherBadNumber': '200002'}], | ||
bad_patterns=['bad']) | ||
|
||
self.logger.info('Good Number: 900009') | ||
self.logger.info('Bad Number: 100001') | ||
self.logger.info('Other Bad Number: 200002') | ||
|
||
self.dump_logs() | ||
self.assertRegex( | ||
self.captured.getvalue(), | ||
r'\[.*\] Good Number: 900009\n' + | ||
r'\[.*\] Bad Number: <REDACTED 1-BadNumber>\n' + | ||
r'\[.*\] Other Bad Number: <REDACTED 2-OtherBadNumber>\n') | ||
|
||
@no_duplicates | ||
def test_whitelist(self): | ||
schwab.debug.register_redactions( | ||
[{'GoodNumber': '900009'}, | ||
{'BadNumber': '100001'}, | ||
{'OtherBadNumber': '200002'}], | ||
bad_patterns=['bad'], | ||
whitelisted=['otherbadnumber']) | ||
|
||
self.logger.info('Good Number: 900009') | ||
self.logger.info('Bad Number: 100001') | ||
self.logger.info('Other Bad Number: 200002') | ||
|
||
self.dump_logs() | ||
self.assertRegex( | ||
self.captured.getvalue(), | ||
r'\[.*\] Good Number: 900009\n' + | ||
r'\[.*\] Bad Number: <REDACTED 1-BadNumber>\n' + | ||
r'\[.*\] Other Bad Number: 200002\n') | ||
|
||
@no_duplicates | ||
@patch('schwab.debug.register_redactions', new_callable=Mock) | ||
def test_register_from_request_success(self, register_redactions): | ||
resp = MockResponse({'success': 1}, 200) | ||
schwab.debug.register_redactions_from_response(resp) | ||
register_redactions.assert_called_with({'success': 1}) | ||
|
||
@no_duplicates | ||
@patch('schwab.debug.register_redactions', new_callable=Mock) | ||
def test_register_from_request_not_okay(self, register_redactions): | ||
resp = MockResponse({'success': 1}, 403) | ||
schwab.debug.register_redactions_from_response(resp) | ||
register_redactions.assert_not_called() | ||
|
||
@no_duplicates | ||
@patch('schwab.debug.register_redactions', new_callable=Mock) | ||
def test_register_unparseable_json(self, register_redactions): | ||
class MR(MockResponse): | ||
def json(self): | ||
raise json.decoder.JSONDecodeError('e243rschwabgew', '', 0) | ||
|
||
resp = MR({'success': 1}, 200) | ||
schwab.debug.register_redactions_from_response(resp) | ||
register_redactions.assert_not_called() | ||
|
||
class EnableDebugLoggingTest(unittest.TestCase): | ||
|
||
@patch('logging.Logger.addHandler') | ||
def test_enable_doesnt_throw_exceptions(self, _): | ||
try: | ||
schwab.debug.enable_bug_report_logging() | ||
except AttributeError: | ||
self.fail("debug.enable_bug_report_logging() raised AttributeError unexpectedly") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from ..utils import has_diff, no_duplicates | ||
from schwab.orders.common import * | ||
from schwab.orders.generic import OrderBuilder | ||
|
||
import unittest | ||
|
||
class MultiOrderTest(unittest.TestCase): | ||
|
||
@no_duplicates | ||
def test_oco(self): | ||
self.assertFalse(has_diff({ | ||
'orderStrategyType': 'OCO', | ||
'childOrderStrategies': [ | ||
{'session': 'NORMAL'}, | ||
{'duration': 'DAY'}, | ||
] | ||
}, one_cancels_other( | ||
OrderBuilder().set_session(Session.NORMAL), | ||
OrderBuilder().set_duration(Duration.DAY)).build())) | ||
|
||
@no_duplicates | ||
def test_trigger(self): | ||
self.assertFalse(has_diff({ | ||
'orderStrategyType': 'TRIGGER', | ||
'session': 'NORMAL', | ||
'childOrderStrategies': [ | ||
{'duration': 'DAY'}, | ||
] | ||
}, first_triggers_second( | ||
OrderBuilder().set_session(Session.NORMAL), | ||
OrderBuilder().set_duration(Duration.DAY)).build())) |
Oops, something went wrong.