Skip to content

Commit

Permalink
Use mock client in e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Nov 22, 2024
1 parent f0de649 commit 1440e16
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 96 deletions.
14 changes: 13 additions & 1 deletion cmd/export_external_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/stellar/go/utils/apiclient"
"github.com/stellar/stellar-etl/internal/input"
"github.com/stellar/stellar-etl/internal/transform"
"github.com/stellar/stellar-etl/internal/utils"
Expand All @@ -20,14 +21,24 @@ var externalDataCmd = &cobra.Command{
path := utils.MustBucketFlags(cmd.Flags(), cmdLogger)
provider := utils.MustProviderFlags(cmd.Flags(), cmdLogger)
cloudStorageBucket, cloudCredentials, cloudProvider := utils.MustCloudStorageFlags(cmd.Flags(), cmdLogger)
isTest, err := cmd.Flags().GetBool("testnet")
if err != nil {
cmdLogger.Fatal("could not get testnet boolean: ", err)
}

outFile := mustOutFile(path)
numFailures := 0
totalNumBytes := 0

switch provider {
case "retool":
entities, err := input.GetEntityData[utils.RetoolEntityDataTransformInput](nil, provider, timestampArgs.StartTime, timestampArgs.EndTime)
var client *apiclient.APIClient
if isTest {
client = input.GetMockClient(provider)
} else {
client = nil
}
entities, err := input.GetEntityData[utils.RetoolEntityDataTransformInput](client, provider, timestampArgs.StartTime, timestampArgs.EndTime)
if err != nil {
cmdLogger.Fatal("could not read entity data: ", err)
}
Expand Down Expand Up @@ -66,6 +77,7 @@ func init() {
utils.AddCloudStorageFlags(externalDataCmd.Flags())
utils.AddTimestampRangeFlags(externalDataCmd.Flags())
utils.AddProviderFlags(externalDataCmd.Flags())
utils.AddTestFlags(externalDataCmd.Flags())
externalDataCmd.MarkFlagRequired("provider")
externalDataCmd.MarkFlagRequired("start-time")
externalDataCmd.MarkFlagRequired("end-time")
Expand Down
2 changes: 1 addition & 1 deletion cmd/export_external_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TestExportExternalData(t *testing.T) {
tests := []cliTest{
{
name: "external data from retool",
args: []string{"export_external_data", "--provider", "retool", "--start-time", "", "--end-time", "", "-o", gotTestDir(t, "external_data_retool.txt")},
args: []string{"export_external_data", "--provider", "retool", "--start-time", "", "--end-time", "", "-o", gotTestDir(t, "external_data_retool.txt"), "--testnet"},
golden: "external_data_retool.golden",
wantErr: nil,
},
Expand Down
95 changes: 95 additions & 0 deletions internal/input/external_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package input
import (
"fmt"
"log"
"net/http"
"net/url"

"github.com/stellar/go/support/http/httptest"
"github.com/stellar/go/utils/apiclient"
"github.com/stellar/stellar-etl/internal/utils"
)
Expand Down Expand Up @@ -80,3 +82,96 @@ func GetEntityData[T any](client *apiclient.APIClient, provider string, startTim

return dataSlice, nil
}

func GetMockClient(provider string) *apiclient.APIClient {
var mockResponses []httptest.ResponseData
switch provider {
case "retool":
mockResponses = []httptest.ResponseData{
{
Status: http.StatusOK,
Body: `[
{
"id": 16,
"created_at": 1706749912776,
"updated_at": null,
"custodial": true,
"non_custodial": true,
"home_domains_id": 240,
"name": "El Dorado",
"description": "",
"website_url": "",
"sdp_enabled": false,
"soroban_enabled": false,
"notes": "",
"verified": false,
"fee_sponsor": false,
"account_sponsor": false,
"live": true,
"status": "live",
"_home_domain": {
"id": 240,
"created_at": 1706749903897,
"home_domain": "eldorado.io",
"updated_at": 1706749903897
},
"_app_geographies_details": [
{
"id": 39,
"apps_id": 16,
"created_at": 1707887845605,
"geographies_id": [
{
"id": 176,
"created_at": 1691020699576,
"updated_at": 1706650713745,
"name": "Argentina",
"official_name": "The Argentine Republic"
},
{
"id": 273,
"created_at": 1691020699834,
"updated_at": 1706650708355,
"name": "Brazil",
"official_name": "The Federative Republic of Brazil"
}
],
"retail": false,
"enterprise": false
}
],
"_app_to_ramps_integrations": [
{
"id": 18,
"created_at": 1707617027154,
"anchors_id": 28,
"apps_id": 16,
"_anchor": {
"id": 28,
"created_at": 1705423531705,
"name": "MoneyGram",
"updated_at": 1706596979487,
"home_domains_id": 203
}
}
]
}
]`,
Header: nil,
},
}
default:
panic("unsupported provider: " + provider)
}
hmock := httptest.NewClient()
providerConfig := GetProviderConfig(provider)

hmock.On("GET", fmt.Sprintf("%s/%s", providerConfig.BaseURL, providerConfig.Endpoint)).
ReturnMultipleResults(mockResponses)

mockClient := &apiclient.APIClient{
BaseURL: providerConfig.BaseURL,
HTTP: hmock,
}
return mockClient
}
97 changes: 3 additions & 94 deletions internal/input/external_data_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
package input

import (
"fmt"
"net/http"
"testing"

"github.com/stellar/go/support/http/httptest"
"github.com/stellar/go/utils/apiclient"
"github.com/stellar/stellar-etl/internal/utils"
"github.com/stretchr/testify/assert"
)

func getMockClient(provider string, mockResponses []httptest.ResponseData) *apiclient.APIClient {
hmock := httptest.NewClient()
providerConfig := GetProviderConfig(provider)

hmock.On("GET", fmt.Sprintf("%s/%s", providerConfig.BaseURL, providerConfig.Endpoint)).
ReturnMultipleResults(mockResponses)

mockClient := &apiclient.APIClient{
BaseURL: providerConfig.BaseURL,
HTTP: hmock,
}
return mockClient
}

func getEntityDataHelper[T any](t *testing.T, provider string, mockResponses []httptest.ResponseData, expected []T) {
mockClient := getMockClient(provider, mockResponses)
func getEntityDataHelper[T any](t *testing.T, provider string, expected []T) {
mockClient := GetMockClient(provider)
result, err := GetEntityData[T](mockClient, provider, "", "")
if err != nil {
t.Fatalf("Error calling GetEntityData: %v", err)
Expand All @@ -36,79 +18,6 @@ func getEntityDataHelper[T any](t *testing.T, provider string, mockResponses []h
}

func TestGetEntityDataForRetool(t *testing.T) {
mockResponses := []httptest.ResponseData{
{
Status: http.StatusOK,
Body: `[
{
"id": 16,
"created_at": 1706749912776,
"updated_at": null,
"custodial": true,
"non_custodial": true,
"home_domains_id": 240,
"name": "El Dorado",
"description": "",
"website_url": "",
"sdp_enabled": false,
"soroban_enabled": false,
"notes": "",
"verified": false,
"fee_sponsor": false,
"account_sponsor": false,
"live": true,
"status": "live",
"_home_domain": {
"id": 240,
"created_at": 1706749903897,
"home_domain": "eldorado.io",
"updated_at": 1706749903897
},
"_app_geographies_details": [
{
"id": 39,
"apps_id": 16,
"created_at": 1707887845605,
"geographies_id": [
{
"id": 176,
"created_at": 1691020699576,
"updated_at": 1706650713745,
"name": "Argentina",
"official_name": "The Argentine Republic"
},
{
"id": 273,
"created_at": 1691020699834,
"updated_at": 1706650708355,
"name": "Brazil",
"official_name": "The Federative Republic of Brazil"
}
],
"retail": false,
"enterprise": false
}
],
"_app_to_ramps_integrations": [
{
"id": 18,
"created_at": 1707617027154,
"anchors_id": 28,
"apps_id": 16,
"_anchor": {
"id": 28,
"created_at": 1705423531705,
"name": "MoneyGram",
"updated_at": 1706596979487,
"home_domains_id": 203
}
}
]
}
]`,
Header: nil,
},
}
expected := []utils.RetoolEntityDataTransformInput{
{
ID: 16,
Expand Down Expand Up @@ -175,5 +84,5 @@ func TestGetEntityDataForRetool(t *testing.T) {
},
},
}
getEntityDataHelper[utils.RetoolEntityDataTransformInput](t, "retool", mockResponses, expected)
getEntityDataHelper[utils.RetoolEntityDataTransformInput](t, "retool", expected)
}
4 changes: 4 additions & 0 deletions internal/utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ func AddProviderFlags(flags *pflag.FlagSet) {
flags.StringP("provider", "p", "", "Third party provider name. Example: retool, github")
}

func AddTestFlags(flags *pflag.FlagSet) {
flags.Bool("testnet", false, "If set, will connect to Testnet instead of Mainnet.")
}

// TODO: https://stellarorg.atlassian.net/browse/HUBBLE-386 better flags/params
// Some flags should be named better
type FlagValues struct {
Expand Down
1 change: 1 addition & 0 deletions testdata/external_data/external_data_retool.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_sponsor":false,"app_geographies":["Argentina","Brazil"],"created_at":1706749912776,"custodial":true,"description":"","fee_sponsor":false,"home_domain":"eldorado.io","home_domains_id":240,"id":16,"live":true,"name":"El Dorado","non_custodial":true,"notes":"","ramps":["MoneyGram"],"sdp_enabled":false,"soroban_enabled":false,"status":"live","updated_at":null,"verified":false,"website_url":""}

0 comments on commit 1440e16

Please sign in to comment.