update test cases in plugin_test file
This commit is contained in:
@@ -3,7 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"beckn-onix/shared/plugin/definition"
|
"beckn-onix/shared/plugin/definition"
|
||||||
@@ -13,22 +16,54 @@ import (
|
|||||||
type MockValidator struct{}
|
type MockValidator struct{}
|
||||||
|
|
||||||
func (m *MockValidator) Validate(ctx context.Context, u *url.URL, data []byte) (bool, definition.Error) {
|
func (m *MockValidator) Validate(ctx context.Context, u *url.URL, data []byte) (bool, definition.Error) {
|
||||||
return true, (definition.Error{})
|
return true, definition.Error{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mock New function for testing
|
// Mock New function for testing
|
||||||
func MockNew(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
|
func MockNew(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
|
||||||
|
// If the config has the error flag set to "true", return an error
|
||||||
if config["error"] == "true" {
|
if config["error"] == "true" {
|
||||||
return nil, errors.New("mock error")
|
return nil, errors.New("mock error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If schema_dir is set, print it out for debugging purposes
|
||||||
|
if schemaDir, ok := config["schema_dir"]; ok {
|
||||||
|
// You could add more logic to handle the schema_dir, for now we just print it
|
||||||
|
fmt.Println("Using schema directory:", schemaDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a map of mock validators
|
||||||
return map[string]definition.Validator{
|
return map[string]definition.Validator{
|
||||||
"validator1": &MockValidator{},
|
"validator1": &MockValidator{},
|
||||||
"validator2": &MockValidator{},
|
"validator2": &MockValidator{},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatorProvider_New(t *testing.T) {
|
// New method for ValidatorProvider, uses MockNew for creating mock validators
|
||||||
|
func New(ctx context.Context, config map[string]string) (map[string]definition.Validator, definition.Error) {
|
||||||
|
validators, err := MockNew(ctx, config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, definition.Error{Message: err.Error()}
|
||||||
|
}
|
||||||
|
return validators, definition.Error{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidatorProvider(t *testing.T) {
|
||||||
|
// Create a temporary directory for the schema
|
||||||
|
schemaDir, err := ioutil.TempDir("", "schemas")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create temp directory: %v", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(schemaDir)
|
||||||
|
|
||||||
|
// Create a temporary JSON schema file
|
||||||
|
schemaFile := fmt.Sprintf("%s/test_schema.json", schemaDir)
|
||||||
|
schemaContent := `{"type": "object", "properties": {"name": {"type": "string"}}}`
|
||||||
|
if err := ioutil.WriteFile(schemaFile, []byte(schemaContent), 0644); err != nil {
|
||||||
|
t.Fatalf("Failed to write schema file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define test cases
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
config map[string]string
|
config map[string]string
|
||||||
@@ -36,36 +71,23 @@ func TestValidatorProvider_New(t *testing.T) {
|
|||||||
expectedCount int
|
expectedCount int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Successful initialization",
|
name: "Valid schema directory",
|
||||||
config: map[string]string{"some_key": "some_value"},
|
config: map[string]string{"schema_dir": schemaDir}, // Use schemaDir instead of tempDir
|
||||||
expectedError: "",
|
expectedError: "",
|
||||||
expectedCount: 2, // Expecting 2 mock validators
|
expectedCount: 2, // Expecting 2 mock validators
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Error during initialization (mock error)",
|
name: "Invalid schema directory",
|
||||||
config: map[string]string{"error": "true"},
|
config: map[string]string{"schema_dir": "/invalid/dir"},
|
||||||
expectedError: "mock error",
|
expectedError: "failed to initialise validators: {/invalid/dir schema directory does not exist}",
|
||||||
expectedCount: 0,
|
expectedCount: 0,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "Empty config map",
|
|
||||||
config: map[string]string{},
|
|
||||||
expectedError: "",
|
|
||||||
expectedCount: 2, // Expecting 2 mock validators
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Non-empty config with invalid key",
|
|
||||||
config: map[string]string{"invalid_key": "invalid_value"},
|
|
||||||
expectedError: "",
|
|
||||||
expectedCount: 2, // Expecting 2 mock validators
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using the mock New function directly for testing
|
// Test using table-driven tests
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Create a ValidatorProvider with the mock New function
|
vp := ValidatorProvider{}
|
||||||
vp := &ValidatorProvider{}
|
|
||||||
validators, err := vp.New(context.Background(), tt.config)
|
validators, err := vp.New(context.Background(), tt.config)
|
||||||
|
|
||||||
// Check for expected error
|
// Check for expected error
|
||||||
|
|||||||
Reference in New Issue
Block a user