Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyabrin committed Sep 24, 2024
1 parent 0e9a4e3 commit 2e52692
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 121 deletions.
14 changes: 9 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2

updates:
- package-ecosystem: "gomod" # See documentation for possible values
directory: "/" # Location of package manifests
- package-ecosystem: gomod
directory: /
schedule:
interval: daily

- package-ecosystem: docker
directory: /
schedule:
interval: "daily"
interval: daily
31 changes: 19 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (

const API_URL = "https://cloud-api.yandex.net/v1/disk/"

type Method string
type HttpMethod string

const (
GET Method = "GET"
POST Method = "POST"
PUT Method = "PUT"
PATCH Method = "PATCH"
DELETE Method = "DELETE"
GET HttpMethod = "GET"
POST HttpMethod = "POST"
PUT HttpMethod = "PUT"
PATCH HttpMethod = "PATCH"
DELETE HttpMethod = "DELETE"
)

type Client struct {
Expand All @@ -47,21 +47,28 @@ func New(token ...string) *Client {
}
}

func (c *Client) doRequest(ctx context.Context, method Method, resource string, body io.Reader) (*http.Response, error) {
func (c *Client) doRequest(ctx context.Context, method HttpMethod, resource string, data io.Reader) (*http.Response, error) {

var resp *http.Response
var err error
var data io.Reader
var body io.Reader

// ctx, cancel := context.WithCancel(ctx)
body = data

data = body
// todo: make time parameterized, not const
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
defer cancel()

if method == GET || method == DELETE {
data = nil
body = nil
}

req, err := http.NewRequestWithContext(ctx, string(method), API_URL+resource, body)
if err != nil {
c.Logger.Fatal("error request", err)
return nil, err
}

req, err := http.NewRequestWithContext(ctx, string(method), API_URL+resource, data)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "OAuth "+c.AccessToken)

Expand Down
76 changes: 76 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package disk

import (
"os"
"testing"
"time"
)

func TestNew(t *testing.T) {
// Helper function to reset environment variable
resetEnv := func() {
os.Unsetenv("YANDEX_DISK_ACCESS_TOKEN")
}

t.Run("With provided token", func(t *testing.T) {
resetEnv()
client := New("test-token")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "test-token" {
t.Errorf("Expected AccessToken to be 'test-token', got '%s'", client.AccessToken)
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 10*time.Second {
t.Errorf("Expected Timeout to be 10 seconds, got %v", client.HTTPClient.Timeout)
}
})

t.Run("With environment variable", func(t *testing.T) {
resetEnv()
os.Setenv("YANDEX_DISK_ACCESS_TOKEN", "env-token")
client := New()
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "env-token" {
t.Errorf("Expected AccessToken to be 'env-token', got '%s'", client.AccessToken)
}
})

t.Run("Without token and empty environment variable", func(t *testing.T) {
resetEnv()
client := New()
if client != nil {
t.Fatal("Expected nil client")
}
})

t.Run("With multiple tokens", func(t *testing.T) {
resetEnv()
client := New("token1", "token2")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "token1" {
t.Errorf("Expected AccessToken to be 'token1', got '%s'", client.AccessToken)
}
})

t.Run("HTTPClient configuration", func(t *testing.T) {
resetEnv()
client := New("test-token")
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 10*time.Second {
t.Errorf("Expected Timeout to be 10 seconds, got %v", client.HTTPClient.Timeout)
}
})
}
4 changes: 2 additions & 2 deletions disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package disk
import (
"context"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -32,7 +32,7 @@ func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
}

func loadTestResponse(actionName string) []byte {
response, _ := ioutil.ReadFile(TEST_DATA_DIR + actionName + ".json")
response, _ := os.ReadFile(TEST_DATA_DIR + actionName + ".json")
return response
}

Expand Down
34 changes: 14 additions & 20 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package disk

import (
"encoding/json"
"log"
)
import "log"

func prettyPrint(data interface{}) []byte {
result, err := json.MarshalIndent(data, "", " ")
if haveError(err) {
log.Fatal(err)
}
return result
}

func haveError(err error) bool {
// handleError is a helper function to handle errors
// and exit the program if an error occurs
func handleError(err error) {
if err != nil {
log.Fatal(err)
return true
log.Fatal("Error:", err)
}
return false
}

func inArray(n int, array []int) bool {
if len(array) == 0 {
return false
}

set := make(map[int]struct{}, len(array))
for _, b := range array {
if b == n {
return true
}
set[b] = struct{}{}
}
return false

_, exists := set[n]
return exists
}
Loading

0 comments on commit 2e52692

Please sign in to comment.