Skip to content

Commit f8ce3e2

Browse files
committed
test(mssql): add test for WithScripts and GetSQLCmdPath
1 parent a280aa1 commit f8ce3e2

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

modules/mssql/mssql_test.go

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package mssql_test
33
import (
44
"context"
55
"database/sql"
6+
"path/filepath"
67
"testing"
78

89
_ "github.com/microsoft/go-mssqldb"
910
"github.com/stretchr/testify/require"
10-
1111
"github.com/testcontainers/testcontainers-go"
1212
"github.com/testcontainers/testcontainers-go/modules/mssql"
1313
"github.com/testcontainers/testcontainers-go/wait"
@@ -128,3 +128,126 @@ func TestMSSQLServerWithInvalidPassword(t *testing.T) {
128128
testcontainers.CleanupContainer(t, ctr)
129129
require.NoError(t, err)
130130
}
131+
132+
// tests that a container can be created with a DDL script
133+
func TestMSSQLServerWithScriptsDDL(t *testing.T) {
134+
const password = "MyCustom@Passw0rd"
135+
seedFile := filepath.Join("testdata", "seed.sql")
136+
137+
testCases := []struct {
138+
name string
139+
input []testcontainers.ContainerCustomizer
140+
}{
141+
{
142+
name: "WithPassword/beforeWithScripts",
143+
input: []testcontainers.ContainerCustomizer{
144+
mssql.WithPassword(password),
145+
mssql.WithScripts(seedFile),
146+
},
147+
},
148+
{
149+
name: "WithPassword/afterWithScripts",
150+
input: []testcontainers.ContainerCustomizer{
151+
mssql.WithScripts(seedFile),
152+
mssql.WithPassword(password),
153+
},
154+
},
155+
}
156+
157+
for _, tc := range testCases {
158+
t.Run(tc.name, func(t *testing.T) {
159+
ctx := context.Background()
160+
161+
// Add common options
162+
options := append([]testcontainers.ContainerCustomizer{
163+
mssql.WithAcceptEULA(),
164+
}, tc.input...)
165+
166+
ctr, err := mssql.Run(ctx, "mcr.microsoft.com/mssql/server:2022-CU17-ubuntu-22.04", options...)
167+
require.NoError(t, err)
168+
testcontainers.CleanupContainer(t, ctr)
169+
170+
connectionString, err := ctr.ConnectionString(ctx)
171+
require.NoError(t, err)
172+
173+
db, err := sql.Open("sqlserver", connectionString)
174+
require.NoError(t, err)
175+
defer db.Close()
176+
177+
err = db.PingContext(ctx)
178+
require.NoError(t, err)
179+
180+
rows, err := db.QueryContext(ctx, "SELECT * FROM pizza_palace.pizzas")
181+
require.NoError(t, err)
182+
defer rows.Close()
183+
184+
// test data structure, must match the seed.sql file
185+
type Pizza struct {
186+
ID int
187+
ToppingName string
188+
Deliciousness string
189+
}
190+
191+
want := []Pizza{
192+
{1, "Pineapple", "Controversial but tasty"},
193+
{2, "Pepperoni", "Classic never fails"},
194+
}
195+
got := make([]Pizza, 0)
196+
197+
for rows.Next() {
198+
var p Pizza
199+
err := rows.Scan(&p.ID, &p.ToppingName, &p.Deliciousness)
200+
require.NoError(t, err)
201+
got = append(got, p)
202+
}
203+
204+
require.EqualValues(t, want, got)
205+
})
206+
}
207+
}
208+
209+
// tests that the correct path to the sqlcmd executable is returned based on the image tag
210+
func TestGetSQLCmdPath(t *testing.T) {
211+
tests := []struct {
212+
name string
213+
input string
214+
want string
215+
}{
216+
{
217+
name: "2019-CU14/old-version",
218+
input: "mcr.microsoft.com/mssql/server:2019-CU14-ubuntu-20.04",
219+
want: "/opt/mssql-tools/bin/sqlcmd",
220+
},
221+
{
222+
name: "2022-latest/new-version",
223+
input: "mcr.microsoft.com/mssql/server:2022-latest",
224+
want: "/opt/mssql-tools18/bin/sqlcmd",
225+
},
226+
{
227+
name: "2019-latest/old-version",
228+
input: "mcr.microsoft.com/mssql/server:2019-latest",
229+
want: "/opt/mssql-tools/bin/sqlcmd",
230+
},
231+
{
232+
name: "2022-CU14/new-version",
233+
input: "mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04",
234+
want: "/opt/mssql-tools18/bin/sqlcmd",
235+
},
236+
{
237+
name: "2022-CU17/new-version",
238+
input: "mcr.microsoft.com/mssql/server:2022-CU17-ubuntu-22.04",
239+
want: "/opt/mssql-tools18/bin/sqlcmd",
240+
},
241+
{
242+
name: "2025-CU14/new-version",
243+
input: "mcr.microsoft.com/mssql/server:2025-CU14-ubuntu-22.04",
244+
want: "/opt/mssql-tools18/bin/sqlcmd",
245+
},
246+
}
247+
for _, tt := range tests {
248+
t.Run(tt.name, func(t *testing.T) {
249+
got := mssql.GetSQLCmdPath(tt.input)
250+
require.Equal(t, tt.want, got)
251+
})
252+
}
253+
}

modules/mssql/testdata/seed.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE SCHEMA pizza_palace;
2+
GO
3+
4+
CREATE TABLE pizza_palace.pizzas (
5+
ID INT PRIMARY KEY IDENTITY,
6+
ToppingName NVARCHAR(100),
7+
Deliciousness NVARCHAR(100) UNIQUE
8+
);
9+
GO
10+
11+
INSERT INTO pizza_palace.pizzas (ToppingName, Deliciousness) VALUES
12+
('Pineapple', 'Controversial but tasty'),
13+
('Pepperoni', 'Classic never fails')
14+
GO

0 commit comments

Comments
 (0)