-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement routes overwrite core for the /quote endpoint (#16)
* fix: proper error message when token in is too small * go work sum * feat: config path * feat: full JSON config * lint * feat: implement routes overwrite core for the /quote endpoint * remove go work sum * fix test
- Loading branch information
Showing
17 changed files
with
670 additions
and
2,315 deletions.
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 |
---|---|---|
|
@@ -6,3 +6,6 @@ redis-cache | |
bin | ||
|
||
build | ||
|
||
# Go | ||
go.work.sum |
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
This file was deleted.
Oops, something went wrong.
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
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,5 @@ | ||
package cache | ||
|
||
import "time" | ||
|
||
const NoExpiration time.Duration = 0 |
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,61 @@ | ||
package cache | ||
|
||
import "time" | ||
|
||
type RoutesOverwrite struct { | ||
cache *Cache | ||
} | ||
|
||
const noExpiration time.Duration = 0 | ||
|
||
// NewRoutesOverwrite creates a new routes overwrite container. | ||
func NewRoutesOverwrite() *RoutesOverwrite { | ||
return &RoutesOverwrite{ | ||
cache: New(), | ||
} | ||
} | ||
|
||
// NewNoOpRoutesOverwrite creates a new routes overwrite container that does nothing. | ||
func NewNoOpRoutesOverwrite() *RoutesOverwrite { | ||
return &RoutesOverwrite{} | ||
} | ||
|
||
// CreateRoutesOverwrite creates a new routes overwrite container depending on the value of isRoutesOverwriteEnabled. | ||
// If isRoutesOverwriteEnabled is true, it will return a new routes overwrite container. | ||
// If isRoutesOverwriteEnabled is false, it will return a new no-op routes overwrite container. | ||
func CreateRoutesOverwrite(isRoutesOverwriteEnabled bool) *RoutesOverwrite { | ||
if isRoutesOverwriteEnabled { | ||
return NewRoutesOverwrite() | ||
} | ||
return NewNoOpRoutesOverwrite() | ||
} | ||
|
||
// Set adds an item to the cache with a specified key and value. | ||
// If the routes overwrite cache is not enabled, it will silently ignore the call. | ||
func (r *RoutesOverwrite) Set(key string, value interface{}) { | ||
if r.cache == nil { | ||
return | ||
} | ||
|
||
r.cache.Set(key, value, noExpiration) | ||
} | ||
|
||
// Get retrieves the value associated with a key from the cache. Returns false if the key does not exist. | ||
// If the routes overwrite cache is not enabled, it will silently ignore the call. | ||
func (r *RoutesOverwrite) Get(key string) (interface{}, bool) { | ||
if r.cache == nil { | ||
return nil, false | ||
} | ||
|
||
return r.cache.Get(key) | ||
} | ||
|
||
// Delete removes an item from the cache. | ||
// If the routes overwrite cache is not enabled, it will silently ignore the call. | ||
func (r *RoutesOverwrite) Delete(key string) { | ||
if r.cache == nil { | ||
return | ||
} | ||
|
||
r.cache.Delete(key) | ||
} |
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,157 @@ | ||
package cache_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/osmosis-labs/sqs/domain/cache" | ||
) | ||
|
||
func TestRoutesOverwrite_Set(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
isRoutesOverwriteEnabled bool | ||
key string | ||
value interface{} | ||
expectedExists bool | ||
expectedValue interface{} | ||
}{ | ||
{ | ||
name: "Cache Enabled - Set Value", | ||
isRoutesOverwriteEnabled: true, | ||
key: "key1", | ||
value: "value1", | ||
expectedExists: true, | ||
expectedValue: "value1", | ||
}, | ||
{ | ||
name: "Cache Disabled - Set Value", | ||
isRoutesOverwriteEnabled: false, | ||
key: "key2", | ||
value: "value2", | ||
expectedExists: false, | ||
expectedValue: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := cache.CreateRoutesOverwrite(tt.isRoutesOverwriteEnabled) | ||
r.Set(tt.key, tt.value) | ||
|
||
// Additional assertions | ||
value, exists := r.Get(tt.key) | ||
if exists != tt.expectedExists { | ||
t.Errorf("Expected key %s to exist: %v, got: %v", tt.key, tt.expectedExists, exists) | ||
} | ||
|
||
if value != tt.expectedValue { | ||
t.Errorf("Expected value for key %s: %v, got: %v", tt.key, tt.expectedValue, value) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRoutesOverwrite_Get(t *testing.T) { | ||
// Assuming Set method is working correctly. | ||
// Setting up some initial data for testing Get method. | ||
|
||
tests := []struct { | ||
name string | ||
isRoutesOverwriteEnabled bool | ||
key string | ||
expectedValue interface{} | ||
expectedExists bool | ||
}{ | ||
{ | ||
name: "Cache Enabled - Key Exists", | ||
isRoutesOverwriteEnabled: true, | ||
key: "key1", | ||
expectedValue: "value1", | ||
expectedExists: true, | ||
}, | ||
{ | ||
name: "Cache Enabled - Key Does Not Exist", | ||
isRoutesOverwriteEnabled: true, | ||
key: "key3", | ||
expectedValue: nil, | ||
expectedExists: false, | ||
}, | ||
{ | ||
name: "Cache Disabled - Key Exists", | ||
isRoutesOverwriteEnabled: false, | ||
key: "key2", | ||
expectedValue: nil, | ||
expectedExists: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := cache.CreateRoutesOverwrite(tt.isRoutesOverwriteEnabled) | ||
r.Set("key1", "value1") | ||
r.Set("key2", "value2") | ||
|
||
value, exists := r.Get(tt.key) | ||
|
||
if value != tt.expectedValue || exists != tt.expectedExists { | ||
t.Errorf("Got (%v, %v), expected (%v, %v)", value, exists, tt.expectedValue, tt.expectedExists) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRoutesOverwrite_Delete(t *testing.T) { | ||
// Assuming Set method is working correctly. | ||
// Setting up some initial data for testing Delete method. | ||
|
||
r := cache.NewRoutesOverwrite() | ||
r.Set("key1", "value1") | ||
r.Set("key2", "value2") | ||
|
||
tests := []struct { | ||
name string | ||
isRoutesOverwriteEnabled bool | ||
key string | ||
expectedExists bool | ||
expectedValue interface{} | ||
}{ | ||
{ | ||
name: "Cache Enabled - Delete Key", | ||
isRoutesOverwriteEnabled: true, | ||
key: "key1", | ||
expectedExists: false, | ||
expectedValue: nil, | ||
}, | ||
{ | ||
name: "Cache Enabled - Delete Non-Existing Key", | ||
isRoutesOverwriteEnabled: true, | ||
key: "key3", | ||
expectedExists: false, | ||
expectedValue: nil, | ||
}, | ||
{ | ||
name: "Cache Disabled - Delete Key", | ||
isRoutesOverwriteEnabled: false, | ||
key: "key2", | ||
expectedExists: false, | ||
expectedValue: nil, | ||
}, | ||
// Add more test cases as needed | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r.Delete(tt.key) | ||
|
||
// Additional assertions | ||
value, exists := r.Get(tt.key) | ||
if exists != tt.expectedExists { | ||
t.Errorf("Expected key %s to exist: %v, got: %v", tt.key, tt.expectedExists, exists) | ||
} | ||
|
||
if value != tt.expectedValue { | ||
t.Errorf("Expected value for key %s: %v, got: %v", tt.key, tt.expectedValue, value) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.