-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move check if file exsits to provider
- Loading branch information
1 parent
00ea57a
commit c7421ae
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} |