refactoring testing removing for now
This commit is contained in:
parent
853d8e4d6c
commit
eec5da5f92
@ -1,93 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestReadFile tests the ReadFile function of the Config struct.
|
||||
func TestReadFile(t *testing.T) {
|
||||
// Create a temporary YAML configuration file for testing
|
||||
tmpFile, err := ioutil.TempFile("", "config.yml")
|
||||
assert.NoError(t, err)
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
// Define a sample YAML content for testing
|
||||
yamlContent := `
|
||||
environment: production
|
||||
host: 192.168.1.1
|
||||
port: 8080
|
||||
cache:
|
||||
java_status_duration: 10m
|
||||
bedrock_status_duration: 15m
|
||||
icon_duration: 30m
|
||||
`
|
||||
// Write the YAML content to the temporary file
|
||||
_, err = tmpFile.WriteString(yamlContent)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Create a Config instance
|
||||
config := &Config{}
|
||||
|
||||
// Call the ReadFile function
|
||||
err = config.ReadFile(tmpFile.Name())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Verify the values in the Config instance
|
||||
assert.Equal(t, "production", config.Environment)
|
||||
assert.Equal(t, "192.168.1.1", config.Host)
|
||||
assert.Equal(t, uint16(8080), config.Port)
|
||||
assert.Equal(t, 10*time.Minute, config.Cache.JavaStatusDuration)
|
||||
assert.Equal(t, 15*time.Minute, config.Cache.BedrockStatusDuration)
|
||||
assert.Equal(t, 30*time.Minute, config.Cache.IconDuration)
|
||||
}
|
||||
|
||||
// TestOverrideWithEnvVars tests the overrideWithEnvVars function of the Config struct.
|
||||
func TestOverrideWithEnvVars(t *testing.T) {
|
||||
// Set environment variables for testing
|
||||
os.Setenv("ENVIRONMENT", "development")
|
||||
os.Setenv("HOST", "localhost")
|
||||
os.Setenv("PORT", "3000")
|
||||
defer func() {
|
||||
os.Unsetenv("ENVIRONMENT")
|
||||
os.Unsetenv("HOST")
|
||||
os.Unsetenv("PORT")
|
||||
}()
|
||||
|
||||
// Create a Config instance with default values
|
||||
config := &Config{
|
||||
Environment: "production",
|
||||
Host: "192.168.1.1",
|
||||
Port: 8080,
|
||||
}
|
||||
|
||||
// Call the overrideWithEnvVars function
|
||||
err := config.overrideWithEnvVars()
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Verify that the values have been overridden by the environment variables
|
||||
assert.Equal(t, "development", config.Environment)
|
||||
assert.Equal(t, "localhost", config.Host)
|
||||
assert.Equal(t, uint16(3000), config.Port)
|
||||
}
|
||||
|
||||
// TestOverrideWithEnvVarsInvalidPort tests the overrideWithEnvVars function with an invalid port value.
|
||||
func TestOverrideWithEnvVarsInvalidPort(t *testing.T) {
|
||||
// Set environment variables for testing
|
||||
os.Setenv("PORT", "invalid")
|
||||
defer os.Unsetenv("PORT")
|
||||
|
||||
// Create a Config instance
|
||||
config := &Config{}
|
||||
|
||||
// Call the overrideWithEnvVars function
|
||||
err := config.overrideWithEnvVars()
|
||||
|
||||
// Verify that an error is returned due to the invalid port value
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "invalid port value in environment variable", err.Error())
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestInit tests the init function in the main package.
|
||||
func TestInit(t *testing.T) {
|
||||
// Create a temporary YAML configuration file for testing
|
||||
tmpFile, err := ioutil.TempFile("", "config.yml")
|
||||
assert.NoError(t, err)
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
// Define a sample YAML content for testing
|
||||
yamlContent := `
|
||||
environment: development
|
||||
host: 127.0.0.1
|
||||
port: 8080
|
||||
cache:
|
||||
java_status_duration: 10m
|
||||
bedrock_status_duration: 15m
|
||||
icon_duration: 30m
|
||||
`
|
||||
// Write the YAML content to the temporary file
|
||||
_, err = tmpFile.WriteString(yamlContent)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Set environment variables for testing
|
||||
os.Setenv("ENVIRONMENT", "development")
|
||||
os.Setenv("HOST", "localhost")
|
||||
os.Setenv("PORT", "3000")
|
||||
defer func() {
|
||||
os.Unsetenv("ENVIRONMENT")
|
||||
os.Unsetenv("HOST")
|
||||
os.Unsetenv("PORT")
|
||||
}()
|
||||
|
||||
// Call the init function
|
||||
init()
|
||||
|
||||
// Verify that the values have been set correctly in the Config instance
|
||||
assert.Equal(t, "development", config.Environment)
|
||||
assert.Equal(t, "localhost", config.Host)
|
||||
assert.Equal(t, uint16(3000), config.Port)
|
||||
}
|
||||
|
||||
@ -1,74 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redismock/v8"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRedisConnect(t *testing.T) {
|
||||
r := &Redis{}
|
||||
config = &Config{
|
||||
Redis: nil,
|
||||
}
|
||||
|
||||
// Test missing Redis configuration
|
||||
err := r.Connect()
|
||||
assert.Equal(t, errors.New("missing Redis configuration"), err)
|
||||
|
||||
// Test successful connection
|
||||
db, mock := redismock.NewClientMock()
|
||||
mock.On("Ping", context.Background()).Return(nil)
|
||||
r.Client = db
|
||||
err = r.Connect()
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRedisGet(t *testing.T) {
|
||||
r := &Redis{}
|
||||
db, mock := redismock.NewClientMock()
|
||||
r.Client = db
|
||||
|
||||
// Test getting value and TTL
|
||||
mock.On("Get", context.Background(), "testKey").Return("testValue")
|
||||
mock.On("TTL", context.Background(), "testKey").Return(time.Minute, nil)
|
||||
value, ttl, err := r.Get("testKey")
|
||||
assert.Equal(t, []byte("testValue"), value)
|
||||
assert.Equal(t, time.Minute, ttl)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Test key not found
|
||||
mock.On("Get", context.Background(), "nonExistentKey").Return(nil)
|
||||
mock.On("TTL", context.Background(), "nonExistentKey").Return(time.Duration(0), nil)
|
||||
value, ttl, err = r.Get("nonExistentKey")
|
||||
assert.Nil(t, value)
|
||||
assert.Equal(t, time.Duration(0), ttl)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRedisSet(t *testing.T) {
|
||||
r := &Redis{}
|
||||
db, mock := redismock.NewClientMock()
|
||||
r.Client = db
|
||||
|
||||
// Test setting value and TTL
|
||||
mock.On("Set", context.Background(), "testKey", "testValue", time.Minute).Return(nil)
|
||||
err := r.Set("testKey", "testValue", time.Minute)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRedisIncrement(t *testing.T) {
|
||||
r := &Redis{}
|
||||
db, mock := redismock.NewClientMock()
|
||||
r.Client = db
|
||||
|
||||
// Test incrementing a key
|
||||
mock.On("Incr", context.Background(), "testKey").Return(nil)
|
||||
err := r.Increment("testKey")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@ -1,64 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
func TestPingHandler(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/ping", PingHandler)
|
||||
|
||||
resp, err := app.Test(utils.NewRequest(http.MethodGet, "/ping"))
|
||||
utils.AssertEqual(t, nil, err, "PingHandler")
|
||||
utils.AssertEqual(t, http.StatusOK, resp.StatusCode, "PingHandler")
|
||||
}
|
||||
|
||||
func TestJavaStatusHandler(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/status/java/:address", JavaStatusHandler)
|
||||
|
||||
resp, err := app.Test(utils.NewRequest(http.MethodGet, "/status/java/localhost:25565"))
|
||||
utils.AssertEqual(t, nil, err, "JavaStatusHandler")
|
||||
utils.AssertEqual(t, http.StatusOK, resp.StatusCode, "JavaStatusHandler")
|
||||
|
||||
// Test invalid address
|
||||
resp, err = app.Test(utils.NewRequest(http.MethodGet, "/status/java/invalid"))
|
||||
utils.AssertEqual(t, nil, err, "JavaStatusHandler")
|
||||
utils.AssertEqual(t, http.StatusBadRequest, resp.StatusCode, "JavaStatusHandler")
|
||||
}
|
||||
|
||||
func TestBedrockStatusHandler(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/status/bedrock/:address", BedrockStatusHandler)
|
||||
|
||||
resp, err := app.Test(utils.NewRequest(http.MethodGet, "/status/bedrock/localhost:19132"))
|
||||
utils.AssertEqual(t, nil, err, "BedrockStatusHandler")
|
||||
utils.AssertEqual(t, http.StatusOK, resp.StatusCode, "BedrockStatusHandler")
|
||||
|
||||
// Test invalid address
|
||||
resp, err = app.Test(utils.NewRequest(http.MethodGet, "/status/bedrock/invalid"))
|
||||
utils.AssertEqual(t, nil, err, "BedrockStatusHandler")
|
||||
utils.AssertEqual(t, http.StatusBadRequest, resp.StatusCode, "BedrockStatusHandler")
|
||||
}
|
||||
|
||||
func TestIconHandler(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/icon/:address", IconHandler)
|
||||
|
||||
resp, err := app.Test(utils.NewRequest(http.MethodGet, "/icon/localhost:25565"))
|
||||
utils.AssertEqual(t, nil, err, "IconHandler")
|
||||
utils.AssertEqual(t, http.StatusOK, resp.StatusCode, "IconHandler")
|
||||
|
||||
// Test invalid address
|
||||
resp, err = app.Test(utils.NewRequest(http.MethodGet, "/icon/invalid"))
|
||||
utils.AssertEqual(t, nil, err, "IconHandler")
|
||||
utils.AssertEqual(t, http.StatusBadRequest, resp.StatusCode, "IconHandler")
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user