Skip to content

Commit

Permalink
test: add offline tests
Browse files Browse the repository at this point in the history
Option to perform tests only offline
  • Loading branch information
TypicalAM authored Apr 15, 2024
2 parents 4014a33 + 6db01ce commit 77df16b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ If you have an idea or something doesn't work feel free to create an issue. If i
- Include output of `goread --version`
- Include logs which are usually located at `/tmp/goread.log` on linux and `%TMP%\goread.log` on Windows

When running tests (for example when packaging) you can disable online tests by setting the env var `TEST_OFFLINE_ONLY` to a truthy value (for example "YES").

## 💁 Credit where credit is due

### Libraries
Expand Down
27 changes: 27 additions & 0 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package backend

import (
"os"
"strings"
"testing"

"github.com/TypicalAM/goread/internal/backend/rss"
)

const TEST_OFFLINE_ENV = "TEST_OFFLINE_ONLY"

// testOffline checks if the tests should be in offline mode
func testOffline() bool {
offline, ok := os.LookupEnv(TEST_OFFLINE_ENV)
if !ok {
return false
}

truthy := []string{"1", "YES", "Y", "TRUE", "ON"}
for _, val := range truthy {
if strings.ToUpper(offline) == val {
return true
}
}

return false
}

// getBackend creates a fake backend
func getBackend() (*Backend, error) {
b, err := New("../test/data/urls.yml", "", false)
Expand Down Expand Up @@ -90,6 +111,12 @@ func TestBackendGetFeeds(t *testing.T) {

// TestBackendGetArticles if we get an error getting items from a feed doesn't work
func TestBackendGetArticles(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create a backend with a valid file
b, err := getBackend()
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions internal/backend/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
package cache

import (
"os"
"strings"
"testing"
"time"
)

const TEST_OFFLINE_ENV = "TEST_OFFLINE_ONLY"

// testOffline checks if the tests should be in offline mode
func testOffline() bool {
offline, ok := os.LookupEnv(TEST_OFFLINE_ENV)
if !ok {
return false
}

truthy := []string{"1", "YES", "Y", "TRUE", "ON"}
for _, val := range truthy {
if strings.ToUpper(offline) == val {
return true
}
}

return false
}

// getCache returns a new cache with the fake data
func getCache() (*Cache, error) {
cache, err := New("../../test/data")
Expand Down Expand Up @@ -53,6 +74,12 @@ func TestCacheLoadCorrectly(t *testing.T) {

// TestCacheGetArticles if we get an error when there's a cache miss but the cache doesn't change
func TestCacheGetArticles(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create the cache object with a valid file
cache, err := getCache()
if err != nil {
Expand Down Expand Up @@ -86,6 +113,12 @@ func TestCacheGetArticles(t *testing.T) {

// TestCacheGetArticleExpired if we get an error then the store doesn't delete expired cache when getting data
func TestCacheGetArticleExpired(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create the cache object with a valid file
cache, err := getCache()
if err != nil {
Expand Down

0 comments on commit 77df16b

Please sign in to comment.