bestchains / HackTheAI

Apache License 2.0
2 stars 7 forks source link

AI 评测:单元测试 #3

Open bjwswang opened 1 year ago

bjwswang commented 1 year ago

定义测试用例

  1. TestCase1
  1. [TestCase2]

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)
}

  1. 不能一次性通过,在涉及需要指定Account地址的地方,写了一个随意账户地址,导致大部分功能测试不通过
  2. 经过简单调整后,测试可通过,并获得76%的测试覆盖率
  3. 测试代码出现equal对比两个指针类型变量的情况,属于一些常识错误
  1. 生成过程中,出现异常
    
    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.

bjwswang commented 1 year ago

Testcase2

描述: 查询Nonce 特点:

Codeium

bjwswang commented 1 year ago

Testcase3

相关链接 https://github.com/kubebb/core/pull/15/files#r1205109415

bjwswang commented 1 year ago

@dayuy 你的首次AI单测记录 😆

dayuy commented 1 year ago

Testcase4

package main

import (
    "reflect"
    "testing"
)

func TestRemoveDuplicateForStringSlice(t *testing.T) {
    testCases := []struct {
        name     string
        input    []string
        expected []string
    }{
        {
            name:     "no duplicates",
            input:    []string{"a", "b", "c"},
            expected: []string{"a", "b", "c"},
        },
        {
            name:     "some duplicates",
            input:    []string{"a", "b", "a", "c", "b"},
            expected: []string{"a", "b", "c"},
        },
        {
            name:     "all duplicates",
            input:    []string{"a", "a", "a", "a", ""},
            expected: []string{"a"},
        },
        {
            name:     "empty slice",
            input:    []string{},
            expected: []string{},
        },
    }

    for _, tc := range testCases {
        t.Run(tc.name, func(t *testing.T) {
            result := RemoveDuplicateForStringSlice(tc.input)
            if !reflect.DeepEqual(result, tc.expected) {
                t.Errorf("expected %v, but got %v", tc.expected, result)
            }
        })
    }
}
dayuy commented 1 year ago

Testcase5

func TestGetNestedString(t *testing.T) { obj := map[string]interface{}{ "foo": map[string]interface{}{ "bar": "baz", }, }

// Test a valid nested string value.
expected := "baz"
actual := GetNestedString(obj, "foo", "bar")
if actual != expected {
    t.Errorf("Expected %s but got %s", expected, actual)
}

// Test a missing nested field.
expected = ""
actual = GetNestedString(obj, "foo", "notfound")
if actual != expected {
    t.Errorf("Expected %s but got %s", expected, actual)
}

// Test a non-string nested value.
obj["foo"].(map[string]interface{})["baz"] = 123
expected = ""
actual = GetNestedString(obj, "foo", "baz")
if actual != expected {
    t.Errorf("Expected %s but got %s", expected, actual)
}

}