Skip to content

Commit

Permalink
Merge pull request #1582 from hyperledger/add_coverage
Browse files Browse the repository at this point in the history
Add more tests for missing coverage
  • Loading branch information
EnriqueL8 authored Sep 17, 2024
2 parents 35db032 + 97e0169 commit 37efd0f
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 52 deletions.
106 changes: 106 additions & 0 deletions ffconfig/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright © 2022 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

var configPath string = "../test/data/config/firefly.core.yaml"

func TestMainFail(t *testing.T) {
// Run the crashing code when FLAG is set
if os.Getenv("FLAG") == "1" {
main()
return
}
// Run the test in a subprocess
cmd := exec.Command(os.Args[0], "-test.run=TestMainFail")
cmd.Env = append(os.Environ(), "FLAG=1")
err := cmd.Run()

// Cast the error as *exec.ExitError and compare the result
e, ok := err.(*exec.ExitError)
expectedErrorString := "exit status 1"
assert.Equal(t, true, ok)
assert.Equal(t, expectedErrorString, e.Error())
}

func TestConfigMigrateRootCmdErrorNoArgs(t *testing.T) {
rootCmd.SetArgs([]string{})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.Error(t, err)
assert.Regexp(t, "a command is required", err)
}

func TestConfigMigrateCmdMissingConfig(t *testing.T) {
rootCmd.SetArgs([]string{"migrate"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.Error(t, err)
assert.Regexp(t, "no such file or directory", err)
}

func TestConfigMigrateCmd(t *testing.T) {
rootCmd.SetArgs([]string{"migrate", "-f", configPath})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.NoError(t, err)
}

func TestMain(t *testing.T) {
// Run the exiting code when FLAG is set
if os.Getenv("FLAG") == "0" {
rootCmd.SetArgs([]string{"migrate", "-f", configPath})
main()
return
}

// Run the test in a subprocess
cmd := exec.Command(os.Args[0], "-test.run=TestMain")
cmd.Env = append(os.Environ(), "FLAG=0")
err := cmd.Run()

// Cast the error as *exec.ExitError and compare the result
_, ok := err.(*exec.ExitError)
assert.Equal(t, false, ok)
}

func TestConfigMigrateCmdWriteOutput(t *testing.T) {
tmpDir, err := os.MkdirTemp(os.TempDir(), "out")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)

rootCmd.SetArgs([]string{"migrate", "-f", configPath, "-o", fmt.Sprintf(tmpDir, "out.config")})
defer rootCmd.SetArgs([]string{})
err = rootCmd.Execute()
assert.NoError(t, err)
}

func TestConfigMigrateCmdBadVersion(t *testing.T) {
rootCmd.SetArgs([]string{"migrate", "-f", configPath, "--from", "badversion"})
defer rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
assert.Error(t, err)
assert.Regexp(t, "bad 'from' version", err)
}
60 changes: 60 additions & 0 deletions internal/blockchain/bifactory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bifactory

import (
"context"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestGetPluginUnknown(t *testing.T) {
ctx := context.Background()
_, err := GetPlugin(ctx, "foo")
assert.Error(t, err)
assert.Regexp(t, "FF10110", err)
}

func TestGetPluginEthereum(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "ethereum")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

func TestGetPluginFabric(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "fabric")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

func TestGetPluginTezos(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "tezos")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

var root = config.RootSection("di")

func TestInitConfig(t *testing.T) {
conf := root.SubArray("plugins")
InitConfig(conf)
}
52 changes: 0 additions & 52 deletions internal/coremsgs/es/es_struct_descriptions.go

This file was deleted.

53 changes: 53 additions & 0 deletions internal/database/difactory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package difactory

import (
"context"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestGetPluginUnknown(t *testing.T) {
ctx := context.Background()
_, err := GetPlugin(ctx, "foo")
assert.Error(t, err)
assert.Regexp(t, "FF10122", err)
}

func TestGetPluginPostgres(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "postgres")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

func TestGetPluginSQLite(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "sqlite3")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

var root = config.RootSection("di")

func TestInitConfig(t *testing.T) {
conf := root.SubArray("plugins")
InitConfig(conf)
}
46 changes: 46 additions & 0 deletions internal/dataexchange/dxfactory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dxfactory

import (
"context"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestGetPluginUnknown(t *testing.T) {
ctx := context.Background()
_, err := GetPlugin(ctx, "foo")
assert.Error(t, err)
assert.Regexp(t, "FF10213", err)
}

func TestGetPlugin(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "ffdx")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

var root = config.RootSection("di")

func TestInitConfig(t *testing.T) {
conf := root.SubArray("plugins")
InitConfig(conf)
}
59 changes: 59 additions & 0 deletions internal/events/eifactory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package eifactory

import (
"context"
"testing"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/stretchr/testify/assert"
)

func TestGetPluginUnknown(t *testing.T) {
ctx := context.Background()
_, err := GetPlugin(ctx, "foo")
assert.Error(t, err)
assert.Regexp(t, "FF10172", err)
}

func TestGetPluginWebSockets(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "websockets")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

func TestGetPluginWebHooks(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "webhooks")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

func TestGetPluginEvents(t *testing.T) {
ctx := context.Background()
plugin, err := GetPlugin(ctx, "system")
assert.NoError(t, err)
assert.NotNil(t, plugin)
}

var root = config.RootSection("di")

func TestInitConfig(t *testing.T) {
InitConfig(root)
}
Loading

0 comments on commit 37efd0f

Please sign in to comment.