Skip to content

Commit

Permalink
export struct vars
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Nov 26, 2024
1 parent 1efbbe7 commit 3b3b21e
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 228 deletions.
40 changes: 20 additions & 20 deletions cmd/command_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

var executableName = "stellar-etl"
var update = flag.Bool("update", false, "update the golden files of this test")
var update = flag.Bool("update", false, "update the Golden files of this test")
var gotFolder = "testdata/got/"

type CloudStorage interface {
Expand Down Expand Up @@ -193,11 +193,11 @@ func WriteParquet(data []transform.SchemaParquet, path string, schema interface{
}

type CliTest struct {
name string
args []string
golden string
wantErr error
sortForComparison bool
Name string
Args []string
Golden string
WantErr error
SortForComparison bool
}

func indexOf(l []string, s string) int {
Expand All @@ -209,18 +209,18 @@ func indexOf(l []string, s string) int {
return -1
}

func RunCLITest(t *testing.T, test CliTest, goldenFolder string) {
func RunCLITest(t *testing.T, test CliTest, GoldenFolder string) {
flag.Parse()
t.Run(test.name, func(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
dir, err := os.Getwd()
assert.NoError(t, err)

idxOfOutputArg := indexOf(test.args, "-o")
idxOfOutputArg := indexOf(test.Args, "-o")
var testOutput []byte
var outLocation string
var stat os.FileInfo
if idxOfOutputArg > -1 {
outLocation = test.args[idxOfOutputArg+1]
outLocation = test.Args[idxOfOutputArg+1]
_, err = os.Stat(outLocation)
if err != nil {
// Check if the error is due to the file not existing
Expand All @@ -235,7 +235,7 @@ func RunCLITest(t *testing.T, test CliTest, goldenFolder string) {
}
}

cmd := exec.Command(path.Join(dir, executableName), test.args...)
cmd := exec.Command(path.Join(dir, executableName), test.Args...)
errOut, actualError := cmd.CombinedOutput()
if idxOfOutputArg > -1 {
stat, err = os.Stat(outLocation)
Expand Down Expand Up @@ -268,23 +268,23 @@ func RunCLITest(t *testing.T, test CliTest, goldenFolder string) {
// Since the CLI uses a logger to report errors, the final error message isn't the same as the errors thrown in code.
// Instead, it's wrapped in other os/system errors
// By reading the error text from the logger, we can extract the lower level error that the user would see
if test.golden == "" {
if test.Golden == "" {
errorMsg := fmt.Errorf(extractErrorMsg(string(errOut)))
assert.Equal(t, test.wantErr, errorMsg)
assert.Equal(t, test.WantErr, errorMsg)
return
}

assert.Equal(t, test.wantErr, actualError)
assert.Equal(t, test.WantErr, actualError)
actualString := string(testOutput)
if test.sortForComparison {
if test.SortForComparison {
trimmed := strings.Trim(actualString, "\n")
lines := strings.Split(trimmed, "\n")
sort.Strings(lines)
actualString = strings.Join(lines, "\n")
actualString = fmt.Sprintf("%s\n", actualString)
}

wantString, err := getGolden(t, goldenFolder+test.golden, actualString, *update)
wantString, err := getGolden(t, GoldenFolder+test.Golden, actualString, *update)
assert.NoError(t, err)
assert.Equal(t, wantString, actualString)
})
Expand All @@ -296,18 +296,18 @@ func extractErrorMsg(loggerOutput string) string {
return loggerOutput[errIndex : errIndex+endIndex]
}

func getGolden(t *testing.T, goldenFile string, actual string, update bool) (string, error) {
func getGolden(t *testing.T, GoldenFile string, actual string, update bool) (string, error) {
t.Helper()
f, err := os.OpenFile(goldenFile, os.O_RDWR|os.O_CREATE, 0644)
f, err := os.OpenFile(GoldenFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return "", err
}
defer f.Close()

// If the update flag is true, clear the current contents of the golden file and write the actual output
// If the update flag is true, clear the current contents of the Golden file and write the actual output
// This is useful for when new tests or added or functionality changes that breaks current tests
if update {
err := os.Truncate(goldenFile, 0)
err := os.Truncate(GoldenFile, 0)
if err != nil {
return "", err
}
Expand Down
32 changes: 16 additions & 16 deletions cmd/export_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
func TestExportAssets(t *testing.T) {
tests := []CliTest{
{
name: "assets from one ledger",
args: []string{"export_assets", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "one_ledger_assets.txt")},
golden: "one_ledger_assets.golden",
wantErr: nil,
Name: "assets from one ledger",
Args: []string{"export_assets", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "one_ledger_assets.txt")},
Golden: "one_ledger_assets.golden",
WantErr: nil,
},
{
name: "assets from 10 ledgers",
args: []string{"export_assets", "-s", "30822015", "-e", "30822025", "-o", GotTestDir(t, "10_ledgers_assets.txt")},
golden: "10_ledgers_assets.golden",
wantErr: nil,
Name: "assets from 10 ledgers",
Args: []string{"export_assets", "-s", "30822015", "-e", "30822025", "-o", GotTestDir(t, "10_ledgers_assets.txt")},
Golden: "10_ledgers_assets.golden",
WantErr: nil,
},
{
name: "range too large",
args: []string{"export_assets", "-s", "30822015", "-e", "30822025", "-l", "5", "-o", GotTestDir(t, "large_range_assets.txt")},
golden: "large_range_assets.golden",
wantErr: nil,
Name: "range too large",
Args: []string{"export_assets", "-s", "30822015", "-e", "30822025", "-l", "5", "-o", GotTestDir(t, "large_range_assets.txt")},
Golden: "large_range_assets.golden",
WantErr: nil,
},
{
name: "ledger with no assets",
args: []string{"export_assets", "-s", "10363513", "-e", "10363513", "-o", GotTestDir(t, "ledger_no_assets.txt")},
golden: "ledger_no_assets.golden",
wantErr: nil,
Name: "ledger with no assets",
Args: []string{"export_assets", "-s", "10363513", "-e", "10363513", "-o", GotTestDir(t, "ledger_no_assets.txt")},
Golden: "ledger_no_assets.golden",
WantErr: nil,
},
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/export_contract_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
func TestExportContractEvents(t *testing.T) {
tests := []CliTest{
{
name: "contract events from multiple ledger",
args: []string{"export_contract_events", "-s", "52271338", "-e", "52271350", "-o", GotTestDir(t, "large_range_ledger_txs.txt")},
golden: "large_range_ledger_txs.golden",
wantErr: nil,
Name: "contract events from multiple ledger",
Args: []string{"export_contract_events", "-s", "52271338", "-e", "52271350", "-o", GotTestDir(t, "large_range_ledger_txs.txt")},
Golden: "large_range_ledger_txs.golden",
WantErr: nil,
},
}

Expand Down
32 changes: 16 additions & 16 deletions cmd/export_effects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
func TestExportEffects(t *testing.T) {
tests := []CliTest{
{
name: "effects from one ledger",
args: []string{"export_effects", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "one_ledger_effects.txt")},
golden: "one_ledger_effects.golden",
wantErr: nil,
Name: "effects from one ledger",
Args: []string{"export_effects", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "one_ledger_effects.txt")},
Golden: "one_ledger_effects.golden",
WantErr: nil,
},
{
name: "effects from 10 ledgers",
args: []string{"export_effects", "-s", "30822015", "-e", "30822025", "-o", GotTestDir(t, "10_ledgers_effects.txt")},
golden: "10_ledgers_effects.golden",
wantErr: nil,
Name: "effects from 10 ledgers",
Args: []string{"export_effects", "-s", "30822015", "-e", "30822025", "-o", GotTestDir(t, "10_ledgers_effects.txt")},
Golden: "10_ledgers_effects.golden",
WantErr: nil,
},
{
name: "range too large",
args: []string{"export_effects", "-s", "25820678", "-e", "25821678", "-l", "5", "-o", GotTestDir(t, "large_range_effects.txt")},
golden: "large_range_effects.golden",
wantErr: nil,
Name: "range too large",
Args: []string{"export_effects", "-s", "25820678", "-e", "25821678", "-l", "5", "-o", GotTestDir(t, "large_range_effects.txt")},
Golden: "large_range_effects.golden",
WantErr: nil,
},
{
name: "ledger with no effects",
args: []string{"export_effects", "-s", "10363513", "-e", "10363513", "-o", GotTestDir(t, "ledger_no_effects.txt")},
golden: "ledger_no_effects.golden",
wantErr: nil,
Name: "ledger with no effects",
Args: []string{"export_effects", "-s", "10363513", "-e", "10363513", "-o", GotTestDir(t, "ledger_no_effects.txt")},
Golden: "ledger_no_effects.golden",
WantErr: nil,
},
}

Expand Down
96 changes: 48 additions & 48 deletions cmd/export_ledger_entry_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,85 @@ func TestExportChanges(t *testing.T) {

tests := []CliTest{
{
name: "unbounded range with no config",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-s", "100000"},
golden: "",
wantErr: fmt.Errorf("stellar-core needs a config file path when exporting ledgers continuously (endNum = 0)"),
Name: "unbounded range with no config",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-s", "100000"},
Golden: "",
WantErr: fmt.Errorf("stellar-core needs a config file path when exporting ledgers continuously (endNum = 0)"),
},
{
name: "0 batch size",
args: []string{"export_ledger_entry_changes", "-b", "0", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "100000", "-e", "164000"},
golden: "",
wantErr: fmt.Errorf("batch-size (0) must be greater than 0"),
Name: "0 batch size",
Args: []string{"export_ledger_entry_changes", "-b", "0", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "100000", "-e", "164000"},
Golden: "",
WantErr: fmt.Errorf("batch-size (0) must be greater than 0"),
},
{
name: "All changes from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265350", "-o", GotTestDir(t, "all/")},
golden: "all.golden",
wantErr: nil,
Name: "All changes from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265350", "-o", GotTestDir(t, "all/")},
Golden: "all.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 31 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Account changes from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "accounts/"), "--export-accounts", "true"},
golden: "accounts.golden",
wantErr: nil,
Name: "Account changes from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "accounts/"), "--export-accounts", "true"},
Golden: "accounts.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 38 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Claimable balance from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "claimable_balances/"), "--export-balances", "true"},
golden: "claimable_balances.golden",
wantErr: nil,
Name: "Claimable balance from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "claimable_balances/"), "--export-balances", "true"},
Golden: "claimable_balances.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 45 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "trustlines from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "trustlines/"), "--export-trustlines", "true"},
golden: "trustlines.golden",
wantErr: nil,
Name: "trustlines from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "trustlines/"), "--export-trustlines", "true"},
Golden: "trustlines.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 52 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Offers from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "offers/"), "--export-offers", "true"},
golden: "offers.golden",
wantErr: nil,
Name: "Offers from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "offers/"), "--export-offers", "true"},
Golden: "offers.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 59 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Pools from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "pools/"), "--export-pools", "true"},
golden: "pools.golden",
wantErr: nil,
Name: "Pools from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "49265302", "-e", "49265400", "-o", GotTestDir(t, "pools/"), "--export-pools", "true"},
Golden: "pools.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 66 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Contract code from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50666990", "-e", "50666999", "-o", GotTestDir(t, "contract_code/"), "--export-contract-code", "true"},
golden: "contract_code.golden",
wantErr: nil,
Name: "Contract code from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50666990", "-e", "50666999", "-o", GotTestDir(t, "contract_code/"), "--export-contract-code", "true"},
Golden: "contract_code.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 73 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Contract data from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "51340657", "-e", "51340757", "-o", GotTestDir(t, "contract_data/"), "--export-contract-data", "true"},
golden: "contract_data.golden",
wantErr: nil,
Name: "Contract data from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "51340657", "-e", "51340757", "-o", GotTestDir(t, "contract_data/"), "--export-contract-data", "true"},
Golden: "contract_data.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 80 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "Config setting from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50457424", "-e", "50457440", "-o", GotTestDir(t, "config_setting/"), "--export-config-settings", "true"},
golden: "config_setting.golden",
wantErr: nil,
Name: "Config setting from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50457424", "-e", "50457440", "-o", GotTestDir(t, "config_setting/"), "--export-config-settings", "true"},
Golden: "config_setting.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 87 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
{
name: "ttl from ledger entry",
args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50603521", "-e", "50603621", "-o", GotTestDir(t, "ttl/"), "--export-ttl", "true"},
golden: "ttl.golden",
wantErr: nil,
Name: "ttl from ledger entry",
Args: []string{"export_ledger_entry_changes", "-x", coreExecutablePath, "-c", coreConfigPath, "-s", "50603521", "-e", "50603621", "-o", GotTestDir(t, "ttl/"), "--export-ttl", "true"},
Golden: "ttl.golden",
WantErr: nil,
sortForComparison: true,

Check failure on line 94 in cmd/export_ledger_entry_changes_test.go

View workflow job for this annotation

GitHub Actions / pre-commit

unknown field sortForComparison in struct literal of type struct{Name string; Args []string; Golden string; WantErr error; SortForComparison bool}, but does have SortForComparison
},
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/export_ledger_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
func TestExportLedgerTransaction(t *testing.T) {
tests := []CliTest{
{
name: "Transactions from one ledger",
args: []string{"export_ledger_transaction", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "ledger_transactions.txt")},
golden: "ledger_transactions.golden",
wantErr: nil,
Name: "Transactions from one ledger",
Args: []string{"export_ledger_transaction", "-s", "30820015", "-e", "30820015", "-o", GotTestDir(t, "ledger_transactions.txt")},
Golden: "ledger_transactions.golden",
WantErr: nil,
},
}

Expand Down
Loading

0 comments on commit 3b3b21e

Please sign in to comment.