Skip to content

Commit

Permalink
Fixing some unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
akromm-zz committed Dec 8, 2014
1 parent 9d24f0a commit 5ebae77
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Microsoft.WindowsAzure.Commands.SqlDatabase.Test.Utilities;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server;

namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.Database.Cmdlet
{
Expand Down Expand Up @@ -112,6 +113,10 @@ public void ImportExportAzureSqlDatabaseTests()
@" -StorageAccountKey $storageAccountKey");
}).FirstOrDefault();

// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
//
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;

//testSession.ServiceBaseUri = new Uri("https://lqtqbo6kkp.database.windows.net");
Collection<PSObject> databaseContext = MockServerHelper.ExecuteWithMock(
testSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public void NewAzureSqlDatabaseServerContextWithSqlAuth()
UnitTestHelper.ImportAzureModule(powershell);
UnitTestHelper.CreateTestCredential(powershell);

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

using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
{
// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
//
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;

// Test warning when different $metadata is received.
Collection<PSObject> serverContext;
using (new MockHttpServer(
Expand All @@ -212,6 +219,10 @@ public void NewAzureSqlDatabaseServerContextWithSqlAuthNegativeCases()
Assert.AreEqual(2, powershell.Streams.Warning.Count, "Should have warning!");
powershell.Streams.ClearStreams();

// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
//
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;

// Test error case
using (new MockHttpServer(
exceptionManager,
Expand Down Expand Up @@ -308,6 +319,10 @@ public static void CreateServerContextSqlAuth(
testSession.SessionProperties["Username"],
testSession.SessionProperties["Password"]);

// Tell the sql auth factory to create a v2 context (skip checking sql version using select query).
//
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v2;

Collection<PSObject> serverContext;
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
{
Expand Down Expand Up @@ -344,7 +359,7 @@ public static void CreateServerContextSqlAuth(
/// Common helper method for other tests to create a context for ESA server.
/// </summary>
/// <param name="contextVariable">The variable name that will hold the new context.</param>
public static void CreateServerContextSqlAuthV2(
public static void CreateServerContextSqlAuthV12(
System.Management.Automation.PowerShell powershell,
string manageUrl,
string username,
Expand All @@ -357,6 +372,10 @@ public static void CreateServerContextSqlAuthV2(
username,
password);

// Tell the sql auth factory to create a v22 context (skip checking sql version using select query).
//
SqlAuthContextFactory.sqlVersion = SqlAuthContextFactory.SqlVersion.v12;

Collection<PSObject> serverContext;
using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// ---

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

[TestInitialize]
public void Setup()
Expand All @@ -54,7 +55,7 @@ public void NewAzureSqlDatabaseWithSqlAuthv12()
System.Management.Automation.PowerShell.Create())
{
// Create a context
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
powershell,
manageUrl,
username,
Expand Down Expand Up @@ -121,7 +122,6 @@ public void NewAzureSqlDatabaseWithSqlAuthv12()
}
}


//[RecordMockDataResults("./")]
[TestMethod]
public void GetAzureSqlDatabaseWithSqlAuthv12()
Expand All @@ -133,7 +133,7 @@ public void GetAzureSqlDatabaseWithSqlAuthv12()
System.Management.Automation.PowerShell.Create())
{
// Create a context
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
powershell,
manageUrl,
username,
Expand Down Expand Up @@ -189,7 +189,7 @@ public void SetAzureSqlDatabaseWithSqlAuthv12()
System.Management.Automation.PowerShell.Create())
{
// Create a context
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV12(
powershell,
manageUrl,
username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ private AzureSubscription CurrentSubscription

#endregion


/// <summary>
/// Connect to a Azure SQL Server with the given ManagementService Uri using
/// SQL authentication credentials.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.WindowsAzure.Commands.SqlDatabase.Properties;
using Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Common;
using System;
using System.Collections.Generic;
Expand All @@ -14,6 +28,38 @@ namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Services.Server
{
public class SqlAuthContextFactory
{
/// <summary>
/// The different sql versions available
/// </summary>
internal enum SqlVersion
{
/// <summary>
/// Not set. Determine by querying the server
/// </summary>
None,

/// <summary>
/// V2 server
/// </summary>
v2,

/// <summary>
/// V12 server
/// </summary>
v12
}
internal static SqlVersion sqlVersion = SqlVersion.None;

/// <summary>
/// Gets a sql auth connection context.
/// </summary>
/// <param name="cmdlet">The cmdlet requesting the context</param>
/// <param name="serverName">The name of the server to connect to</param>
/// <param name="manageUrl">The manage url of the server</param>
/// <param name="credentials">The credentials to connect to the server</param>
/// <param name="sessionActivityId">The session activity ID</param>
/// <param name="managementServiceUri">The URI for management service</param>
/// <returns>The connection context</returns>
public static IServerDataServiceContext GetContext(
PSCmdlet cmdlet,
string serverName,
Expand All @@ -22,7 +68,22 @@ public static IServerDataServiceContext GetContext(
Guid sessionActivityId,
Uri managementServiceUri)
{
Version version = GetVersion(manageUrl, credentials);
Version version;

// If a version was specified (by tests) us it.
if (sqlVersion == SqlVersion.v2)
{
version = new Version(11, 0);
}
else if (sqlVersion == SqlVersion.v12)
{
version = new Version(12, 0);
}
else // If no version specified, determine the version by querying the server.
{
version = GetVersion(manageUrl, credentials);
}
sqlVersion = SqlVersion.None;

IServerDataServiceContext context = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,17 @@ public Database CreateNewDatabase(
return GetDatabase(databaseName);
}

/// <summary>
/// Checks to make sure the collation only contains alphanumeric characters and '_'
/// </summary>
/// <param name="databaseCollation">The string to verify</param>
private void SqlCollationCheck(string databaseCollation)
{
if(string.IsNullOrEmpty (databaseCollation))
{
return;
}

bool isValid = databaseCollation.All( (c) =>
{
if(!char.IsLetterOrDigit(c) && c != '_')
Expand Down

0 comments on commit 5ebae77

Please sign in to comment.