|
| 1 | +package domains |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLoadTableSchema(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + sql string |
| 13 | + expected []TableSchema |
| 14 | + }{ |
| 15 | + { |
| 16 | + sql: "CREATE TABLE users (\n" + |
| 17 | + " id BIGINT AUTO_INCREMENT PRIMARY KEY,\n" + |
| 18 | + " `name` VARCHAR(255) NOT NULL,\n" + |
| 19 | + " created_at DATETIME(6) UNIQUE,\n" + |
| 20 | + " description TEXT NOT NULL,\n" + |
| 21 | + " icon LONGBLOB NOT NULL,\n" + |
| 22 | + " UNIQUE KEY uniq_name (name),\n" + |
| 23 | + ");\n" + |
| 24 | + "CREATE TABLE posts (\n" + |
| 25 | + " id BIGINT NOT NULL AUTO_INCREMENT,\n" + |
| 26 | + " title VARCHAR(255) NOT NULL,\n" + |
| 27 | + " content TEXT NOT NULL,\n" + |
| 28 | + " PRIMARY KEY (id),\n" + |
| 29 | + " UNIQUE (`title`),\n" + |
| 30 | + ");", |
| 31 | + expected: []TableSchema{ |
| 32 | + { |
| 33 | + TableName: "users", |
| 34 | + Columns: []TableSchemaColumn{ |
| 35 | + {ColumnName: "id", DataType: TableSchemaDataType_INT64, IsNullable: true, IsPrimary: true, IsUnique: false}, |
| 36 | + {ColumnName: "name", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: true}, |
| 37 | + {ColumnName: "created_at", DataType: TableSchemaDataType_DATETIME, IsNullable: true, IsPrimary: false, IsUnique: true}, |
| 38 | + {ColumnName: "description", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 39 | + {ColumnName: "icon", DataType: TableSchemaDataType_BYTES, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + TableName: "posts", |
| 44 | + Columns: []TableSchemaColumn{ |
| 45 | + {ColumnName: "id", DataType: TableSchemaDataType_INT64, IsNullable: false, IsPrimary: true, IsUnique: false}, |
| 46 | + {ColumnName: "title", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: true}, |
| 47 | + {ColumnName: "content", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + sql: "DROP TABLE IF EXISTS users;\n" + |
| 54 | + "CREATE TABLE users (\n" + |
| 55 | + " `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + |
| 56 | + " `account_name` varchar(64) NOT NULL UNIQUE,\n" + |
| 57 | + " `passhash` varchar(128) NOT NULL, -- SHA2 512 non-binary (hex)\n" + |
| 58 | + " `authority` tinyint(1) NOT NULL DEFAULT 0,\n" + |
| 59 | + " `del_flg` tinyint(1) NOT NULL DEFAULT 0,\n" + |
| 60 | + " `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n" + |
| 61 | + ") DEFAULT CHARSET=utf8mb4;\n" + |
| 62 | + "\n" + |
| 63 | + "DROP TABLE IF EXISTS posts;\n" + |
| 64 | + "CREATE TABLE posts (\n" + |
| 65 | + " `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + |
| 66 | + " `user_id` int NOT NULL,\n" + |
| 67 | + " `mime` varchar(64) NOT NULL,\n" + |
| 68 | + " `imgdata` mediumblob NOT NULL,\n" + |
| 69 | + " `body` text NOT NULL,\n" + |
| 70 | + " `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n" + |
| 71 | + ") DEFAULT CHARSET=utf8mb4;\n" + |
| 72 | + "\n" + |
| 73 | + "DROP TABLE IF EXISTS comments;\n" + |
| 74 | + "CREATE TABLE comments (\n" + |
| 75 | + " `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + |
| 76 | + " `post_id` int NOT NULL,\n" + |
| 77 | + " `user_id` int NOT NULL,\n" + |
| 78 | + " `comment` text NOT NULL,\n" + |
| 79 | + " `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n" + |
| 80 | + ") DEFAULT CHARSET=utf8mb4;\n", |
| 81 | + expected: []TableSchema{ |
| 82 | + { |
| 83 | + TableName: "users", |
| 84 | + Columns: []TableSchemaColumn{ |
| 85 | + {ColumnName: "id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: true, IsUnique: false}, |
| 86 | + {ColumnName: "account_name", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: true}, |
| 87 | + {ColumnName: "passhash", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 88 | + {ColumnName: "authority", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 89 | + {ColumnName: "del_flg", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 90 | + {ColumnName: "created_at", DataType: TableSchemaDataType_DATETIME, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + TableName: "posts", |
| 95 | + Columns: []TableSchemaColumn{ |
| 96 | + {ColumnName: "id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: true, IsUnique: false}, |
| 97 | + {ColumnName: "user_id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 98 | + {ColumnName: "mime", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 99 | + {ColumnName: "imgdata", DataType: TableSchemaDataType_BYTES, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 100 | + {ColumnName: "body", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 101 | + {ColumnName: "created_at", DataType: TableSchemaDataType_DATETIME, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + TableName: "comments", |
| 106 | + Columns: []TableSchemaColumn{ |
| 107 | + {ColumnName: "id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: true, IsUnique: false}, |
| 108 | + {ColumnName: "post_id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 109 | + {ColumnName: "user_id", DataType: TableSchemaDataType_INT, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 110 | + {ColumnName: "comment", DataType: TableSchemaDataType_STRING, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 111 | + {ColumnName: "created_at", DataType: TableSchemaDataType_DATETIME, IsNullable: false, IsPrimary: false, IsUnique: false}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for i, test := range tests { |
| 119 | + t.Run("test"+fmt.Sprint(i), func(t *testing.T) { |
| 120 | + schemas, err := LoadTableSchema(test.sql) |
| 121 | + assert.NoError(t, err) |
| 122 | + assert.Equal(t, test.expected, schemas) |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
0 commit comments