code: def test_get_column_analysis(unittest, test_data):
      import dtale.views as views
  
      with app.test_client() as c:
          with ExitStack():
              build_data_inst({c.port: test_data})
              build_dtypes(
                  {c.port: views.build_dtypes_state(test_data)}
              )
              build_settings({c.port: {}})
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(col="foo", filtered="true"),
              )
              response_data = response.get_json()
              expected = dict(
                  labels=[
                      "0.55",
                      "0.60",
                      "0.65",
                      "0.70",
                      "0.75",
                      "0.80",
                      "0.85",
                      "0.90",
                      "0.95",
                      "1.00",
                      "1.05",
                      "1.10",
                      "1.15",
                      "1.20",
                      "1.25",
                      "1.30",
                      "1.35",
                      "1.40",
                      "1.45",
                      "1.50",
                  ],
                  data=[
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      50,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                      0,
                  ],
                  desc={
                      "count": "50",
                      "std": "0",
                      "min": "1",
                      "max": "1",
                      "50%": "1",
                      "25%": "1",
                      "75%": "1",
                      "mean": "1",
                      "missing_ct": "0",
                      "missing_pct": 0.0,
                      "total_count": "50",
                      "kurt": 0.0,
                      "skew": 0.0,
                  },
                  chart_type="histogram",
                  dtype="int64",
                  query="",
                  timestamp=response_data["timestamp"],
              )
              unittest.assertEqual(
                  {
                      k: v
                      for k, v in response_data.items()
                      if k not in ["code", "cols"]
                  },
                  expected,
                  "should return 20-bin histogram for foo",
              )
              unittest.assertEqual(
                  response_data["code"], HISTOGRAM_CODE
              )
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(col="foo", bins=5, filtered="true"),
              )
              response_data = response.get_json()
              expected = dict(
                  labels=["0.7", "0.9", "1.1", "1.3", "1.5"],
                  data=[0, 0, 50, 0, 0],
                  desc={
                      "count": "50",
                      "std": "0",
                      "min": "1",
                      "max": "1",
                      "50%": "1",
                      "25%": "1",
                      "75%": "1",
                      "mean": "1",
                      "missing_ct": "0",
                      "missing_pct": 0.0,
                      "total_count": "50",
                      "kurt": 0.0,
                      "skew": 0.0,
                  },
                  chart_type="histogram",
                  dtype="int64",
                  query="",
                  timestamp=response_data["timestamp"],
              )
              unittest.assertEqual(
                  {
                      k: v
                      for k, v in response_data.items()
                      if k not in ["code", "cols"]
                  },
                  expected,
                  "should return 5-bin histogram for foo",
              )
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="foo", bins=5, target="baz", filtered="true"
                  ),
              )
              response_data = response.get_json()
              assert len(response_data["targets"])
              assert response_data["targets"][0]["target"] == "baz"
  
              global_state.set_settings(
                  c.port, dict(query="security_id > 10")
              )
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(col="foo", bins=5, filtered="true"),
              )
              response_data = response.get_json()
              expected = dict(
                  labels=["0.7", "0.9", "1.1", "1.3", "1.5"],
                  data=[0, 0, 39, 0, 0],
                  desc={
                      "count": "39",
                      "std": "0",
                      "min": "1",
                      "max": "1",
                      "50%": "1",
                      "25%": "1",
                      "75%": "1",
                      "mean": "1",
                      "missing_ct": "0",
                      "missing_pct": 0.0,
                      "total_count": "39",
                      "kurt": 0.0,
                      "skew": 0.0,
                  },
                  chart_type="histogram",
                  dtype="int64",
                  query="security_id > 10",
                  timestamp=response_data["timestamp"],
              )
              unittest.assertEqual(
                  {
                      k: v
                      for k, v in response_data.items()
                      if k not in ["code", "cols"]
                  },
                  expected,
                  "should return a filtered 5-bin histogram for foo",
              )
              global_state.set_settings(c.port, {})
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="foo",
                      type="value_counts",
                      top=2,
                      filtered="true",
                  ),
              )
              response_data = response.get_json()
              assert response_data["chart_type"] == "value_counts"
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="foo",
                      type="value_counts",
                      ordinalCol="bar",
                      ordinalAgg="mean",
                      filtered="true",
                  ),
              )
              response_data = response.get_json()
              assert "ordinal" in response_data
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="foo",
                      type="value_counts",
                      ordinalCol="bar",
                      ordinalAgg="pctsum",
                      filtered="true",
                  ),
              )
              response_data = response.get_json()
              assert "ordinal" in response_data
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="bar",
                      type="categories",
                      categoryCol="foo",
                      categoryAgg="mean",
                      filtered="true",
                  ),
              )
              response_data = response.get_json()
              assert "count" in response_data
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(
                      col="bar",
                      type="categories",
                      categoryCol="foo",
                      categoryAgg="pctsum",
                      filtered="true",
                  ),
              )
              response_data = response.get_json()
              assert "count" in response_data
  
      with app.test_client() as c:
          with ExitStack() as stack:
              build_data_inst({c.port: test_data})
              stack.enter_context(
                  mock.patch(
                      "numpy.histogram",
                      mock.Mock(
                          side_effect=Exception("histogram failure")
                      ),
                  )
              )
  
              response = c.get(
                  "/dtale/column-analysis/{}".format(c.port),
                  query_string=dict(col="foo", filtered="true"),
              )
              response_data = response.get_json()
              unittest.assertEqual(
                  response_data["error"],
                  "histogram failure",
                  "should handle histogram exception",
              )
  
file_path: dtale/tests/dtale/test_column_analysis.py
imports: import mock
  import numpy
  import os
  import pandas
  import pytest
  import dtale.global_state
  from tests.dtale.test_views import app
  from tests import ExitStack
  from tests.dtale import build_data_inst, build_settings, build_dtypes
label_desc: Use image node_id 1 for calling a global function with example usage: test_get_column_analysis(unittest, test_data) without return types