From af2b71abf04bb5ccd986f46cd818287af3600ae3 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 7 Mar 2024 14:25:07 +0530 Subject: [PATCH] Add cmd and utils unit tests and a CI workflow --- .gitea/workflows/test-unit.yml | 19 +++++++++++++++++++ Makefile | 3 +++ cmd/laconic2d/cmd_test.go | 28 ++++++++++++++++++++++++++++ utils/json_test.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 .gitea/workflows/test-unit.yml create mode 100644 cmd/laconic2d/cmd_test.go create mode 100644 utils/json_test.go diff --git a/.gitea/workflows/test-unit.yml b/.gitea/workflows/test-unit.yml new file mode 100644 index 00000000..fc04eadb --- /dev/null +++ b/.gitea/workflows/test-unit.yml @@ -0,0 +1,19 @@ +name: Unit Tests +on: + pull_request: + push: + branches: + - main + +jobs: + test-unit: + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v3 + with: + go-version: 1.21 + check-latest: true + - uses: actions/checkout@v3 + - name: Test + run: | + make test-unit diff --git a/Makefile b/Makefile index a89331a3..c387948a 100644 --- a/Makefile +++ b/Makefile @@ -103,3 +103,6 @@ test-integration: test-e2e: $(MAKE) -C tests test-e2e + +test-unit: + go test ./utils/... ./cmd/... -mod=readonly -test.v diff --git a/cmd/laconic2d/cmd_test.go b/cmd/laconic2d/cmd_test.go new file mode 100644 index 00000000..dc9ecd5a --- /dev/null +++ b/cmd/laconic2d/cmd_test.go @@ -0,0 +1,28 @@ +package main_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/client/flags" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" + + "git.vdb.to/cerc-io/laconic2d/app" + "git.vdb.to/cerc-io/laconic2d/cmd/laconic2d/cmd" +) + +func TestInitCmd(t *testing.T) { + rootCmd := cmd.NewRootCmd() + rootCmd.SetArgs([]string{ + "init", // Test the init cmd + "localtestnet", // Moniker + fmt.Sprintf("--%s=%s", cli.FlagOverwrite, "true"), // Overwrite genesis.json, in case it already exists + fmt.Sprintf("--%s=%s", flags.FlagChainID, "laconic_9000-1"), + }) + + err := svrcmd.Execute(rootCmd, "", app.DefaultNodeHome) + require.NoError(t, err) +} diff --git a/utils/json_test.go b/utils/json_test.go new file mode 100644 index 00000000..921a7f05 --- /dev/null +++ b/utils/json_test.go @@ -0,0 +1,33 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAndValidateCIDGeneration(t *testing.T) { + testCases := []struct { + name string + content string + expected string + }{ + { + "empty string", "", "", + }, + { + "empty json", "{}", "bafyreigbtj4x7ip5legnfznufuopl4sg4knzc2cof6duas4b3q2fy6swua", + }, + + { + "test record", "{\"build_artifact_cid\":\"QmP8jTG1m9GSDJLCbeWhVSVgEzCPPwXRdCRuJtQ5Tz9Kc9\",\"repo_registration_record_cid\":\"QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D\",\"tls_cert_cid\":\"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR\",\"type\":\"WebsiteRegistrationRecord\",\"url\":\"https://cerc.io\",\"version\":\"0.0.1\"}", + "bafyreiek4hnoqmits66bjyxswapplweuoqe4en2ux6u772o4y3askpd3ny", + }, + } + + for _, tc := range testCases { + newImpl, err := CIDFromJSONBytes([]byte(tc.content)) + require.NoError(t, err) + require.Equal(t, tc.expected, newImpl) + } +}