This repository was archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
This repository was archived by the owner on Jul 11, 2023. It is now read-only.
AI 评测:单元测试 #3
Copy link
Copy link
Open
Description
定义测试用例
-
描述: 测试类型转换
-
特点:
- 无任何其他库依赖
- 不涉及系统调用,如文件读写
- 不涉及网络调用
-
结果:
- 基于chatgpt一次性生成
- 覆盖率100%
- [TestCase2]
-
描述: 测试LocalWallet功能
-
特点:
- 涉及系统调用,需读写文件
-
结果(chatgpt)
查看详细单测代码
package account
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAccount(t *testing.T) {
// Create a temporary directory for the wallet
tempDir, err := ioutil.TempDir("", "wallet")
assert.NoError(t, err)
defer os.RemoveAll(tempDir)
// Create a new local wallet
wallet, err := NewLocalWallet(tempDir)
assert.NoError(t, err)
// Generate a new account
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.NoError(t, err)
publicKey := privateKey.PublicKey
account := &Account{
Address: "testaccount",
PrivateKey: x509.MarshalECPrivateKey(privateKey),
signer: *privateKey,
}
// Store the account in the wallet
err = wallet.StoreAccount(account)
assert.NoError(t, err)
// Retrieve the account from the wallet
loadedAccount, err := wallet.GetAccount("testaccount")
assert.NoError(t, err)
assert.Equal(t, account.Address, loadedAccount.Address)
assert.Equal(t, account.PrivateKey, loadedAccount.PrivateKey)
assert.Equal(t, account.signer, loadedAccount.signer)
// Verify the stored account file
filePath := tempDir + "/testaccount"
assert.FileExists(t, filePath)
fileBytes, err := ioutil.ReadFile(filePath)
assert.NoError(t, err)
assert.Equal(t, string(fileBytes), `{"address":"testaccount","privKey":"`+string(account.PrivateKey)+`"}`)
// List the accounts in the wallet
accounts, err := wallet.ListAccounts()
assert.NoError(t, err)
assert.Equal(t, []string{"testaccount"}, accounts)
// Delete the account from the wallet
err = wallet.DeleteAccounts("testaccount")
assert.NoError(t, err)
assert.NoFileExists(t, filePath)
}- 不能一次性通过,在涉及需要指定Account地址的地方,写了一个随意账户地址,导致大部分功能测试不通过
- 经过简单调整后,测试可通过,并获得76%的测试覆盖率
- 测试代码出现equal对比两个指针类型变量的情况,属于一些常识错误
- 结果(codeium)
- 生成过程中,出现异常
I'm sorry, but something went wrong. Here's the error message:
invalid_argument: protocol error: incomplete envelope: context deadline exceeded (Client.Timeout or context cancellation while reading body)
We're working hard to improve the quality of your experience. Please try again later. If the issue persists, our team is > available on [Discord](https://discord.gg/3XFf78nAx5).
Metadata
Metadata
Assignees
Labels
No labels