yjp 1 year ago
parent
commit
b65c801d8b
2 changed files with 59 additions and 1 deletions
  1. 2 0
      test_sql.go
  2. 57 1
      v1_test.go

+ 2 - 0
test_sql.go

@@ -23,6 +23,8 @@ const (
 	sqlInsertFormat = `insert into %s (id, name, time, table_num) values ('%s', '%s', parse_time('%s', '2006-01-02 15:04:05'), %d)`
 	sqlDeleteFormat = `delete from %s where id = '%s'`
 	sqlUpdateFormat = `update %s set name = '%s', time = parse_time('%s', '2006-01-02 15:04:05'), table_num = %d where id = '%s'`
+	sqlSelectFormat = `select * from %s where id = '%s'`
+	sqlCountFormat  = `select count(*) as count from %s where id = '%s'`
 )
 
 const (

+ 57 - 1
v1_test.go

@@ -218,8 +218,64 @@ func TestApiV1Operate(t *testing.T) {
 	})
 
 	operate(t, fmt.Sprintf(sqlInsertFormat, tablePrefix, id, name, now.Format(time.DateTime), tableNum), keyColumns)
+
+	tableRows := operate(t, fmt.Sprintf(sqlSelectFormat, tablePrefix, id), nil)
+
+	if tableRows[0]["id"] != id {
+		t.Fatal("id不正确")
+	}
+
+	if tableRows[0]["name"] != name {
+		t.Fatal("name不正确")
+	}
+
+	if tableRows[0]["time"] != now.Format("2006-01-02T15:04:05+08:00") {
+		t.Fatal("time不正确")
+	}
+
+	if tableRows[0]["table_num"] != float64(tableNum) {
+		t.Fatal("tableNum不正确")
+	}
+
+	tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
+
+	if tableRows[0]["count"] != float64(1) {
+		t.Fatal("数量不正确")
+	}
+
 	operate(t, fmt.Sprintf(sqlUpdateFormat, tablePrefix, newName, newNow.Format(time.DateTime), newTableNum, id), keyColumns)
+
+	tableRows = operate(t, fmt.Sprintf(sqlSelectFormat, tablePrefix, id), nil)
+
+	if tableRows[0]["id"] != id {
+		t.Fatal("id不正确")
+	}
+
+	if tableRows[0]["name"] != newName {
+		t.Fatal("name不正确")
+	}
+
+	if tableRows[0]["time"] != newNow.Format("2006-01-02T15:04:05+08:00") {
+		t.Fatal("time不正确")
+	}
+
+	if tableRows[0]["table_num"] != float64(newTableNum) {
+		t.Fatal("tableNum不正确")
+	}
+
+	tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
+
+	if tableRows[0]["count"] != float64(1) {
+		t.Fatal("数量不正确")
+	}
+
 	operate(t, fmt.Sprintf(sqlDeleteFormat, tablePrefix, id), keyColumns)
+
+	tableRows = operate(t, fmt.Sprintf(sqlCountFormat, tablePrefix, id), nil)
+
+	if tableRows[0]["count"] != float64(0) {
+		t.Fatal("数量不正确")
+	}
 }
 
 func autoMigrate(t *testing.T, items []client.AutoMigrateItem) {
@@ -264,7 +320,7 @@ func operateParse(t *testing.T, sql string) map[string]any {
 func operate(t *testing.T, sql string, keyColumns []string) []map[string]any {
 	result := new(struct {
 		response.MsgResponse
-		TableRows []map[string]any `json:"table_rows"`
+		TableRows []map[string]any `json:"tableRows"`
 	})
 
 	resp, err := resty.New().R().