forked from liftedinit/yaci
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit tests & fixes (liftedinit#14)
- Loading branch information
Showing
24 changed files
with
677 additions
and
191 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
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,21 @@ | ||
package yaci_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/liftedinit/yaci/cmd/yaci" | ||
) | ||
|
||
func TestExtractCmd(t *testing.T) { | ||
// --stop and --live are mutually exclusive | ||
_, err := executeCommand(yaci.RootCmd, "extract", "json", "foobar", "--live", "--stop", "10") | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "if any flags in the group [live stop] are set none of the others can be; [live stop] were all set") | ||
|
||
// Show help | ||
output, err := executeCommand(yaci.RootCmd, "extract") | ||
assert.NoError(t, err) | ||
assert.Contains(t, output, "Extract chain data to") | ||
} |
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,63 @@ | ||
package yaci_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/gruntwork-io/terratest/modules/docker" | ||
"github.com/liftedinit/yaci/cmd/yaci" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
DockerWorkingDirectory = "../../docker" | ||
GRPCEndpoint = "localhost:9090" | ||
RestEndpoint = "localhost:3000" | ||
PsqlConnectionString = "postgres://postgres:foobar@localhost/postgres" | ||
) | ||
|
||
var ( | ||
RestTxEndpoint = fmt.Sprintf("http://%s/transactions", RestEndpoint) | ||
) | ||
|
||
func TestPostgres(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode.") | ||
} | ||
|
||
// Start the infrastructure using Docker Compose. | ||
// The infrastructure is defined in the `infra.yml` file. | ||
opts := &docker.Options{WorkingDir: DockerWorkingDirectory} | ||
defer docker.RunDockerCompose(t, opts, "-f", "infra.yml", "down", "-v") | ||
_, err := docker.RunDockerComposeE(t, opts, "-f", "infra.yml", "up", "-d", "--wait") | ||
require.NoError(t, err) | ||
|
||
// Run the YACI command to extract the chain data to a PostgreSQL database | ||
cmd := yaci.RootCmd | ||
cmd.SetArgs([]string{"extract", "postgres", GRPCEndpoint, "-p", PsqlConnectionString, "-k"}) | ||
|
||
// Execute the command. This will extract the chain data to a PostgreSQL database up to the latest block. | ||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
// Verify that the chain data has been extracted to the PostgreSQL database using the REST API | ||
client := resty.New() | ||
resp, err := client. | ||
R(). | ||
SetHeader("Accept", "application/json"). | ||
Get(RestTxEndpoint) | ||
require.NoError(t, err) | ||
require.Equal(t, 200, resp.StatusCode()) | ||
require.NotEmpty(t, resp.Body()) | ||
|
||
// Parse the response JSON body | ||
var transactions []map[string]interface{} | ||
err = json.Unmarshal(resp.Body(), &transactions) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, transactions) | ||
|
||
// The number of transactions is 6 as defined in the `infra.yml` file under the `manifest-ledger-tx` service | ||
require.Len(t, transactions, 6) | ||
} |
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,33 @@ | ||
package yaci_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/liftedinit/yaci/cmd/yaci" | ||
) | ||
|
||
func executeCommand(root *cobra.Command, args ...string) (output string, err error) { | ||
buf := new(bytes.Buffer) | ||
root.SetOut(buf) | ||
root.SetErr(buf) | ||
root.SetArgs(args) | ||
|
||
_, err = root.ExecuteC() | ||
return buf.String(), err | ||
} | ||
|
||
func TestRootCmd(t *testing.T) { | ||
// Show help | ||
output, err := executeCommand(yaci.RootCmd) | ||
assert.NoError(t, err) | ||
assert.Contains(t, output, "yaci connects to a gRPC server and extracts blockchain data.") | ||
|
||
// Test invalid logLevel | ||
output, err = executeCommand(yaci.RootCmd, "version", "--logLevel", "invalid") | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "invalid log level: invalid. Valid log levels are: debug|error|info|warn") | ||
} |
File renamed without changes.
Oops, something went wrong.