diff --git a/src/xsql/db_test.go b/src/xsql/db_test.go index f152ef4..fb61d96 100644 --- a/src/xsql/db_test.go +++ b/src/xsql/db_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/protobuf/types/known/timestamppb" "log" - "strings" + "os" "testing" "time" ) @@ -91,22 +91,11 @@ func newDB() *xsql.DB { func TestCreateTable(t *testing.T) { a := assert.New(t) - q := `DROP TABLE IF EXISTS #xsql#; -CREATE TABLE #xsql# ( - #id# int unsigned NOT NULL AUTO_INCREMENT, - #foo# varchar(255) DEFAULT NULL, - #bar# datetime DEFAULT NULL, - #bool# int NOT NULL DEFAULT '0', - #enum# int NOT NULL DEFAULT '0', - #json# json DEFAULT NULL, - PRIMARY KEY (#id#) -) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; -INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (1, 'v', '2022-04-12 23:50:00', 1, 1, '{"foo":"bar"}'); -INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (2, 'v1', '2022-04-13 23:50:00', 1, 1, '[1,2]'); -INSERT INTO #xsql# (#id#, #foo#, #bar#, #bool#, #enum#, #json#) VALUES (3, 'v2', '2022-04-14 23:50:00', 1, 1, null); -` DB := newDB() - _, err := DB.Exec(strings.ReplaceAll(q, "#", "`")) + + b, err := os.ReadFile("./xsql.sql") + a.Nil(err) + _, err = DB.Exec(string(b)) a.Empty(err) } diff --git a/src/xsql/xsql.sql b/src/xsql/xsql.sql index 9822379..3b854b9 100644 --- a/src/xsql/xsql.sql +++ b/src/xsql/xsql.sql @@ -1,39 +1,13 @@ -/* - Navicat Premium Data Transfer - - Source Server : localhost_3306 - Source Server Type : MySQL - Source Server Version : 80027 - Source Host : localhost:3306 - Source Schema : test - - Target Server Type : MySQL - Target Server Version : 80027 - File Encoding : 65001 - - Date: 15/04/2022 22:20:33 -*/ - -SET NAMES utf8mb4; -SET FOREIGN_KEY_CHECKS = 0; - --- ---------------------------- --- Table structure for xsql --- ---------------------------- DROP TABLE IF EXISTS `xsql`; CREATE TABLE `xsql` ( - `id` int unsigned NOT NULL AUTO_INCREMENT, - `foo` varchar(255) DEFAULT NULL, - `bar` datetime DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; - --- ---------------------------- --- Records of xsql --- ---------------------------- -BEGIN; -INSERT INTO `xsql` (`id`, `foo`, `bar`) VALUES (1, 'v', '2022-04-14 23:49:48'); -INSERT INTO `xsql` (`id`, `foo`, `bar`) VALUES (2, 'v1', '2022-04-14 23:50:00'); -COMMIT; - -SET FOREIGN_KEY_CHECKS = 1; + `id` int unsigned NOT NULL AUTO_INCREMENT, + `foo` varchar(255) DEFAULT NULL, + `bar` datetime DEFAULT NULL, + `bool` int NOT NULL DEFAULT '0', + `enum` int NOT NULL DEFAULT '0', + `json` json DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; +INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (1, 'v', '2022-04-12 23:50:00', 1, 1, '{"foo":"bar"}'); +INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (2, 'v1', '2022-04-13 23:50:00', 1, 1, '[1,2]'); +INSERT INTO `xsql` (`id`, `foo`, `bar`, `bool`, `enum`, `json`) VALUES (3, 'v2', '2022-04-14 23:50:00', 1, 1, null); \ No newline at end of file