Skip to content

Commit

Permalink
fix: error on loading watches when one of them failed (#25)
Browse files Browse the repository at this point in the history
* 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
schaermu authored Apr 19, 2024
1 parent f57f35a commit 16ea98e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 14 deletions.
7 changes: 4 additions & 3 deletions pkg/collectors/price_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ func TestPriceCollector_NewWatchDuringRuntime(t *testing.T) {
testutil.ExpectMetrics(t, c, "price_metrics_autoregister.prom", expectedPriceMetrics...)
}

func TestPriceCollector_HandlesArrayResponse(t *testing.T) {
func TestPriceCollector_IgnoresWatchesWithoutTitle(t *testing.T) {
_, watchDb := testutil.NewCollectorTestDb()
server := testutil.CreateTestApiServer(t, watchDb, testutil.WithPricesAsArray())
server := testutil.CreateTestApiServer(t, watchDb)
emptyUuid, emptyTitleItem := testutil.NewTestItem("", 100, "CHF", 20, 15, 10)
watchDb[emptyUuid] = emptyTitleItem
defer server.Close()

client := cdio.NewTestApiClient(server.URL())
c := NewPriceCollector(client)

testutil.ExpectMetricCount(t, c, 2, expectedPriceMetrics...)
testutil.ExpectMetrics(t, c, "price_metrics.prom", expectedPriceMetrics...)
}
36 changes: 25 additions & 11 deletions pkg/data/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ import (
"net/url"
)

type StringBoolean bool

func (sb *StringBoolean) UnmarshalJSON(data []byte) error {
if string(data) == "false" {
*sb = false
} else {
*sb = true
}
return nil
}

type WatchItem struct {
LastChanged int64 `json:"last_changed"`
LastChecked int64 `json:"last_checked"`
LastError bool `json:"last_error"`
Title string `json:"title"`
Url string `json:"url"`
CheckCount int `json:"check_count,omitempty"`
FetchTime float64 `json:"fetch_time,omitempty"`
NotificationAlertCount int `json:"notification_alert_count,omitempty"`
LastCheckStatus int `json:"last_check_status,omitempty"`
PriceData *PriceData `json:"price,omitempty"`
LastChanged int64 `json:"last_changed"`
LastChecked int64 `json:"last_checked"`
LastError StringBoolean `json:"last_error"`
Title string `json:"title"`
Url string `json:"url"`
CheckCount int `json:"check_count,omitempty"`
FetchTime float64 `json:"fetch_time,omitempty"`
NotificationAlertCount int `json:"notification_alert_count,omitempty"`
LastCheckStatus int `json:"last_check_status,omitempty"`
PriceData *PriceData `json:"price,omitempty"`
}

type PriceData struct {
Expand All @@ -35,10 +46,13 @@ type SystemInfo struct {
}

func (w *WatchItem) GetMetrics() ([]string, error) {
url, err := url.Parse(w.Url)
url, err := url.ParseRequestURI(w.Url)
if err != nil {
return nil, err
} else if url.Host == "" {
return nil, fmt.Errorf("host is empty")
}

if w.Title == "" {
return nil, fmt.Errorf("title is empty")
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/data/api_test.go
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")
}
}

0 comments on commit 16ea98e

Please sign in to comment.