Add tests
This commit is contained in:
13
.github/workflows/docker-publish.yml
vendored
13
.github/workflows/docker-publish.yml
vendored
@ -7,8 +7,21 @@ on:
|
||||
schedule:
|
||||
- cron: "0 0 * * *"
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: "stable"
|
||||
- run: go test ./... -v
|
||||
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
@ -7,6 +7,6 @@ COPY go.mod go.sum ./
|
||||
RUN go mod download && go mod verify
|
||||
|
||||
COPY . .
|
||||
RUN go build -v -o /usr/local/bin/app ./...
|
||||
RUN go test ./... && go build -v -o /usr/local/bin/app ./...
|
||||
|
||||
CMD ["app"]
|
||||
|
11
main.go
11
main.go
@ -24,6 +24,10 @@ func init() {
|
||||
}
|
||||
|
||||
func GetPostsFromReddit(subreddit string) (string, error) {
|
||||
if subreddit == "" {
|
||||
return "", fmt.Errorf("subreddit cannot be empty")
|
||||
}
|
||||
|
||||
client, err := reddit.NewReadonlyClient()
|
||||
if err != nil {
|
||||
log.Println("Failed to create Reddit client:", err)
|
||||
@ -37,7 +41,12 @@ func GetPostsFromReddit(subreddit string) (string, error) {
|
||||
Time: "all",
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("failed to get posts from Reddit: %v", err)
|
||||
}
|
||||
|
||||
// Check if the subreddit exists
|
||||
if len(posts) == 0 {
|
||||
return "", fmt.Errorf("subreddit '%v' does not exist", subreddit)
|
||||
}
|
||||
|
||||
// [Title](<https://old.reddit.com{Permalink}>)\n{URL}
|
||||
|
35
main_test.go
Normal file
35
main_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Returns a string with a length between 1 and 2000 characters
|
||||
func TestGetPostsFromReddit_ReturnsPostWithValidLength(t *testing.T) {
|
||||
subreddit := "celebs"
|
||||
post, err := GetPostsFromReddit(subreddit)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
if len(post) < 1 || len(post) > 2000 {
|
||||
t.Errorf("Post length is not within the valid range")
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an error when the subreddit does not exist
|
||||
func TestGetPostsFromReddit_ReturnsErrorWhenSubredditDoesNotExist(t *testing.T) {
|
||||
subreddit := "nonexistent"
|
||||
_, err := GetPostsFromReddit(subreddit)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an error when the subreddit is empty
|
||||
func TestGetPostsFromReddit_ReturnsErrorWhenSubredditIsEmpty(t *testing.T) {
|
||||
subreddit := ""
|
||||
_, err := GetPostsFromReddit(subreddit)
|
||||
if err.Error() != "subreddit cannot be empty" {
|
||||
t.Errorf("Expected error 'subreddit cannot be empty', but got '%v'", err)
|
||||
}
|
||||
}
|
72
settings_test.go
Normal file
72
settings_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Returns a Config object with DiscordToken set when DISCORD_TOKEN environment variable is set
|
||||
func TestLoadFromEnvironment_DiscordTokenSet(t *testing.T) {
|
||||
os.Setenv("DISCORD_TOKEN", "test_token")
|
||||
defer os.Unsetenv("DISCORD_TOKEN")
|
||||
|
||||
config, err := loadFromEnvironment()
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got: %v", err)
|
||||
}
|
||||
|
||||
expected := &Config{
|
||||
DiscordToken: "test_token",
|
||||
}
|
||||
if !reflect.DeepEqual(config, expected) {
|
||||
t.Errorf("Expected config to be %v, but got %v", expected, config)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an error when DISCORD_TOKEN environment variable is empty
|
||||
func TestLoadFromEnvironment_EmptyDiscordToken(t *testing.T) {
|
||||
os.Setenv("DISCORD_TOKEN", "")
|
||||
defer os.Unsetenv("DISCORD_TOKEN")
|
||||
|
||||
_, err := loadFromEnvironment()
|
||||
if err == nil {
|
||||
t.Error("Expected an error, but got nil")
|
||||
}
|
||||
|
||||
expected := "DISCORD_TOKEN environment variable not set or empty. Also tried reading from settings.json file"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected error message to be '%s', but got '%s'", expected, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an error when DISCORD_TOKEN environment variable is not set and settings.json file is not present
|
||||
func TestLoadFromEnvironment_NoDiscordTokenNoSettingsFile(t *testing.T) {
|
||||
os.Unsetenv("DISCORD_TOKEN")
|
||||
|
||||
_, err := loadFromEnvironment()
|
||||
if err == nil {
|
||||
t.Error("Expected an error, but got nil")
|
||||
}
|
||||
|
||||
expected := "DISCORD_TOKEN environment variable not set or empty. Also tried reading from settings.json file"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected error message to be '%s', but got '%s'", expected, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Returns an error when settings.json file is present but DiscordToken is not set
|
||||
func TestLoadFromEnvironment_SettingsFileNoDiscordToken(t *testing.T) {
|
||||
os.Setenv("DISCORD_TOKEN", "")
|
||||
defer os.Unsetenv("DISCORD_TOKEN")
|
||||
|
||||
_, err := loadFromEnvironment()
|
||||
if err == nil {
|
||||
t.Error("Expected an error, but got nil")
|
||||
}
|
||||
|
||||
expected := "DISCORD_TOKEN environment variable not set or empty. Also tried reading from settings.json file"
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected error message to be '%s', but got '%s'", expected, err.Error())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user