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