sk-matrix-project/app/wails/lib/general_test.go

122 lines
2.5 KiB
Go
Raw Normal View History

2023-07-08 20:46:27 +08:00
package lib
import (
"bytes"
2023-07-09 20:46:46 +08:00
"database/sql"
2023-07-08 20:46:27 +08:00
"fmt"
"github.com/gen2brain/beeep"
2023-07-08 22:59:16 +08:00
"os"
2023-07-08 20:46:27 +08:00
"os/exec"
2023-07-09 20:46:46 +08:00
"path/filepath"
2023-07-08 20:46:27 +08:00
"strings"
"testing"
"time"
2023-07-09 20:46:46 +08:00
_ "github.com/mutecomm/go-sqlcipher/v4"
2023-07-08 20:46:27 +08:00
)
func TestName(t *testing.T) {
cmd := exec.Command("chcp")
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
_ = cmd.Run()
code := strings.Split(string(b.Bytes()), ":")[1]
fmt.Printf("系统字符集编码为 : %v\n", code)
}
func TestBeeepBeep(t *testing.T) {
for i := 0; i < 10; i++ {
go func(freq float64) {
err := beeep.Beep(freq, beeep.DefaultDuration)
if err != nil {
panic(err)
}
}(beeep.DefaultFreq + (float64(i * 100)))
}
}
func TestBeeepNotify(t *testing.T) {
err := beeep.Notify("测试", "测试", "")
if err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
}
2023-07-08 22:59:16 +08:00
func TestEnv(t *testing.T) {
for _, e := range os.Environ() {
parts := strings.SplitN(e, "=", 2)
if len(parts) != 2 {
continue
} else {
println(parts[0], parts[1])
}
}
}
func TestJavaEnv(t *testing.T) {
fmt.Println(os.Getenv("java_home"))
fmt.Println(os.Getenv("JAVA_HOME"))
}
2023-07-09 20:46:46 +08:00
func TestSqlite(t *testing.T) {
fmt.Println(os.Args[0])
fmt.Println(os.Getwd())
dbPath := filepath.Join(filepath.Dir(os.Args[0]), "test.db")
fmt.Printf("dbPath => %v\n", dbPath)
// 加密
//dataSourceName := fmt.Sprintf("file:%s?_pragma_key=%s", dbPath, "shikong")
// 不加密
dataSourceName := fmt.Sprintf("file:%s?", dbPath)
db, err := sql.Open("sqlite3", dataSourceName)
_, _ = db.Exec("PRAGMA cipher_use_hmac = OFF;")
_, _ = db.Exec("PRAGMA cipher_page_size = 1024;")
_, _ = db.Exec("PRAGMA kdf_iter = 4000;")
if err != nil {
t.Fatal(err)
}
defer func() {
_ = db.Close()
}()
err = db.Ping()
if err != nil {
t.Fatal(err)
}
//_, err = db.Exec("PRAGMA key = shikong;")
//if err != nil {
// t.Fatal(err)
// return
//}
sql := `
DROP TABLE IF EXISTS cars;
CREATE TABLE cars(id INTEGER PRIMARY KEY, name TEXT, price INT);
INSERT INTO cars(name, price) VALUES('Audi',52642);
INSERT INTO cars(name, price) VALUES('Mercedes',57127);
INSERT INTO cars(name, price) VALUES('Skoda',9000);
INSERT INTO cars(name, price) VALUES('Volvo',29000);
INSERT INTO cars(name, price) VALUES('Bentley',350000);
INSERT INTO cars(name, price) VALUES('Citroen',21000);
INSERT INTO cars(name, price) VALUES('Hummer',41400);
INSERT INTO cars(name, price) VALUES('Volkswagen',21600);
`
_, _ = db.Exec(sql)
result, err := db.Exec("select * from sqlite_master;")
if err != nil {
t.Fatal(err)
return
}
fmt.Printf("%+v\n", result)
fmt.Println("数据库连接成功")
}