Skip to content

Commit

Permalink
improve unit test for caching (#26185)
Browse files Browse the repository at this point in the history
- Configure `setting.CacheService.TTL` which will force the code to go
trough the caching mechanism.
- Remove the TODO and uncomment the test code.

(cherry picked from commit a201f2f1896c1a086f3e37f86b1306d44c07f2e4)

Refs: https://codeberg.org/forgejo/forgejo/pulls/974

---------

Co-authored-by: Gusted <postmaster@gusted.xyz>
  • Loading branch information
earl-warren and Gusted authored Jul 27, 2023
1 parent eaea530 commit 494ee56
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions modules/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func createTestCache() {
Adapter: "memory",
TTL: time.Minute,
})
setting.CacheService.TTL = 24 * time.Hour
}

func TestNewContext(t *testing.T) {
Expand Down Expand Up @@ -54,26 +55,25 @@ func TestGetString(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "", data)

// data, err = GetString("key", func() (string, error) {
// return "some data", nil
// })
// assert.NoError(t, err)
// assert.Equal(t, "", data)
// Remove("key")
data, err = GetString("key", func() (string, error) {
return "some data", nil
})
assert.NoError(t, err)
assert.Equal(t, "", data)
Remove("key")

data, err = GetString("key", func() (string, error) {
return "some data", nil
})
assert.NoError(t, err)
assert.Equal(t, "some data", data)

// data, err = GetString("key", func() (string, error) {
// return "", fmt.Errorf("some error")
// })
// assert.NoError(t, err)
// assert.Equal(t, "some data", data)

// TODO: uncommented code works in IDE but not with go test
data, err = GetString("key", func() (string, error) {
return "", fmt.Errorf("some error")
})
assert.NoError(t, err)
assert.Equal(t, "some data", data)
Remove("key")
}

func TestGetInt(t *testing.T) {
Expand All @@ -91,26 +91,25 @@ func TestGetInt(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, data)

// data, err = GetInt("key", func() (int, error) {
// return 100, nil
// })
// assert.NoError(t, err)
// assert.Equal(t, 0, data)
// Remove("key")
data, err = GetInt("key", func() (int, error) {
return 100, nil
})
assert.NoError(t, err)
assert.Equal(t, 0, data)
Remove("key")

data, err = GetInt("key", func() (int, error) {
return 100, nil
})
assert.NoError(t, err)
assert.Equal(t, 100, data)

// data, err = GetInt("key", func() (int, error) {
// return 0, fmt.Errorf("some error")
// })
// assert.NoError(t, err)
// assert.Equal(t, 100, data)

// TODO: uncommented code works in IDE but not with go test
data, err = GetInt("key", func() (int, error) {
return 0, fmt.Errorf("some error")
})
assert.NoError(t, err)
assert.Equal(t, 100, data)
Remove("key")
}

func TestGetInt64(t *testing.T) {
Expand All @@ -128,24 +127,23 @@ func TestGetInt64(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, 0, data)

// data, err = GetInt64("key", func() (int64, error) {
// return 100, nil
// })
// assert.NoError(t, err)
// assert.EqualValues(t, 0, data)
// Remove("key")
data, err = GetInt64("key", func() (int64, error) {
return 100, nil
})
assert.NoError(t, err)
assert.EqualValues(t, 0, data)
Remove("key")

data, err = GetInt64("key", func() (int64, error) {
return 100, nil
})
assert.NoError(t, err)
assert.EqualValues(t, 100, data)

// data, err = GetInt64("key", func() (int, error) {
// return 0, fmt.Errorf("some error")
// })
// assert.NoError(t, err)
// assert.EqualValues(t, 100, data)

// TODO: uncommented code works in IDE but not with go test
data, err = GetInt64("key", func() (int64, error) {
return 0, fmt.Errorf("some error")
})
assert.NoError(t, err)
assert.EqualValues(t, 100, data)
Remove("key")
}

0 comments on commit 494ee56

Please sign in to comment.