Skip to content

Commit 6f3380c

Browse files
committed
Adding additional tests.
1 parent 5f450fe commit 6f3380c

File tree

7 files changed

+1321
-91
lines changed

7 files changed

+1321
-91
lines changed

src/ServiceManagement/Sql/Commands.SqlDatabase.Test/Resources/TSqlMockSessions/SqlAuthv12MockTests.xml

Lines changed: 1110 additions & 3 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ public static void CreateServerContextSqlAuthV2(
365365
CultureInfo.InvariantCulture,
366366
@"{1} = New-AzureSqlDatabaseServerContext " +
367367
@"-ManageUrl {0} " +
368-
@"-Credential $credential " +
369-
@"-Version 12.0 ",
368+
@"-Credential $credential ",
370369
manageUrl,
371370
contextVariable),
372371
contextVariable);

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

Lines changed: 171 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.Database.Cmdlet
2727
{
28-
[RecordMockDataResults("./")]
2928
[TestClass]
3029
public class SqlAuthv12MockTests
3130
{
@@ -36,8 +35,6 @@ public class SqlAuthv12MockTests
3635
[TestInitialize]
3736
public void Setup()
3837
{
39-
var mockConn = new MockSqlConnection();
40-
TSqlConnectionContext.MockSqlConnection = mockConn;
4138
}
4239

4340
[TestCleanup]
@@ -46,9 +43,13 @@ public void Cleanup()
4643
// Do any test clean up here.
4744
}
4845

46+
//[RecordMockDataResults("./")]
4947
[TestMethod]
5048
public void NewAzureSqlDatabaseWithSqlAuthv12()
5149
{
50+
var mockConn = new MockSqlConnection();
51+
TSqlConnectionContext.MockSqlConnection = mockConn;
52+
5253
using (System.Management.Automation.PowerShell powershell =
5354
System.Management.Automation.PowerShell.Create())
5455
{
@@ -121,6 +122,171 @@ public void NewAzureSqlDatabaseWithSqlAuthv12()
121122
}
122123

123124

125+
//[RecordMockDataResults("./")]
126+
[TestMethod]
127+
public void GetAzureSqlDatabaseWithSqlAuthv12()
128+
{
129+
var mockConn = new MockSqlConnection();
130+
TSqlConnectionContext.MockSqlConnection = mockConn;
131+
132+
using (System.Management.Automation.PowerShell powershell =
133+
System.Management.Automation.PowerShell.Create())
134+
{
135+
// Create a context
136+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
137+
powershell,
138+
manageUrl,
139+
username,
140+
password,
141+
"$context");
142+
143+
Collection<PSObject> database1, database2, database3;
144+
145+
database1 = powershell.InvokeBatchScript(
146+
@"$testdb1 = Get-AzureSqlDatabase " +
147+
@"-Context $context " +
148+
@"-DatabaseName testdb1 ",
149+
@"$testdb1");
150+
database2 = powershell.InvokeBatchScript(
151+
@"$testdb2 = Get-AzureSqlDatabase " +
152+
@"-Context $context " +
153+
@"-Database $testdb1 ",
154+
@"$testdb2");
155+
database3 = powershell.InvokeBatchScript(
156+
@"$testdb3 = Get-AzureSqlDatabase " +
157+
@"-Context $context ",
158+
@"$testdb3");
159+
160+
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
161+
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
162+
powershell.Streams.ClearStreams();
163+
164+
Services.Server.Database database = database1.Single().BaseObject as Services.Server.Database;
165+
Assert.IsTrue(database != null, "Expecting a Database object");
166+
ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
167+
168+
database = database2.Single().BaseObject as Services.Server.Database;
169+
Assert.IsTrue(database != null, "Expecting a Database object");
170+
ValidateDatabaseProperties(database, "testdb1", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
171+
172+
Assert.IsTrue(database3.Count == 5);
173+
foreach (var entry in database3)
174+
{
175+
var db = entry.BaseObject as Services.Server.Database;
176+
Assert.IsTrue(db != null, "Expecting a Database object");
177+
}
178+
}
179+
}
180+
181+
//[RecordMockDataResults("./")]
182+
[TestMethod]
183+
public void SetAzureSqlDatabaseWithSqlAuthv12()
184+
{
185+
var mockConn = new MockSqlConnection();
186+
TSqlConnectionContext.MockSqlConnection = mockConn;
187+
188+
using (System.Management.Automation.PowerShell powershell =
189+
System.Management.Automation.PowerShell.Create())
190+
{
191+
// Create a context
192+
NewAzureSqlDatabaseServerContextTests.CreateServerContextSqlAuthV2(
193+
powershell,
194+
manageUrl,
195+
username,
196+
password,
197+
"$context");
198+
199+
Collection<PSObject> database1, database2, database3, database4;
200+
201+
database1 = powershell.InvokeBatchScript(
202+
@"$testdb1 = Set-AzureSqlDatabase " +
203+
@"-Context $context " +
204+
@"-DatabaseName testdb1 " +
205+
@"-Edition Basic " +
206+
@"-MaxSizeGb 1 " +
207+
@"-Force " +
208+
@"-PassThru ",
209+
@"$testdb1");
210+
database2 = powershell.InvokeBatchScript(
211+
@"$testdb2 = Set-AzureSqlDatabase " +
212+
@"-Context $context " +
213+
@"-DatabaseName testdb2 " +
214+
@"-Edition Standard " +
215+
@"-MaxSizeBytes 107374182400 " +
216+
@"-Force " +
217+
@"-PassThru ",
218+
@"$testdb2");
219+
database3 = powershell.InvokeBatchScript(
220+
@"$testdb3 = Set-AzureSqlDatabase " +
221+
@"-Context $context " +
222+
@"-DatabaseName testdb3 " +
223+
@"-NewDatabaseName testdb3alt " +
224+
@"-Force " +
225+
@"-PassThru ",
226+
@"$testdb3");
227+
var slo = powershell.InvokeBatchScript(
228+
@"$so = Get-AzureSqlDatabaseServiceObjective " +
229+
@"-Context $context " +
230+
@"-ServiceObjectiveName S0 ",
231+
@"$so");
232+
database4 = powershell.InvokeBatchScript(
233+
@"$testdb4 = Set-AzureSqlDatabase " +
234+
@"-Context $context " +
235+
@"-DatabaseName testdb4 " +
236+
@"-ServiceObjective $so " +
237+
@"-Force " +
238+
@"-PassThru ",
239+
@"$testdb4");
240+
241+
//
242+
// Wait for operations to complete
243+
//
244+
245+
database1 = powershell.InvokeBatchScript(
246+
@"$testdb1 = Get-AzureSqlDatabase " +
247+
@"-Context $context " +
248+
@"-DatabaseName testdb1 ",
249+
@"$testdb1");
250+
database2 = powershell.InvokeBatchScript(
251+
@"$testdb2 = Get-AzureSqlDatabase " +
252+
@"-Context $context " +
253+
@"-DatabaseName testdb2 ",
254+
@"$testdb2");
255+
database3 = powershell.InvokeBatchScript(
256+
@"$testdb3 = Get-AzureSqlDatabase " +
257+
@"-Context $context " +
258+
@"-DatabaseName testdb3alt ",
259+
@"$testdb3");
260+
database4 = powershell.InvokeBatchScript(
261+
@"$testdb4 = Get-AzureSqlDatabase " +
262+
@"-Context $context " +
263+
@"-DatabaseName testdb4 ",
264+
@"$testdb4");
265+
266+
Assert.AreEqual(0, powershell.Streams.Error.Count, "Errors during run!");
267+
Assert.AreEqual(0, powershell.Streams.Warning.Count, "Warnings during run!");
268+
powershell.Streams.ClearStreams();
269+
270+
Services.Server.Database database = database1.Single().BaseObject as Services.Server.Database;
271+
Assert.IsTrue(database != null, "Expecting a Database object");
272+
ValidateDatabaseProperties(database, "testdb1", "Basic", 1, 1073741824L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.BasicSloGuid);
273+
274+
database = database2.Single().BaseObject as Services.Server.Database;
275+
Assert.IsTrue(database != null, "Expecting a Database object");
276+
ValidateDatabaseProperties(database, "testdb2", "Standard", 100, 107374182400L, "Japanese_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
277+
278+
database = database3.Single().BaseObject as Services.Server.Database;
279+
Assert.IsTrue(database != null, "Expecting a Database object");
280+
ValidateDatabaseProperties(database, "testdb3alt", "Standard", 100, 107374182400L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
281+
282+
database = database4.Single().BaseObject as Services.Server.Database;
283+
Assert.IsTrue(database != null, "Expecting a Database object");
284+
ValidateDatabaseProperties(database, "testdb4", "Standard", 250, 268435456000L, "SQL_Latin1_General_CP1_CI_AS", false, DatabaseTestHelper.StandardS0SloGuid);
285+
}
286+
}
287+
288+
#region Helpers
289+
124290
/// <summary>
125291
/// Validate the properties of a database against the expected values supplied as input.
126292
/// </summary>
@@ -148,5 +314,7 @@ internal static void ValidateDatabaseProperties(
148314
Assert.AreEqual(isSystem, database.IsSystemObject);
149315
// Assert.AreEqual(slo, database.ServiceObjectiveId);
150316
}
317+
318+
#endregion
151319
}
152320
}

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ namespace Microsoft.WindowsAzure.Commands.SqlDatabase.Test.UnitTests.TSql
2626
{
2727
internal sealed class MockSettings
2828
{
29+
/// <summary>
30+
/// Gets or sets the id for the mock
31+
/// </summary>
2932
private string mockId;
3033

3134
/// <summary>
@@ -143,8 +146,6 @@ from StackFrame frame in stackFrames
143146
if (testMethodFrame != null)
144147
{
145148
settings.mockId = GetMockId(testMethodFrame);
146-
//settings.initializeMethod = FindMockSetupMethod<RecordMockInitializeAttribute>(testMethodFrame);
147-
//settings.cleanupMethod = FindMockSetupMethod<RecordMockCleanupAttribute>(testMethodFrame);
148149

149150
RecordMockDataResultsAttribute recordAttr = FindRecordMockDataResultsAttribute(testMethodFrame);
150151
if (recordAttr != null)
@@ -197,40 +198,16 @@ private static RecordMockDataResultsAttribute FindRecordMockDataResultsAttribute
197198
return recordAttr;
198199
}
199200

200-
//private static SetupMethodDelegate FindMockSetupMethod<T>(StackFrame testMethodFrame)
201-
// where T : Attribute
202-
//{
203-
// Type declaringType = testMethodFrame.GetMethod().DeclaringType;
204-
// MethodInfo[] methods = declaringType.GetMethods();
205-
206-
// foreach (MethodInfo method in methods)
207-
// {
208-
// if (method.GetCustomAttribute<T>() != null)
209-
// {
210-
// if (!method.IsStatic)
211-
// {
212-
// throw new NotSupportedException("Non-static mock setup method are not supported.");
213-
// }
214-
215-
// return delegate(SqlConnection connection)
216-
// {
217-
// method.Invoke(null, new object[] { connection });
218-
// };
219-
// }
220-
// }
221-
222-
// return null;
223-
//}
224-
225201
private static string GetMockId(StackFrame testMethodFrame)
226202
{
227203
List<string> parts = new List<string>();
228204

205+
parts.Insert(0, testMethodFrame.GetMethod().Name);
229206
for (Type type = testMethodFrame.GetMethod().DeclaringType; type != null; type = type.DeclaringType)
230207
{
231208
parts.Insert(0, type.Name);
232209
}
233-
210+
234211
return String.Join(".", parts.ToArray());
235212
}
236213
}

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

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,28 @@ public override bool DesignTimeVisible
173173
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
174174
{
175175
Assert.IsTrue((this.Connection.State & ConnectionState.Open) == ConnectionState.Open, "Connection has to be opened when executing a command");
176+
177+
MockQueryResult mockResult = null;
176178

177-
string commandKey = this.GetCommandKey();
178-
MockQueryResult mockResult = FindMockResult(this.settings.MockId, this.Connection.Database, commandKey, this.settings.IsolatedQueries);
179-
180-
if (mockResult == null && this.settings.RecordingMode)
179+
if (this.settings.RecordingMode)
181180
{
182181
mockResult = this.RecordExecuteDbDataReader();
183182
}
184-
185-
if (mockResult == null || mockResult.DataSetResult == null)
183+
else
186184
{
187-
if (mockResult != null && mockResult.ExceptionResult != null)
188-
{
189-
throw mockResult.ExceptionResult.Exception;
190-
}
191-
else
185+
string commandKey = this.GetCommandKey();
186+
mockResult = FindMockResult(this.settings.MockId, this.Connection.Database, commandKey, this.settings.IsolatedQueries);
187+
188+
if (mockResult == null || mockResult.DataSetResult == null)
192189
{
193-
throw new NotSupportedException(string.Format("Mock SqlConnection does not know how to handle query: '{0}'", commandKey));
190+
if (mockResult != null && mockResult.ExceptionResult != null)
191+
{
192+
throw mockResult.ExceptionResult.Exception;
193+
}
194+
else
195+
{
196+
throw new NotSupportedException(string.Format("Mock SqlConnection does not know how to handle query: '{0}'", commandKey));
197+
}
194198
}
195199
}
196200

@@ -244,13 +248,17 @@ public override object ExecuteScalar()
244248
{
245249
Assert.IsTrue((this.Connection.State & ConnectionState.Open) == ConnectionState.Open, "Connection has to be opened when executing command");
246250

247-
string commandKey = this.GetCommandKey();
248-
MockQueryResult mockResult = FindMockResult(this.settings.MockId, this.Connection.Database, commandKey, this.settings.IsolatedQueries);
251+
MockQueryResult mockResult = null;
249252

250-
if (mockResult == null && this.settings.RecordingMode)
253+
if (this.settings.RecordingMode)
251254
{
252255
mockResult = this.RecordExecuteScalar();
253256
}
257+
else
258+
{
259+
string commandKey = this.GetCommandKey();
260+
FindMockResult(this.settings.MockId, this.Connection.Database, commandKey, this.settings.IsolatedQueries);
261+
}
254262

255263
return mockResult != null ? mockResult.ScalarResult : null;
256264
}
@@ -350,7 +358,14 @@ private MockQueryResult RecordExecuteDbDataReader()
350358
foreach (DbParameter param in this.Parameters)
351359
{
352360
SqlParameter sqlParam = new SqlParameter(param.ParameterName, param.DbType);
353-
sqlParam.Value = param.Value;
361+
if (param.Value == null)
362+
{
363+
sqlParam.Value = DBNull.Value;
364+
}
365+
else
366+
{
367+
sqlParam.Value = param.Value;
368+
}
354369
cmd.Parameters.Add(sqlParam);
355370
}
356371

@@ -473,7 +488,7 @@ private string GetCommandKey()
473488
key = key.Replace(parameter.ParameterName, value);
474489
}
475490

476-
key = key.Replace("\r", string.Empty).Replace("\n", Environment.NewLine);
491+
//key = key.Replace("\r", string.Empty).Replace("\n", Environment.NewLine);
477492

478493
key = TempTableNameRegex.Replace(key, TempTableName);
479494

@@ -599,25 +614,6 @@ private static void InitializeMockResults()
599614
}
600615
}
601616
}
602-
603-
//foreach (string rn in resourceNames)
604-
//{
605-
// using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(rn))
606-
// {
607-
// if (stream.Length > 0)
608-
// {
609-
// MockQueryResultSet mockResultSet = MockQueryResultSet.Deserialize(stream);
610-
611-
// if (mockResultSet.CommandResults != null)
612-
// {
613-
// foreach (MockQueryResult mockResult in mockResultSet.CommandResults)
614-
// {
615-
// AddMockResult(mockResult);
616-
// }
617-
// }
618-
// }
619-
// }
620-
//}
621617
}
622618

623619
#endregion

0 commit comments

Comments
 (0)