Files
sqltoolsservice/test/Microsoft.SqlTools.Hosting.UnitTests/ProtocolTests/RequestContextTests.cs
David Shiflet 839acf67cd Convert most tools service tests to nunit (#1037)
* Remove xunit dependency from testdriver

* swap expected/actual as needed

* Convert Test.Common to nunit

* port hosting unit tests to nunit

* port batchparser integration tests to nunit

* port testdriver.tests to nunit

* fix target to copy dependency

* port servicelayer unittests to nunit

* more unit test fixes

* port integration tests to nunit

* fix test method type

* try using latest windows build for PRs

* reduce test memory use
2020-08-05 13:43:14 -04:00

89 lines
3.7 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.SqlTools.Hosting.Contracts.Internal;
using Microsoft.SqlTools.Hosting.Protocol;
using NUnit.Framework;
namespace Microsoft.SqlTools.Hosting.UnitTests.ProtocolTests
{
[TestFixture]
public class RequestContextTests
{
#region Send Tests
[Test]
public void SendResult()
{
// Setup: Create a blocking collection to collect the output
var bc = new BlockingCollection<Message>(new ConcurrentQueue<Message>());
// If: I write a response with the request context
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
rc.SendResult(CommonObjects.TestMessageContents.DefaultInstance);
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.Response }), "The message writer should have sent a response");
}
[Test]
public void SendEvent()
{
// Setup: Create a blocking collection to collect the output
var bc = new BlockingCollection<Message>(new ConcurrentQueue<Message>());
// If: I write an event with the request context
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
rc.SendEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.Event }), "The message writer should have sent an event");
}
[Test]
public void SendError()
{
// Setup: Create a blocking collection to collect the output
const string errorMessage = "error";
const int errorCode = 123;
var bc = new BlockingCollection<Message>(new ConcurrentQueue<Message>());
// If: I write an error with the request context
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
rc.SendError(errorMessage, errorCode);
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.ResponseError }), "The message writer should have sent an error");
// ... The error object it built should have the reuired fields set
var contents = bc.ToArray()[0].GetTypedContents<Error>();
Assert.AreEqual(errorCode, contents.Code);
Assert.AreEqual(errorMessage, contents.Message);
}
[Test]
public void SendException()
{
// Setup: Create a blocking collection to collect the output
var bc = new BlockingCollection<Message>(new ConcurrentQueue<Message>());
// If: I write an error as an exception with the request context
const string errorMessage = "error";
var e = new Exception(errorMessage);
var rc = new RequestContext<CommonObjects.TestMessageContents>(CommonObjects.RequestMessage, bc);
rc.SendError(e);
Assert.That(bc.Select(m => m.MessageType), Is.EqualTo(new[] { MessageType.ResponseError }), "The message writer should have sent an error");
// ... The error object it built should have the reuired fields set
var contents = bc.First().GetTypedContents<Error>();
Assert.AreEqual(e.HResult, contents.Code);
Assert.AreEqual(errorMessage, contents.Message);
}
#endregion
}
}