Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Allow contract deployments to service account #325

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ func (e *EmulatorBackend) LoadSnapshot(name string) error {
// Creates the number of predefined accounts that will be used
// for deploying the contracts under testing.
func (e *EmulatorBackend) bootstrapAccounts() {
serviceAcc, err := e.ServiceAccount()
if err != nil {
panic(err)
}
e.accounts[serviceAcc.Address] = serviceAcc

for i := 0; i < initialAccountsNumber; i++ {
_, err := e.CreateAccount()
if err != nil {
Expand Down
99 changes: 99 additions & 0 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3869,6 +3869,31 @@ func TestServiceAccount(t *testing.T) {
require.NoError(t, result.Error)
})

t.Run("retrieve from Test.getAccount() function", func(t *testing.T) {
t.Parallel()

const testCode = `
import Test

access(all)
fun testGetServiceAccount() {
// Act
let account = Test.getAccount(0x0000000000000001)

// Assert
Test.assertEqual(Type<Address>(), account.address.getType())
Test.assertEqual(Type<PublicKey>(), account.publicKey.getType())
Test.assertEqual(Address(0x0000000000000001), account.address)
}
`

runner := NewTestRunner()

result, err := runner.RunTest(testCode, "testGetServiceAccount")
require.NoError(t, err)
require.NoError(t, result.Error)
})

t.Run("run scripts and transactions with service account", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3951,6 +3976,80 @@ func TestServiceAccount(t *testing.T) {
require.NoError(t, err)
require.NoError(t, result.Error)
})

t.Run("deploy contract to service account", func(t *testing.T) {
t.Parallel()

const code = `
import Test
import "FooContract"

access(all)
fun setup() {
let err = Test.deployContract(
name: "FooContract",
path: "./FooContract",
arguments: []
)

Test.expect(err, Test.beNil())
}

access(all)
fun test() {
Test.assertEqual("hello from Foo", FooContract.sayHello())
}
`

const fooContract = `
access(all)
contract FooContract {
init() {}

access(all)
fun sayHello(): String {
return "hello from Foo"
}
}
`

fileResolver := func(path string) (string, error) {
switch path {
case "./FooContract":
return fooContract, nil
default:
return "", fmt.Errorf("cannot find file path: %s", path)
}
}

importResolver := func(location common.Location) (string, error) {
switch location := location.(type) {
case common.AddressLocation:
if location.Name == "FooContract" {
return fooContract, nil
}
case common.StringLocation:
if location == "FooContract" {
return fooContract, nil
}
}

return "", fmt.Errorf("cannot find import location: %s", location.ID())
}

contracts := map[string]common.Address{
"FooContract": {0, 0, 0, 0, 0, 0, 0, 1},
}

runner := NewTestRunner().
WithImportResolver(importResolver).
WithFileResolver(fileResolver).
WithContracts(contracts)

result, err := runner.RunTest(code, "test")
require.NoError(t, err)
require.NoError(t, result.Error)
})
}

func TestCoverageReportForUnitTests(t *testing.T) {
Expand Down
Loading