docstr: Should properly handle sort by metrics in various scenarios.
docstr_tok: ["Should","properly","handle","sort","by","metrics","in","various","scenarios","."]
code: def test_handle_sort_by_metrics(self):
      """
      Should properly handle sort by metrics in various scenarios.
      """
      self.login(username="admin")
  
      sql_text = get_sql_text(get_query_context("birth_names"))
      if backend() == "hive":
          # should have no duplicate `SUM(num)`
          assert "SUM(num) AS `sum__num`," not in sql_text
          assert "SUM(num) AS `sum__num`" in sql_text
          # the alias should be in ORDER BY
          assert "ORDER BY `sum__num` DESC" in sql_text
      else:
          assert re.search(
              r'ORDER BY [`"\[]?sum__num[`"\]]? DESC', sql_text
          )
  
      sql_text = get_sql_text(
          get_query_context("birth_names:only_orderby_has_metric")
      )
      if backend() == "hive":
          assert "SUM(num) AS `sum__num`," not in sql_text
          assert "SUM(num) AS `sum__num`" in sql_text
          assert "ORDER BY `sum__num` DESC" in sql_text
      else:
          assert re.search(
              r'ORDER BY SUM\([`"\[]?num[`"\]]?\) DESC',
              sql_text,
              re.IGNORECASE,
          )
  
      sql_text = get_sql_text(
          get_query_context("birth_names:orderby_dup_alias")
      )
  
      # Check SELECT clauses
      if backend() == "presto":
          # presto cannot have ambiguous alias in order by, so selected column
          # alias is renamed.
          assert 'sum("num_boys") AS "num_boys__"' in sql_text
      else:
          assert re.search(
              r'SUM\([`"\[]?num_boys[`"\]]?\) AS [`\"\[]?num_boys[`"\]]?',
              sql_text,
              re.IGNORECASE,
          )
  
      # Check ORDER BY clauses
      if backend() == "hive":
          # Hive must add additional SORT BY metrics to SELECT
          assert re.search(
              r"MAX\(CASE.*END\) AS `MAX\(CASE WHEN...`",
              sql_text,
              re.IGNORECASE | re.DOTALL,
          )
  
          # The additional column with the same expression but a different label
          # as an existing metric should not be added
          assert "sum(`num_girls`) AS `SUM(num_girls)`" not in sql_text
  
          # Should reference all ORDER BY columns by aliases
          assert "ORDER BY `num_girls` DESC," in sql_text
          assert "`AVG(num_boys)` DESC," in sql_text
          assert "`MAX(CASE WHEN...` ASC" in sql_text
      else:
          if backend() == "presto":
              # since the selected `num_boys` is renamed to `num_boys__`
              # it must be references as expression
              assert re.search(
                  r'ORDER BY SUM\([`"\[]?num_girls[`"\]]?\) DESC',
                  sql_text,
                  re.IGNORECASE,
              )
          else:
              # Should reference the adhoc metric by alias when possible
              assert re.search(
                  r'ORDER BY [`"\[]?num_girls[`"\]]? DESC',
                  sql_text,
                  re.IGNORECASE,
              )
  
          # ORDER BY only columns should always be expressions
          assert re.search(
              r'AVG\([`"\[]?num_boys[`"\]]?\) DESC',
              sql_text,
              re.IGNORECASE,
          )
          assert re.search(
              r"MAX\(CASE.*END\) ASC",
              sql_text,
              re.IGNORECASE | re.DOTALL,
          )
  
imports: import re
  import time
  from typing import Any
  import numpy
  import pandas
  import pytest
  from pandas import DateOffset
  from superset import db
  from superset.charts.schemas import ChartDataQueryContextSchema
  from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
  from superset.common.query_context import QueryContext
  from superset.common.query_context_factory import QueryContextFactory
  from superset.common.query_object import QueryObject
  from superset.connectors.sqla.models import SqlMetric
  from superset.daos.datasource import DatasourceDAO
  from superset.extensions import cache_manager
  from superset.superset_typing import AdhocColumn
  from superset.utils.core import AdhocMetricExpressionType, backend, DatasourceType, QueryStatus
  from superset.utils.pandas_postprocessing.utils import FLAT_COLUMN_SEPARATOR
  from tests.integration_tests.base_tests import SupersetTestCase
  from tests.integration_tests.conftest import only_postgresql, only_sqlite
  from tests.integration_tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices, load_birth_names_data
  from tests.integration_tests.fixtures.query_context import get_query_context
label_desc: Use image node_id 16 for calling the TestQueryContext obj's underlying member method code with example usage: obj.test_handle_sort_by_metrics() without return types