code: def test_asof_join_nearest_with_tolerance() -> None:
      a = b = [1, 2, 3, 4, 5]
  
      nones = pl.Series([None, None, None, None, None], dtype=pl.Int64)
  
      # Case 1: complete miss
      df1 = pl.DataFrame(
          {"asof_key": [1, 2, 3, 4, 5], "a": a}
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [7, 8, 9, 10, 11],
              "b": b,
          }
      ).set_sorted("asof_key")
      expected = df1.with_columns(nones.alias("b"))
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=1
      )
      assert_frame_equal(out, expected)
  
      # Case 2: complete miss in other direction
      df1 = pl.DataFrame(
          {"asof_key": [7, 8, 9, 10, 11], "a": a}
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [1, 2, 3, 4, 5],
              "b": b,
          }
      ).set_sorted("asof_key")
      expected = df1.with_columns(nones.alias("b"))
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=1
      )
      assert_frame_equal(out, expected)
  
      # Case 3: match first item
      df1 = pl.DataFrame(
          {"asof_key": [1, 2, 3, 4, 5], "a": a}
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [6, 7, 8, 9, 10],
              "b": b,
          }
      ).set_sorted("asof_key")
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=1
      )
      expected = df1.with_columns(
          pl.Series([None, None, None, None, 1]).alias("b")
      )
      assert_frame_equal(out, expected)
  
      # Case 4: match last item
      df1 = pl.DataFrame(
          {"asof_key": [1, 2, 3, 4, 5], "a": a}
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [-4, -3, -2, -1, 0],
              "b": b,
          }
      ).set_sorted("asof_key")
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=1
      )
      expected = df1.with_columns(
          pl.Series([5, None, None, None, None]).alias("b")
      )
      assert_frame_equal(out, expected)
  
      # Case 5: match multiples, pick closer
      df1 = pl.DataFrame(
          {
              "asof_key": pl.Series([1, 2, 3, 4, 5], dtype=pl.Float64),
              "a": a,
          }
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [0, 2, 2.4, 3.4, 10],
              "b": b,
          }
      ).set_sorted("asof_key")
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=1
      )
      expected = df1.with_columns(
          pl.Series([2, 2, 4, 4, None]).alias("b")
      )
      assert_frame_equal(out, expected)
  
      # Case 6: use 0 tolerance
      df1 = pl.DataFrame(
          {
              "asof_key": pl.Series([1, 2, 3, 4, 5], dtype=pl.Float64),
              "a": a,
          }
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": [0, 2, 2.4, 3.4, 10],
              "b": b,
          }
      ).set_sorted("asof_key")
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance=0
      )
      expected = df1.with_columns(
          pl.Series([None, 2, None, None, None]).alias("b")
      )
      assert_frame_equal(out, expected)
  
      # Case 7: test with datetime
      df1 = pl.DataFrame(
          {
              "asof_key": pl.Series(
                  [
                      datetime(2023, 1, 1),
                      datetime(2023, 1, 2),
                      datetime(2023, 1, 3),
                      datetime(2023, 1, 4),
                      datetime(2023, 1, 6),
                  ]
              ),
              "a": a,
          }
      ).set_sorted("asof_key")
      df2 = pl.DataFrame(
          {
              "asof_key": pl.Series(
                  [
                      datetime(2022, 1, 1),
                      datetime(2022, 1, 2),
                      datetime(2022, 1, 3),
                      datetime(
                          2023, 1, 2, 21, 30, 0
                      ),  # should match with 2023-01-02, 2023-01-03, and 2021-01-04
                      datetime(2023, 1, 7),
                  ]
              ),
              "b": b,
          }
      ).set_sorted("asof_key")
      out = df1.join_asof(
          df2, on="asof_key", strategy="nearest", tolerance="1d4h"
      )
      expected = df1.with_columns(
          pl.Series([None, 4, 4, 4, 5]).alias("b")
      )
      assert_frame_equal(out, expected)
  
      # Case 8: test using timedelta tolerance
      out = df1.join_asof(
          df2,
          on="asof_key",
          strategy="nearest",
          tolerance=timedelta(days=1, hours=4),
      )
      assert_frame_equal(out, expected)
  
      # Case #9: last item is closest match
      df1 = pl.DataFrame(
          {
              "asof_key_left": [10.00001, 20.0, 30.0],
          }
      ).set_sorted("asof_key_left")
      df2 = pl.DataFrame(
          {
              "asof_key_right": [10.00001, 20.0001, 29.0],
          }
      ).set_sorted("asof_key_right")
      out = df1.join_asof(
          df2,
          left_on="asof_key_left",
          right_on="asof_key_right",
          strategy="nearest",
          tolerance=0.5,
      )
      expected = pl.DataFrame(
          {
              "asof_key_left": [10.00001, 20.0, 30.0],
              "asof_key_right": [10.00001, 20.0001, None],
          }
      )
      assert_frame_equal(out, expected)
  
imports: from datetime import date, datetime, timedelta
  from typing import Any
  import numpy
  import pytest
  import polars
  from polars.testing import assert_frame_equal
label_desc: Use image node_id 15 for calling a global function with example usage: test_asof_join_nearest_with_tolerance() without return types