-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error on loading watches when one of them failed (#25)
* additional data type to accept both string and bool * adds testcases for data package * fixes GetMetrics error behavior, adds tests * replaces meaningless price collector test
- Loading branch information
Showing
3 changed files
with
117 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-FileCopyrightText: 2024 Stefan Schärmeli <schaermu@pm.me> | ||
// SPDX-License-Identifier: MIT | ||
package data | ||
|
||
import "testing" | ||
|
||
func TestStringBoolean_ProperlyUnmarshals(t *testing.T) { | ||
sb := StringBoolean(false) | ||
err := sb.UnmarshalJSON([]byte("false")) | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
if sb != false { | ||
t.Errorf("Expected false, got %v", sb) | ||
} | ||
|
||
sb = StringBoolean(true) | ||
err = sb.UnmarshalJSON([]byte("true")) | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
if sb != true { | ||
t.Errorf("Expected true, got %v", sb) | ||
} | ||
} | ||
|
||
func TestStringBoolean_UnmarshalsAnyStringToTrue(t *testing.T) { | ||
sb := StringBoolean(true) | ||
err := sb.UnmarshalJSON([]byte("yehyehyeh foobar")) | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
if sb != true { | ||
t.Errorf("Expected true, got %v", sb) | ||
} | ||
} | ||
|
||
func TestWatchItem_GetMetrics(t *testing.T) { | ||
w := WatchItem{ | ||
Title: "Test", | ||
Url: "https://example.com", | ||
} | ||
metrics, err := w.GetMetrics() | ||
if err != nil { | ||
t.Errorf("Expected no error, got %v", err) | ||
} | ||
if len(metrics) != 2 { | ||
t.Errorf("Expected 2 metrics, got %v", len(metrics)) | ||
} | ||
if metrics[0] != "Test" { | ||
t.Errorf("Expected Test, got %v", metrics[0]) | ||
} | ||
if metrics[1] != "example.com" { | ||
t.Errorf("Expected example.com, got %v", metrics[1]) | ||
} | ||
} | ||
|
||
func TestWatchItem_GetMetrics_EmptyTitle(t *testing.T) { | ||
w := WatchItem{ | ||
Url: "https://example.com", | ||
} | ||
_, err := w.GetMetrics() | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
} | ||
|
||
func TestWatchItem_GetMetrics_InvalidUri(t *testing.T) { | ||
w := WatchItem{ | ||
Title: "Test", | ||
Url: "foo-bar-is-not-a-uri", | ||
} | ||
_, err := w.GetMetrics() | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
} | ||
|
||
func TestWatchItem_GetMetrics_EmptyHost(t *testing.T) { | ||
w := WatchItem{ | ||
Title: "Test", | ||
Url: "http://", | ||
} | ||
_, err := w.GetMetrics() | ||
if err == nil { | ||
t.Errorf("Expected error, got nil") | ||
} | ||
} |