Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Testing #14

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
123 changes: 123 additions & 0 deletions httphandler/datahandling_modifyData_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// datahandling_modifyData_test.go
package httphandler

import (
"encoding/json"
"errors"
"testing"

"github.com/dgraph-io/badger/v3"
"github.com/stretchr/testify/assert"
)

type MockStorage struct {
data map[string]map[string]interface{}
retrieveErr error
}

func (m *MockStorage) GetJSON(downloadKey string) ([]byte, error) {
if data, exists := m.data[downloadKey]; exists {
return json.Marshal(data)
}
return nil, badger.ErrKeyNotFound
}

func (m *MockStorage) Delete(downloadKey string) error {
delete(m.data, downloadKey)
return nil
}

func (m *MockStorage) Store(downloadKey string, dataToStore map[string]interface{}) error {
m.data[downloadKey] = dataToStore
return nil
}

func (m *MockStorage) Retrieve(downloadKey string) (map[string]interface{}, error) {
if m.retrieveErr != nil {
return nil, m.retrieveErr
}
if data, exists := m.data[downloadKey]; exists {
return data, nil
}
return nil, badger.ErrKeyNotFound
}

func TestModifyData(t *testing.T) {
mockStorage := &MockStorage{
data: map[string]map[string]interface{}{
"existingDownloadKey": {
"field1": "value1",
},
},
}

config := Config{
StorageInstance: mockStorage,
}

// Test case: Modify existing data with patch
paramMap := map[string]string{
"field2": "value2",
}
path := ""
isPatch := true
downloadKey := "existingDownloadKey"

modifiedData, err := config.modifyData(downloadKey, paramMap, path, isPatch)
assert.NoError(t, err)

expectedData := map[string]interface{}{
"field1": "value1",
"field2": "value2",
"timestamp": modifiedData["timestamp"],
}
assert.Equal(t, expectedData, modifiedData)

// Test case: Modify existing data with patch and nested path
paramMap = map[string]string{
"nestedField": "nestedValue",
}
path = "nested"
isPatch = true

modifiedData, err = config.modifyData(downloadKey, paramMap, path, isPatch)
assert.NoError(t, err)

expectedData = map[string]interface{}{
"field1": "value1",
"field2": "value2",
"nested": map[string]interface{}{
"nestedField": "nestedValue",
},
"timestamp": modifiedData["timestamp"],
}
assert.Equal(t, expectedData, modifiedData)

// Test case: Overwrite data without patch
paramMap = map[string]string{
"newField": "newValue",
}
path = ""
isPatch = false

modifiedData, err = config.modifyData(downloadKey, paramMap, path, isPatch)
assert.NoError(t, err)

expectedData = map[string]interface{}{
"newField": "newValue",
"timestamp": modifiedData["timestamp"],
}
assert.Equal(t, expectedData, modifiedData)

// Test case: Error during retrieval
mockStorage.retrieveErr = errors.New("retrieval error")
paramMap = map[string]string{
"field3": "value3",
}
path = ""
isPatch = true

modifiedData, err = config.modifyData(downloadKey, paramMap, path, isPatch)
assert.Error(t, err)
assert.Nil(t, modifiedData)
}
130 changes: 130 additions & 0 deletions httphandler/datahandling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package httphandler

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_addTimestampToThisData(t *testing.T) {
type args struct {
paramMap map[string]string
path string
}
tests := []struct {
name string
args args
check func(t *testing.T, paramMap map[string]string)
}{
{
name: "Add timestamp with empty path",
args: args{
paramMap: map[string]string{
"field1": "value1",
"field2": "value2",
},
path: "",
},
check: func(t *testing.T, paramMap map[string]string) {
timestamp := paramMap["timestamp"]
assert.NotEmpty(t, timestamp, "timestamp should not be empty")

for key, value := range paramMap {
if key == "timestamp" {
assert.Equal(t, timestamp, value, "timestamps should match")
}
if key == "field1_timestamp" || key == "field2_timestamp" {
assert.Equal(t, timestamp, value, "timestamps should match")
}
}
},
},
{
name: "Add timestamp with non-empty path",
args: args{
paramMap: map[string]string{
"field1": "value1",
"field2": "value2",
},
path: "nested",
},
check: func(t *testing.T, paramMap map[string]string) {
timestamp := paramMap["timestamp"]
assert.NotEmpty(t, timestamp, "timestamp should not be empty")

nestedTimestamp := paramMap["nested_timestamp"]
assert.Empty(t, nestedTimestamp, "nested_timestamp should not be empty")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
addTimestampToThisData(tt.args.paramMap, tt.args.path)
tt.check(t, tt.args.paramMap)
})
}
}

func Test_collectParams(t *testing.T) {
tests := []struct {
name string
params map[string][]string
want map[string]string
}{
{
name: "Single parameter with one value",
params: map[string][]string{
"param1": {"value1"},
},
want: map[string]string{
"param1": "value1",
},
},
{
name: "Single parameter with multiple values",
params: map[string][]string{
"param1": {"value1", "value2"},
},
want: map[string]string{
"param1": "value1",
},
},
{
name: "Multiple parameters with single values",
params: map[string][]string{
"param1": {"value1"},
"param2": {"value2"},
},
want: map[string]string{
"param1": "value1",
"param2": "value2",
},
},
{
name: "Empty parameter value",
params: map[string][]string{
"param1": {},
},
want: map[string]string{},
},
{
name: "Mixed parameters with empty and non-empty values",
params: map[string][]string{
"param1": {"value1"},
"param2": {},
},
want: map[string]string{
"param1": "value1",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := collectParams(tt.params); !reflect.DeepEqual(got, tt.want) {
t.Errorf("collectParams() = %v, want %v", got, tt.want)
}
})
}
}
File renamed without changes.
Loading