Skip to content

Commit

Permalink
refactor: move check if file exsits to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
michalczmiel committed Feb 16, 2024
1 parent 00ea57a commit c7421ae
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
13 changes: 13 additions & 0 deletions internal/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package internal

import "testing"

func TestGetRandomUserAgent(t *testing.T) {
t.Run("should return a random user agent", func(t *testing.T) {
userAgent := getRandomUserAgent()

if userAgent == "" {
t.Error("Expected user agent, got empty string")
}
})
}
10 changes: 9 additions & 1 deletion internal/provider/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package provider

import "github.com/michalczmiel/batch-image-getter/internal"
import (
"fmt"

"github.com/michalczmiel/batch-image-getter/internal"
)

type FileProvider struct {
path string
Expand All @@ -15,6 +19,10 @@ func NewFileProvider(path string, fileSystem internal.FileSystem) Provider {
}

func (p *FileProvider) Links() ([]string, error) {
if !p.fileSystem.Exists(p.path) {
return nil, fmt.Errorf("file does not exist")
}

lines, err := p.fileSystem.ReadLines(p.path)
if err != nil {
return nil, err
Expand Down
76 changes: 76 additions & 0 deletions internal/provider/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package provider

import (
"io"
"testing"
)

type mockFileSystem struct{}

func (m mockFileSystem) Exists(path string) bool {
return path == "images.txt"
}

func (m mockFileSystem) ReadLines(path string) ([]string, error) {
return []string{
"https://example.com/image1.jpg",
"-",
"\n",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg",
}, nil
}

func (m mockFileSystem) WriteLines(path string, lines []string) error {
return nil
}

func (m mockFileSystem) CreateDirectory(path string) error {
return nil
}

func (m mockFileSystem) Save(body io.ReadCloser, path string) error {
return nil
}

func TestProviderLinks(t *testing.T) {
t.Run("should return error when file does not exist", func(t *testing.T) {
fileSystem := mockFileSystem{}

provider := NewFileProvider("non-existing-file.txt", fileSystem)

_, err := provider.Links()

if err == nil {
t.Fatal("Expected error, got nil")
}
})

t.Run("should return valid links from existing file", func(t *testing.T) {
fileSystem := mockFileSystem{}

provider := NewFileProvider("images.txt", fileSystem)

links, err := provider.Links()

if err != nil {
t.Error(err)
}

expectedLinks := []string{
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg",
}

if len(links) != len(expectedLinks) {
t.Fatalf("Expected %d links, got %d", len(expectedLinks), len(links))
}

for i, link := range links {
if link != expectedLinks[i] {
t.Fatalf("Expected %s, got %s", expectedLinks[i], link)
}
}
})
}

0 comments on commit c7421ae

Please sign in to comment.