@@ -3,11 +3,11 @@ package mssql_test
33import (
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,125 @@ 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+ tests := []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 _ , tt := range tests {
158+ t .Run (tt .name , func (t * testing.T ) {
159+ ctx := context .Background ()
160+
161+ options := append ([]testcontainers.ContainerCustomizer {
162+ mssql .WithAcceptEULA (),
163+ }, tt .input ... )
164+
165+ ctr , err := mssql .Run (ctx , "mcr.microsoft.com/mssql/server:2022-CU17-ubuntu-22.04" , options ... )
166+ require .NoError (t , err )
167+ testcontainers .CleanupContainer (t , ctr )
168+
169+ connectionString , err := ctr .ConnectionString (ctx )
170+ require .NoError (t , err )
171+
172+ db , err := sql .Open ("sqlserver" , connectionString )
173+ require .NoError (t , err )
174+ defer db .Close ()
175+
176+ err = db .PingContext (ctx )
177+ require .NoError (t , err )
178+
179+ rows , err := db .QueryContext (ctx , "SELECT * FROM pizza_palace.pizzas" )
180+ require .NoError (t , err )
181+ defer rows .Close ()
182+
183+ // test data structure, must match the seed.sql file
184+ type Pizza struct {
185+ ID int
186+ ToppingName string
187+ Deliciousness string
188+ }
189+
190+ want := []Pizza {
191+ {1 , "Pineapple" , "Controversial but tasty" },
192+ {2 , "Pepperoni" , "Classic never fails" },
193+ }
194+ got := make ([]Pizza , 0 )
195+
196+ for rows .Next () {
197+ var p Pizza
198+ err := rows .Scan (& p .ID , & p .ToppingName , & p .Deliciousness )
199+ require .NoError (t , err )
200+ got = append (got , p )
201+ }
202+
203+ require .EqualValues (t , want , got )
204+ })
205+ }
206+ }
207+
208+ // tests that the correct path to the sqlcmd executable is returned based on the image tag
209+ func TestGetSQLCmdPath (t * testing.T ) {
210+ tests := []struct {
211+ name string
212+ input string
213+ want string
214+ }{
215+ {
216+ name : "2019-CU14/old-version" ,
217+ input : "mcr.microsoft.com/mssql/server:2019-CU14-ubuntu-20.04" ,
218+ want : "/opt/mssql-tools/bin/sqlcmd" ,
219+ },
220+ {
221+ name : "2022-latest/new-version" ,
222+ input : "mcr.microsoft.com/mssql/server:2022-latest" ,
223+ want : "/opt/mssql-tools18/bin/sqlcmd" ,
224+ },
225+ {
226+ name : "2019-latest/old-version" ,
227+ input : "mcr.microsoft.com/mssql/server:2019-latest" ,
228+ want : "/opt/mssql-tools/bin/sqlcmd" ,
229+ },
230+ {
231+ name : "2022-CU14/new-version" ,
232+ input : "mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04" ,
233+ want : "/opt/mssql-tools18/bin/sqlcmd" ,
234+ },
235+ {
236+ name : "2022-CU17/new-version" ,
237+ input : "mcr.microsoft.com/mssql/server:2022-CU17-ubuntu-22.04" ,
238+ want : "/opt/mssql-tools18/bin/sqlcmd" ,
239+ },
240+ {
241+ name : "2025-CU14/new-version" ,
242+ input : "mcr.microsoft.com/mssql/server:2025-CU14-ubuntu-22.04" ,
243+ want : "/opt/mssql-tools18/bin/sqlcmd" ,
244+ },
245+ }
246+ for _ , tt := range tests {
247+ t .Run (tt .name , func (t * testing.T ) {
248+ got := mssql .GetSQLCmdPath (tt .input )
249+ require .Equal (t , tt .want , got )
250+ })
251+ }
252+ }
0 commit comments