A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

57 lines
1.5 KiB

  1. package db
  2. import "testing"
  3. func TestAlterTableSqlBuilder_ToSQL(t *testing.T) {
  4. type fields struct {
  5. Dialect DialectType
  6. Name string
  7. Changes []string
  8. }
  9. tests := []struct {
  10. name string
  11. builder *AlterTableSqlBuilder
  12. want string
  13. wantErr bool
  14. }{
  15. {
  16. name: "MySQL add int",
  17. builder: DialectMySQL.
  18. AlterTable("the_table").
  19. AddColumn(DialectMySQL.Column("the_col", ColumnTypeInteger, UnsetSize)),
  20. want: "ALTER TABLE the_table ADD COLUMN the_col INT NOT NULL",
  21. wantErr: false,
  22. },
  23. {
  24. name: "MySQL add string",
  25. builder: DialectMySQL.
  26. AlterTable("the_table").
  27. AddColumn(DialectMySQL.Column("the_col", ColumnTypeVarChar, OptionalInt{true, 128})),
  28. want: "ALTER TABLE the_table ADD COLUMN the_col VARCHAR(128) NOT NULL",
  29. wantErr: false,
  30. },
  31. {
  32. name: "MySQL add int and string",
  33. builder: DialectMySQL.
  34. AlterTable("the_table").
  35. AddColumn(DialectMySQL.Column("first_col", ColumnTypeInteger, UnsetSize)).
  36. AddColumn(DialectMySQL.Column("second_col", ColumnTypeVarChar, OptionalInt{true, 128})),
  37. want: "ALTER TABLE the_table ADD COLUMN first_col INT NOT NULL, ADD COLUMN second_col VARCHAR(128) NOT NULL",
  38. wantErr: false,
  39. },
  40. }
  41. for _, tt := range tests {
  42. t.Run(tt.name, func(t *testing.T) {
  43. got, err := tt.builder.ToSQL()
  44. if (err != nil) != tt.wantErr {
  45. t.Errorf("ToSQL() error = %v, wantErr %v", err, tt.wantErr)
  46. return
  47. }
  48. if got != tt.want {
  49. t.Errorf("ToSQL() got = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }