summaryrefslogtreecommitdiff
path: root/tests/test-wttrin-fetch-raw-string.el
blob: e7750255d099e27c7f1343e553c21d7266aeba73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
;;; test-wttrin-fetch-raw-string.el --- Tests for wttrin-fetch-raw-string -*- lexical-binding: t; -*-

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin-fetch-raw-string function.
;; Tests the public API for fetching weather data by location query.

;;; Code:

(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)

;;; Setup and Teardown

(defun test-wttrin-fetch-raw-string-setup ()
  "Setup for fetch-raw-string tests."
  (testutil-wttrin-setup))

(defun test-wttrin-fetch-raw-string-teardown ()
  "Teardown for fetch-raw-string tests."
  (testutil-wttrin-teardown))

;;; Normal Cases

(ert-deftest test-wttrin-fetch-raw-string-normal-builds-correct-url ()
  "The fetch should use a properly constructed wttr.in URL for the query."
  (test-wttrin-fetch-raw-string-setup)
  (unwind-protect
      (let ((fetched-url nil))
        (cl-letf (((symbol-function 'wttrin--fetch-url)
                   (lambda (url _callback) (setq fetched-url url))))
          (wttrin-fetch-raw-string "Paris" #'ignore)
          ;; URL should contain wttr.in and the encoded location
          (should (string-match-p "wttr\\.in" fetched-url))
          (should (string-match-p "Paris" fetched-url))))
    (test-wttrin-fetch-raw-string-teardown)))

(ert-deftest test-wttrin-fetch-raw-string-normal-passes-callback-through ()
  "The user's callback should receive the fetched data."
  (test-wttrin-fetch-raw-string-setup)
  (unwind-protect
      (let ((received-data nil))
        (cl-letf (((symbol-function 'wttrin--fetch-url)
                   (lambda (_url callback)
                     (funcall callback "weather response"))))
          (wttrin-fetch-raw-string "Paris"
                                   (lambda (data &optional _error-msg) (setq received-data data)))
          (should (equal received-data "weather response"))))
    (test-wttrin-fetch-raw-string-teardown)))

;;; Error Cases

(ert-deftest test-wttrin-fetch-raw-string-error-nil-query-signals-error ()
  "Passing nil as query should signal an error (invalid URL construction)."
  (test-wttrin-fetch-raw-string-setup)
  (unwind-protect
      (should-error (wttrin-fetch-raw-string nil #'ignore)
                    :type 'error)
    (test-wttrin-fetch-raw-string-teardown)))

(provide 'test-wttrin-fetch-raw-string)
;;; test-wttrin-fetch-raw-string.el ends here