From 11679b5c98414bbad0bd4c7d144be51eac6afbd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Sat, 24 Jun 2023 02:31:03 +0800 Subject: [PATCH] test --- .gitignore | 2 ++ to_entangle.go | 5 +++++ to_entangle_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 to_entangle_test.go diff --git a/.gitignore b/.gitignore index 3b735ec..ffa6f17 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ *.so *.dylib +data/ + # Test binary, built with `go test -c` *.test diff --git a/to_entangle.go b/to_entangle.go index 8d8a559..548f504 100644 --- a/to_entangle.go +++ b/to_entangle.go @@ -184,3 +184,8 @@ func New(path string) *ToEntangle { db: db, } } + +// Close 关闭 leveldb +func (t *ToEntangle) Close() { + t.db.Close() +} diff --git a/to_entangle_test.go b/to_entangle_test.go new file mode 100644 index 0000000..385d1db --- /dev/null +++ b/to_entangle_test.go @@ -0,0 +1,43 @@ +package toentangle + +import ( + "fmt" + "os" + "testing" +) + +func TestToEntangle_Get(t *testing.T) { + // 创建一个 ToEntangle + entangle := New("data") + + // 添加一对数据, 使其双向绑定 + entangle.Add("a", "b") + entangle.Add("a", "c") + + // 获取 a 的全部绑定数据 + arr, _ := entangle.Get("a") + fmt.Println(arr) + if len(arr) != 2 || arr[0] != "b" || arr[1] != "c" { + t.Errorf("Get(\"a\") = %v; want [\"b\", \"c\"]", arr) + } + + // 获取 b 的全部绑定数据 + arr, _ = entangle.Get("b") + fmt.Println(arr) + if len(arr) != 1 || arr[0] != "a" { + t.Errorf("Get(\"b\") = %v; want [\"a\"]", arr) + } + + // 获取 c 的全部绑定数据 + arr, _ = entangle.Get("c") + fmt.Println(arr) + if len(arr) != 1 || arr[0] != "a" { + t.Errorf("Get(\"c\") = %v; want [\"a\"]", arr) + } + + // 清理 leveldb + entangle.Close() + + // 删除 data 文件夹 + os.RemoveAll("data") +}