Skip to content

Commit 8c00f01

Browse files
committed
Fixing some unit tests.
1 parent 9d24f0a commit 8c00f01

File tree

7 files changed

+102
-11
lines changed

7 files changed

+102
-11
lines changed

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/ImportExportCmdletTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Microsoft.WindowsAzure.Commands.SqlDatabase.Test.Utilities;
2525
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
2626
using Microsoft.WindowsAzure.Commands.Utilities.Common;
27+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server;
2728

2829
namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.Database.Cmdlet
2930
{
@@ -112,6 +113,10 @@ public void ImportExportAzureSqlDatabaseTests()
112113
@" -StorageAccountKey $storageAccountKey");
113114
}).FirstOrDefault();
114115

116+
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
117+
//
118+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;
119+
115120
//testSession.ServiceBaseUri = new Uri("https://lqtqbo6kkp.database.windows.net");
116121
Collection<PSObject> databaseContext = MockServerHelper.ExecuteWithMock(
117122
testSession,

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/NewAzureSqlDatabaseServerContextTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public void NewAzureSqlDatabaseServerContextWithSqlAuth()
126126
UnitTestHelper.ImportAzureModule(powershell);
127127
UnitTestHelper.CreateTestCredential(powershell);
128128

129+
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
130+
//
131+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;
129132
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
130133
{
131134
Collection<PSObject> serverContext;
@@ -190,6 +193,10 @@ public void NewAzureSqlDatabaseServerContextWithSqlAuthNegativeCases()
190193

191194
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
192195
{
196+
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
197+
//
198+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;
199+
193200
// Test warning when different $metadata is received.
194201
Collection<PSObject> serverContext;
195202
using (new MockHttpServer(
@@ -212,6 +219,10 @@ public void NewAzureSqlDatabaseServerContextWithSqlAuthNegativeCases()
212219
Assert.AreEqual(2, powershell.Streams.Warning.Count, "Should have warning!");
213220
powershell.Streams.ClearStreams();
214221

222+
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
223+
//
224+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;
225+
215226
// Test error case
216227
using (new MockHttpServer(
217228
exceptionManager,
@@ -308,6 +319,10 @@ public static void CreateServerContextSqlAuth(
308319
testSession.SessionProperties["Username"],
309320
testSession.SessionProperties["Password"]);
310321

322+
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
323+
//
324+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;
325+
311326
Collection<PSObject> serverContext;
312327
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
313328
{
@@ -344,7 +359,7 @@ public static void CreateServerContextSqlAuth(
344359
/// Common helper method for other tests to create a context for ESA server.
345360
/// </summary>
346361
/// <param name="contextVariable">The variable name that will hold the new context.</param>
347-
public static void CreateServerContextSqlAuthV2(
362+
public static void CreateServerContextSqlAuthV12(
348363
System.Management.Automation.PowerShell powershell,
349364
string manageUrl,
350365
string username,
@@ -357,6 +372,10 @@ public static void CreateServerContextSqlAuthV2(
357372
username,
358373
password);
359374

375+
// Tell the sql auth factory to create a v22 context (skip checking sql version using select query).
376+
//
377+
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v12;
378+
360379
Collection<PSObject> serverContext;
361380
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
362381
{

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/Database/Cmdlet/SqlAuthv12MockTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ---
1414

1515
using Microsoft.VisualStudio.TestTools.UnitTesting;
16+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Database.Cmdlet;
1617
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server;
1718
using Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.TSql;
1819
using System;
@@ -30,7 +31,7 @@ public class SqlAuthv12MockTests
3031
{
3132
public static string username = "testlogin";
3233
public static string password = "MyS3curePa$$w0rd";
33-
public static string manageUrl = "https://mysvr2.adamkr-vm04.onebox.xdb.mscds.com";
34+
public static string manageUrl = "https://mysvr2.database.windows.net";
3435

3536
[TestInitialize]
3637
public void Setup()
@@ -54,7 +55,7 @@ public void NewAzureSqlDatabaseWithSqlAuthv12()
5455
System.Management.Automation.PowerShell.Create())
5556
{
5657
// Create a context
57-
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
58+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
5859
powershell,
5960
manageUrl,
6061
username,
@@ -121,7 +122,6 @@ public void NewAzureSqlDatabaseWithSqlAuthv12()
121122
}
122123
}
123124

124-
125125
//[RecordMockDataResults("./")]
126126
[TestMethod]
127127
public void GetAzureSqlDatabaseWithSqlAuthv12()
@@ -133,7 +133,7 @@ public void GetAzureSqlDatabaseWithSqlAuthv12()
133133
System.Management.Automation.PowerShell.Create())
134134
{
135135
// Create a context
136-
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
136+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
137137
powershell,
138138
manageUrl,
139139
username,
@@ -189,7 +189,7 @@ public void SetAzureSqlDatabaseWithSqlAuthv12()
189189
System.Management.Automation.PowerShell.Create())
190190
{
191191
// Create a context
192-
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
192+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
193193
powershell,
194194
manageUrl,
195195
username,

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/UnitTests/TSql/MockSqlCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,6 @@ private string GetCommandKey()
488488
key = key.Replace(parameter.ParameterName, value);
489489
}
490490

491-
//key = key.Replace("\r", string.Empty).Replace("\n", Environment.NewLine);
492-
493491
key = TempTableNameRegex.Replace(key, TempTableName);
494492

495493
return key;

src/ServiceManagement/Sql/Commands.SqlDatabase/Database/Cmdlet/NewAzureSqlDatabaseServerContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ private AzureSubscription CurrentSubscription
162162

163163
#endregion
164164

165-
166165
/// <summary>
167166
/// Connect to a Azure SQL Server with the given ManagementService Uri using
168167
/// SQL authentication credentials.

src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/SqlAuthContextFactory.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
216
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Common;
317
using System;
418
using System.Collections.Generic;
@@ -14,6 +28,38 @@ namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server
1428
{
1529
public class SqlAuthContextFactory
1630
{
31+
/// <summary>
32+
/// The different sql versions available
33+
/// </summary>
34+
internal enum SqlVersion
35+
{
36+
/// <summary>
37+
/// Not set. Determine by querying the server
38+
/// </summary>
39+
None,
40+
41+
/// <summary>
42+
/// V2 server
43+
/// </summary>
44+
v2,
45+
46+
/// <summary>
47+
/// V12 server
48+
/// </summary>
49+
v12
50+
}
51+
internal static SqlVersion sqlVersion = SqlVersion.None;
52+
53+
/// <summary>
54+
/// Gets a sql auth connection context.
55+
/// </summary>
56+
/// <param name="cmdlet">The cmdlet requesting the context</param>
57+
/// <param name="serverName">The name of the server to connect to</param>
58+
/// <param name="manageUrl">The manage url of the server</param>
59+
/// <param name="credentials">The credentials to connect to the server</param>
60+
/// <param name="sessionActivityId">The session activity ID</param>
61+
/// <param name="managementServiceUri">The URI for management service</param>
62+
/// <returns>The connection context</returns>
1763
public static IServerDataServiceContext GetContext(
1864
PSCmdlet cmdlet,
1965
string serverName,
@@ -22,7 +68,22 @@ public static IServerDataServiceContext GetContext(
2268
Guid sessionActivityId,
2369
Uri managementServiceUri)
2470
{
25-
Version version = GetVersion(manageUrl, credentials);
71+
Version version;
72+
73+
// If a version was specified (by tests) us it.
74+
if (sqlVersion == SqlVersion.v2)
75+
{
76+
version = new Version(11, 0);
77+
}
78+
else if (sqlVersion == SqlVersion.v12)
79+
{
80+
version = new Version(12, 0);
81+
}
82+
else // If no version specified, determine the version by querying the server.
83+
{
84+
version = GetVersion(manageUrl, credentials);
85+
}
86+
sqlVersion = SqlVersion.None;
2687

2788
IServerDataServiceContext context = null;
2889

src/ServiceManagement/Sql/Commands.SqlDatabase/Services/Server/TSqlConnectionContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,17 @@ public Database CreateNewDatabase(
407407
return GetDatabase(databaseName);
408408
}
409409

410+
/// <summary>
411+
/// Checks to make sure the collation only contains alphanumeric characters and '_'
412+
/// </summary>
413+
/// <param name="databaseCollation">The string to verify</param>
410414
private void SqlCollationCheck(string databaseCollation)
411415
{
416+
if(string.IsNullOrEmpty (databaseCollation))
417+
{
418+
return;
419+
}
420+
412421
bool isValid = databaseCollation.All( (c) =>
413422
{
414423
if(!char.IsLetterOrDigit(c) && c != '_')

0 commit comments

Comments
 (0)