Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: build_dtypes_state(test_data)}))
response = c.get('/dtale/dtypes/{}'.format(c.port))
response_data = json.loads(response.data)
assert response_data['success']
for col in test_data.columns:
response = c.get('/dtale/describe/{}/{}'.format(c.port, col))
response_data = json.loads(response.data)
assert response_data['success']
lots_of_groups = pd.DataFrame([dict(a=i, b=1) for i in range(150)])
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: lots_of_groups}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: build_dtypes_state(lots_of_groups)}))
response = c.get('/dtale/dtypes/{}'.format(c.port))
response_data = json.loads(response.data)
assert response_data['success']
response = c.get('/dtale/describe/{}/{}'.format(c.port, 'a'))
response_data = json.loads(response.data)
assert response_data['uniques']['top']
assert response_data['success']
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DTYPES', {}))
response = c.get('/dtale/dtypes/{}'.format(c.port))
response_data = json.loads(response.data)
assert 'error' in response_data
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/correlations/{}'.format(c.port), query_string=dict(query="missing_col == 'blah'"))
response_data = json.loads(response.data)
unittest.assertEqual(
response_data['error'], "name 'missing_col' is not defined", 'should handle correlations exception'
)
with app.test_client() as c:
with ExitStack() as stack:
test_data.loc[test_data.security_id == 1, 'bar'] = np.nan
test_data2 = test_data.copy()
test_data2.loc[:, 'date'] = pd.Timestamp('20000102')
test_data = pd.concat([test_data, test_data2], ignore_index=True)
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/correlations/{}'.format(c.port))
response_data = json.loads(response.data)
expected = expected = dict(
data=[
dict(column='security_id', security_id=1.0, foo=None, bar=None),
dict(column='foo', security_id=None, foo=None, bar=None),
dict(column='bar', security_id=None, foo=None, bar=None)
],
dates=['date']
)
unittest.assertEqual(response_data, expected, 'should return correlations')
# https://github.com/man-group/dtale/issues/43
ii = pd.date_range(start='2018-01-01', end='2019-12-01', freq='D')
ii = pd.Index(ii, name='date')
n = ii.shape[0]
{u'index': 0, u'foo': 0, u'bar': 0},
{u'index': 1, u'foo': 1, u'bar': 1},
{u'index': 2, u'foo': 2, u'bar': 2},
{u'index': 3, u'foo': 3, u'bar': 3},
{u'index': 4, u'foo': 4, u'bar': 4}
],
x='foo'
)
unittest.assertEqual(response_data, expected, 'should return scatter')
test_data = pd.DataFrame(build_ts_data(size=15001, days=1), columns=['date', 'security_id', 'foo', 'bar'])
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get(
'/dtale/scatter/{}'.format(c.port),
query_string=dict(dateCol='date', cols='foo,bar', date='20000101')
)
response_data = json.loads(response.data)
expected = dict(
stats={
'correlated': 15001,
'only_in_s0': 0,
'only_in_s1': 0,
'pearson': 1.0,
'spearman': 1.0,
},
error='Dataset exceeds 15,000 records, cannot render scatter. Please apply filter...'
)
unittest.assertEqual(response_data, expected, 'should return scatter')
response = c.get('/dtale/correlations/{}'.format(c.port))
response_data = json.loads(response.data)
expected = dict(
data=[
dict(column='security_id', security_id=1.0, foo=None, bar=None),
dict(column='foo', security_id=None, foo=None, bar=None),
dict(column='bar', security_id=None, foo=None, bar=None)
],
dates=[]
)
unittest.assertEqual(response_data, expected, 'should return correlations')
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/correlations/{}'.format(c.port), query_string=dict(query="missing_col == 'blah'"))
response_data = json.loads(response.data)
unittest.assertEqual(
response_data['error'], "name 'missing_col' is not defined", 'should handle correlations exception'
)
with app.test_client() as c:
with ExitStack() as stack:
test_data.loc[test_data.security_id == 1, 'bar'] = np.nan
test_data2 = test_data.copy()
test_data2.loc[:, 'date'] = pd.Timestamp('20000102')
test_data = pd.concat([test_data, test_data2], ignore_index=True)
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/correlations/{}'.format(c.port))
response_data = json.loads(response.data)
dates=['date']
)
unittest.assertEqual(response_data, expected, 'should return correlations')
# https://github.com/man-group/dtale/issues/43
ii = pd.date_range(start='2018-01-01', end='2019-12-01', freq='D')
ii = pd.Index(ii, name='date')
n = ii.shape[0]
c = 5
data = np.random.random((n, c))
df = pd.DataFrame(data, index=ii)
df, _ = views.format_data(df)
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: df}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(df)}))
response = c.get('/dtale/correlations/{}'.format(c.port))
response_data = json.loads(response.data)
unittest.assertEqual(len(response_data['dates']), 0, 'should no correlation date columns')
mock_find_free_port = stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
stack.enter_context(mock.patch('dtale.app.is_up', mock.Mock(return_value=False)))
mock_requests = stack.enter_context(mock.patch('requests.get', mock.Mock()))
instance = show(data=test_data, subprocess=False, name='foo')
assert 'http://localhost:9999' == instance._url
mock_run.assert_called_once()
mock_find_free_port.assert_called_once()
pdt.assert_frame_equal(instance.data, test_data)
tmp = test_data.copy()
tmp['biz'] = 2.5
instance.data = tmp
unittest.assertEqual(
views.DTYPES[instance._data_id],
views.build_dtypes_state(tmp),
'should update app data/dtypes'
)
instance2 = get_instance(instance._data_id)
assert instance2._url == instance._url
assert instances()[instance._data_id]._url == instance._url
instance.kill()
mock_requests.assert_called_once()
mock_requests.call_args[0][0] == 'http://localhost:9999/shutdown'
assert views.METADATA['1']['name'] == 'foo'
with ExitStack() as stack:
mock_run = stack.enter_context(mock.patch('dtale.app.DtaleFlask.run', mock.Mock()))
mock_find_free_port = stack.enter_context(mock.patch('dtale.app.find_free_port', mock.Mock(return_value=9999)))
stack.enter_context(mock.patch('socket.gethostname', mock.Mock(return_value='localhost')))
def test_get_data(unittest, test_data):
import dtale.views as views
with app.test_client() as c:
with ExitStack() as stack:
test_data, _ = views.format_data(test_data)
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/data/{}'.format(c.port))
response_data = json.loads(response.data)
unittest.assertEqual(response_data, {}, 'if no "ids" parameter an empty dict should be returned')
response = c.get('/dtale/data/{}'.format(c.port), query_string=dict(ids=json.dumps(['1'])))
response_data = json.loads(response.data)
expected = dict(
total=50,
results={'1': dict(date='2000-01-01', security_id=1, dtale_index=1, foo=1, bar=1.5, baz='baz')},
columns=[
dict(dtype='int64', name='dtale_index'),
dict(dtype='datetime64[ns]', name='date', index=0),
dict(dtype='int64', name='security_id', index=1),
dict(dtype='int64', name='foo', index=2),
dict(dtype='float64', name='bar', min=1.5, max=1.5, index=3),
dict(dtype='string', name='baz', index=4)
expected = dict(
stats={
'correlated': 15001,
'only_in_s0': 0,
'only_in_s1': 0,
'pearson': 1.0,
'spearman': 1.0,
},
error='Dataset exceeds 15,000 records, cannot render scatter. Please apply filter...'
)
unittest.assertEqual(response_data, expected, 'should return scatter')
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get(
'/dtale/scatter/{}'.format(c.port),
query_string=dict(dateCol='date', cols='foo,bar', date='20000101', query="missing_col == 'blah'")
)
response_data = json.loads(response.data)
unittest.assertEqual(
response_data['error'], "name 'missing_col' is not defined", 'should handle correlations exception'
)
def test_get_scatter(unittest):
import dtale.views as views
test_data = pd.DataFrame(build_ts_data(), columns=['date', 'security_id', 'foo', 'bar'])
test_data, _ = views.format_data(test_data)
with app.test_client() as c:
with ExitStack() as stack:
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get(
'/dtale/scatter/{}'.format(c.port),
query_string=dict(dateCol='date', cols='foo,bar', date='20000101', query="date == '20000101'")
)
response_data = json.loads(response.data)
expected = dict(
y='bar',
stats={
'pearson': 0.9999999999999999,
'correlated': 5,
'only_in_s0': 0,
'only_in_s1': 0,
'spearman': 0.9999999999999999
},
data=[
{u'index': 0, u'foo': 0, u'bar': 0},