test_util.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require "minitest/autorun"
  2. require_relative "../lib/util"
  3. class TestUtilParseAusDate < Minitest::Test
  4. # --- Standard formats ---
  5. def test_dd_mm_yyyy
  6. assert_equal Date.new(2025, 3, 15), Util.parse_aus_date("15/03/2025")
  7. end
  8. def test_dd_mm_yy
  9. assert_equal Date.new(2025, 8, 1), Util.parse_aus_date("01/08/25")
  10. end
  11. def test_natural_language_date
  12. assert_equal Date.new(2025, 6, 20), Util.parse_aus_date("20 June 2025")
  13. end
  14. def test_abbreviated_month
  15. assert_equal Date.new(2025, 4, 5), Util.parse_aus_date("5 Apr 2025")
  16. end
  17. def test_iso_format
  18. assert_equal Date.new(2025, 12, 1), Util.parse_aus_date("2025-12-01")
  19. end
  20. # --- Edge cases ---
  21. def test_nil_returns_nil
  22. assert_nil Util.parse_aus_date(nil)
  23. end
  24. def test_empty_string_returns_nil
  25. assert_nil Util.parse_aus_date("")
  26. end
  27. def test_whitespace_only_returns_nil
  28. assert_nil Util.parse_aus_date(" ")
  29. end
  30. def test_garbage_returns_nil
  31. assert_nil Util.parse_aus_date("not a date")
  32. end
  33. def test_partial_date_returns_nil
  34. assert_nil Util.parse_aus_date("15/03")
  35. end
  36. def test_strips_whitespace
  37. assert_equal Date.new(2025, 3, 15), Util.parse_aus_date(" 15/03/2025 ")
  38. end
  39. # --- Ambiguity: dd/mm not mm/dd ---
  40. # 01/02/2025 should be February 1st, not January 2nd
  41. def test_day_month_order
  42. result = Util.parse_aus_date("01/02/2025")
  43. assert_equal 2, result.month, "Expected month 2 (February), got #{result.month}"
  44. assert_equal 1, result.day
  45. end
  46. end
  47. class TestUtilParseEpochMs < Minitest::Test
  48. def test_known_timestamp
  49. # 2025-01-01 00:00:00 UTC in ms
  50. ms = Date.new(2025, 1, 1).to_time.to_i * 1000
  51. assert_equal Date.new(2025, 1, 1), Util.parse_epoch_ms(ms)
  52. end
  53. def test_string_input
  54. ms = Date.new(2024, 6, 15).to_time.to_i * 1000
  55. assert_equal Date.new(2024, 6, 15), Util.parse_epoch_ms(ms.to_s)
  56. end
  57. def test_nil_returns_nil
  58. assert_nil Util.parse_epoch_ms(nil)
  59. end
  60. def test_empty_string_returns_nil
  61. assert_nil Util.parse_epoch_ms("")
  62. end
  63. def test_zero_returns_a_date
  64. # epoch 0 = 1970-01-01 (valid, should not raise)
  65. result = Util.parse_epoch_ms(0)
  66. refute_nil result
  67. end
  68. end
  69. class TestUtilRefToTable < Minitest::Test
  70. def test_known_planbuild_ref
  71. # "DA-HOB-2025-1234" -> "da_hobartcity"
  72. assert_equal "da_hobartcity", Util.ref_to_table("DA-HOB-2025-1234")
  73. end
  74. def test_launceston_ref
  75. assert_equal "da_launceston", Util.ref_to_table("DA-LAU-2024-99")
  76. end
  77. def test_case_insensitive_code
  78. assert_equal "da_hobartcity", Util.ref_to_table("DA-hob-2025-1")
  79. end
  80. def test_unknown_code_raises
  81. assert_raises(RuntimeError) { Util.ref_to_table("DA-ZZZ-2025-1") }
  82. end
  83. end
  84. class TestUtilCouncilMap < Minitest::Test
  85. def test_all_council_map_values_start_with_da
  86. Util::COUNCIL_MAP.each do |key, table|
  87. assert table.start_with?("da_"),
  88. "COUNCIL_MAP[#{key.inspect}] = #{table.inspect} does not start with 'da_'"
  89. end
  90. end
  91. def test_all_planbuild_codes_map_to_known_councils
  92. Util::PLANBUILD_CODE_MAP.each do |code, council_key|
  93. assert Util::COUNCIL_MAP.key?(council_key),
  94. "PLANBUILD_CODE_MAP[#{code.inspect}] = #{council_key.inspect} has no entry in COUNCIL_MAP"
  95. end
  96. end
  97. end