Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

AI 评测:单元测试 #3

Open
bjwswang opened this issue May 24, 2023 · 5 comments
Open

AI 评测:单元测试 #3

bjwswang opened this issue May 24, 2023 · 5 comments

Comments

@bjwswang
Copy link
Member

bjwswang commented May 24, 2023

定义测试用例

  1. TestCase1
  • 描述: 测试类型转换

  • 特点:

    • 无任何其他库依赖
    • 不涉及系统调用,如文件读写
    • 不涉及网络调用
  • 结果:

    • 基于chatgpt一次性生成
    • 覆盖率100%
  1. [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)
}
  1. 不能一次性通过,在涉及需要指定Account地址的地方,写了一个随意账户地址,导致大部分功能测试不通过
  2. 经过简单调整后,测试可通过,并获得76%的测试覆盖率
  3. 测试代码出现equal对比两个指针类型变量的情况,属于一些常识错误
  • 结果(codeium)
  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](https://discord.gg/3XFf78nAx5).
@bjwswang
Copy link
Member Author

bjwswang commented May 24, 2023

Testcase2

描述: 查询Nonce
特点:

Codeium

@bjwswang bjwswang changed the title AI 评测:生成单元测试 AI 评测:单元测试 May 25, 2023
@bjwswang
Copy link
Member Author

Testcase3

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

@bjwswang
Copy link
Member Author

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

@dayuy
Copy link

dayuy commented Jun 1, 2023

Testcase4

  • 描述: 测试Slice去重的功能

  • 特点:

    • 无任何外部依赖
    • 无网络调用
  • 结果

    • 一次性生成
    • 覆盖率100%
  • 单测样例

    func RemoveDuplicateForStringSlice(elements []string) []string {
     result := make([]string, 0, len(elements))
     temp := map[string]struct{}{}
     for _, element := range elements {
     	if _, ok := temp[element]; !ok && element != "" {
     		temp[element] = struct{}{}
     		result = append(result, element)
     	}
     }
     return result
    }
  • 生成的单测代码:

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
Copy link

dayuy commented Jun 1, 2023

Testcase5

  • 描述:测试获取Map的深层嵌套String类型的属性值

  • 特点:

    • 依赖k8s.io
  • 结果

    1. 一次性生成
    2. 生成后的代码有错误的地方:Cannot assign to obj["foo"]["baz"]
    3. 提示其错误,后得到修正:obj["foo"].(map[string]interface{})["baz"] = 123
    4. 覆盖率 100%
  • 单测样例:

func GetNestedString(obj map[string]interface{}, fields ...string) string {
	val, _, _ := unstructured.NestedString(obj, fields...)
	return val
}
  • 生成的单测代码:
import (
    "testing"
    "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants