|
1 | 1 | package mssql_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "database/sql" |
6 | 7 | "testing" |
7 | 8 |
|
| 9 | + _ "embed" |
| 10 | + |
8 | 11 | _ "github.com/microsoft/go-mssqldb" |
9 | 12 | "github.com/stretchr/testify/require" |
10 | | - |
11 | 13 | "github.com/testcontainers/testcontainers-go" |
12 | 14 | "github.com/testcontainers/testcontainers-go/modules/mssql" |
13 | 15 | "github.com/testcontainers/testcontainers-go/wait" |
@@ -128,3 +130,77 @@ func TestMSSQLServerWithInvalidPassword(t *testing.T) { |
128 | 130 | testcontainers.CleanupContainer(t, ctr) |
129 | 131 | require.NoError(t, err) |
130 | 132 | } |
| 133 | + |
| 134 | +//go:embed testdata/seed.sql |
| 135 | +var seedSQLContent []byte |
| 136 | + |
| 137 | +// tests that a container can be created with a DDL script |
| 138 | +func TestMSSQLServerWithScriptsDDL(t *testing.T) { |
| 139 | + const password = "MyCustom@Passw0rd" |
| 140 | + |
| 141 | + // assertContainer contains the logic for asserting the test |
| 142 | + assertContainer := func(t *testing.T, ctx context.Context, options ...testcontainers.ContainerCustomizer) { |
| 143 | + ctr, err := mssql.Run(ctx, |
| 144 | + "mcr.microsoft.com/mssql/server:2022-CU17-ubuntu-22.04", |
| 145 | + append([]testcontainers.ContainerCustomizer{ |
| 146 | + mssql.WithAcceptEULA(), |
| 147 | + testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{ |
| 148 | + ProviderType: testcontainers.ProviderPodman, |
| 149 | + }), |
| 150 | + }, options...)..., |
| 151 | + ) |
| 152 | + testcontainers.CleanupContainer(t, ctr) |
| 153 | + require.NoError(t, err) |
| 154 | + |
| 155 | + connectionString, err := ctr.ConnectionString(ctx) |
| 156 | + require.NoError(t, err) |
| 157 | + |
| 158 | + db, err := sql.Open("sqlserver", connectionString) |
| 159 | + require.NoError(t, err) |
| 160 | + defer db.Close() |
| 161 | + |
| 162 | + err = db.PingContext(ctx) |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + rows, err := db.QueryContext(ctx, "SELECT * FROM pizza_palace.pizzas") |
| 166 | + require.NoError(t, err) |
| 167 | + defer rows.Close() |
| 168 | + |
| 169 | + type Pizza struct { |
| 170 | + ID int |
| 171 | + ToppingName string |
| 172 | + Deliciousness string |
| 173 | + } |
| 174 | + |
| 175 | + want := []Pizza{ |
| 176 | + {1, "Pineapple", "Controversial but tasty"}, |
| 177 | + {2, "Pepperoni", "Classic never fails"}, |
| 178 | + } |
| 179 | + got := make([]Pizza, 0, len(want)) |
| 180 | + |
| 181 | + for rows.Next() { |
| 182 | + var p Pizza |
| 183 | + err := rows.Scan(&p.ID, &p.ToppingName, &p.Deliciousness) |
| 184 | + require.NoError(t, err) |
| 185 | + got = append(got, p) |
| 186 | + } |
| 187 | + |
| 188 | + require.EqualValues(t, want, got) |
| 189 | + } |
| 190 | + |
| 191 | + ctx := context.Background() |
| 192 | + |
| 193 | + t.Run("WithPassword/beforeWithScripts", func(t *testing.T) { |
| 194 | + assertContainer(t, ctx, |
| 195 | + mssql.WithPassword(password), |
| 196 | + mssql.WithInitSQL(bytes.NewReader(seedSQLContent)), |
| 197 | + ) |
| 198 | + }) |
| 199 | + |
| 200 | + t.Run("WithPassword/afterWithScripts", func(t *testing.T) { |
| 201 | + assertContainer(t, ctx, |
| 202 | + mssql.WithInitSQL(bytes.NewReader(seedSQLContent)), |
| 203 | + mssql.WithPassword(password), |
| 204 | + ) |
| 205 | + }) |
| 206 | +} |
0 commit comments