-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Seddryck/feature/improve_code_coverage
Improve code coverage and code factor
- Loading branch information
Showing
37 changed files
with
679 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Pgnoli.Testing/Messages/Backend/Query/ErrorResponseTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Pgnoli.Messages; | ||
using Pgnoli.Messages.Backend.Query; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pgnoli.Testing.Messages.Backend.Query | ||
{ | ||
public class ErrorResponseTest | ||
{ | ||
[Test] | ||
public void Roundtrip_Ok_Success() | ||
{ | ||
var msg = ErrorResponse.Warning("SQL00001", "!Oups!").With('q', "SELECT @@version").Build(); | ||
var bytes = msg.GetBytes(); | ||
Assert.That(bytes, Is.Not.Null); | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(bytes, Has.Length.EqualTo(51)); | ||
Assert.That(bytes[0], Is.EqualTo('E')); | ||
Assert.That(bytes[50], Is.EqualTo(0)); | ||
}); | ||
|
||
var roundtrip = new ErrorResponse(bytes); | ||
Assert.DoesNotThrow(() => roundtrip.Read()); | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(roundtrip.Payload.Severity, Is.EqualTo(msg.Payload.Severity)); | ||
Assert.That(roundtrip.Payload.SqlState, Is.EqualTo(msg.Payload.SqlState)); | ||
Assert.That(roundtrip.Payload.Message, Is.EqualTo(msg.Payload.Message)); | ||
Assert.That(roundtrip.Payload.OptionalMessages, Has.Count.EqualTo(msg.Payload.OptionalMessages.Count)); | ||
foreach (var option in roundtrip.Payload.OptionalMessages) | ||
{ | ||
Assert.That(msg.Payload.OptionalMessages.ContainsKey(option.Key), Is.True); | ||
Assert.That(roundtrip.Payload.OptionalMessages[option.Key], Is.EqualTo(msg.Payload.OptionalMessages[option.Key])); | ||
} | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Write_Default_Success() | ||
{ | ||
var msg = ErrorResponse.Warning("SQL00001", "!Oups!").With('q', "SELECT @@version").Build(); | ||
|
||
var bytes = msg.GetBytes(); | ||
|
||
var reader = new ResourceBytesReader(); | ||
Assert.That(bytes, Is.EqualTo(reader.Read("Backend.Query.ErrorResponse.Default"))); | ||
} | ||
|
||
[Test] | ||
public void Read_Default_Success() | ||
{ | ||
var reader = new ResourceBytesReader(); | ||
var bytes = reader.Read("Backend.Query.ErrorResponse.Default"); | ||
var msg = new ErrorResponse(bytes); | ||
|
||
Assert.DoesNotThrow(() => msg.Read()); | ||
Assert.That(msg.Payload.Severity, Is.EqualTo(ErrorSeverity.Warning)); | ||
Assert.That(msg.Payload.SqlState, Is.EqualTo("SQL00001")); | ||
Assert.That(msg.Payload.Message, Is.EqualTo("!Oups!")); | ||
Assert.That(msg.Payload.OptionalMessages.ContainsKey('q'), Is.True); | ||
Assert.That(msg.Payload.OptionalMessages['q'], Is.EqualTo("SELECT @@version")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Pgnoli.Messages; | ||
using Pgnoli.Messages.Backend.Handshake; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pgnoli.Testing.Messages | ||
{ | ||
public class CodeMessageTest | ||
{ | ||
private class StubMessage : CodeMessage | ||
{ | ||
public const char Code = '='; | ||
|
||
public StubMessage(byte[] bytes) | ||
: base(Code, bytes) { } | ||
|
||
protected override int GetPayloadLength() => 0; | ||
protected internal override void ReadPayload(Buffer buffer) { } | ||
protected internal override void WritePayload(Buffer buffer) { } | ||
} | ||
|
||
private class StubBrokenMessage : StubMessage | ||
{ | ||
public StubBrokenMessage(byte[] bytes) | ||
: base(bytes) { } | ||
|
||
protected override int GetPayloadLength() => 1; | ||
} | ||
|
||
[Test] | ||
public void Read_ValidBytes_DoesNotThrow() | ||
{ | ||
var bytes = new Byte[] { Convert.ToByte(StubMessage.Code), 0, 0, 0, 4 }; | ||
var msg = new StubMessage(bytes); | ||
Assert.DoesNotThrow(() => msg.Read()); | ||
} | ||
|
||
[Test] | ||
public void Read_UnexpectedCode_Throws() | ||
{ | ||
var bytes = new Byte[] { (byte)(Convert.ToByte(StubMessage.Code)-1), 0, 0, 0, 4 }; | ||
var msg = new StubMessage(bytes); | ||
Assert.Throws<MessageMismatchCodeException>(() => msg.Read()); | ||
} | ||
|
||
[Test] | ||
public void Read_UnexpectedLength_Throws() | ||
{ | ||
var bytes = new Byte[] { Convert.ToByte(StubMessage.Code), 0, 0, 0, 20 }; | ||
var msg = new StubMessage(bytes); | ||
Assert.Throws<MessageUnexpectedLengthException>(() => msg.Read()); | ||
} | ||
|
||
[Test] | ||
public void Read_MessageNotFullyConsumedException_Throws() | ||
{ | ||
var bytes = new Byte[] { Convert.ToByte(StubMessage.Code), 0, 0, 0, 5, 1 }; | ||
var msg = new StubBrokenMessage(bytes); | ||
Assert.Throws<MessageNotFullyConsumedException>(() => msg.Read()); | ||
} | ||
|
||
|
||
[Test] | ||
public void Read_TooLongBuffer_TrimmedDoesNotThrow() | ||
{ | ||
var bytes = new Byte[] { Convert.ToByte(StubMessage.Code), 0, 0, 0, 4, 1, 0, 10 }; | ||
var msg = new StubMessage(bytes); | ||
Assert.DoesNotThrow(() => msg.Read()); | ||
Assert.That(msg.GetBytes(), Is.EqualTo(bytes[..(bytes[4]+1)])); | ||
} | ||
|
||
[Test] | ||
public void Read_EmptyBuffer_Throws() | ||
{ | ||
var bytes = Array.Empty<byte>(); | ||
var msg = new StubMessage(bytes); | ||
Assert.Throws<BufferEmptyException>(() => msg.Read()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Pgnoli.Options.DateStyles; | ||
using Pgnoli.Messages; | ||
using Pgnoli.Messages.Frontend; | ||
using Pgnoli.Messages.Frontend.Query; | ||
using Pgnoli.Types.TypeHandlers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection.PortableExecutable; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pgnoli.Testing.Messages.Frontend | ||
{ | ||
public class FrontendParserTest | ||
{ | ||
[Test] | ||
[TestCase("Frontend.Query.Bind.UnnamedPortal", typeof(Bind))] | ||
[TestCase("Frontend.Query.Close.UnnamedPortal", typeof(Close))] | ||
[TestCase("Frontend.Query.Describe.UnnamedPortal", typeof(Describe))] | ||
[TestCase("Frontend.Query.Execute.UnnamedPortal", typeof(Execute))] | ||
[TestCase("Frontend.Query.Query.To_timestamp", typeof(Pgnoli.Messages.Frontend.Query.Query))] | ||
[TestCase("Frontend.Query.Parse.To_timestamp", typeof(Parse))] | ||
[TestCase("Frontend.Query.Sync.Default", typeof(Sync))] | ||
public void Parse(string msgPath, Type expected) | ||
{ | ||
var reader = new ResourceBytesReader(); | ||
var bytes = reader.Read(msgPath); | ||
|
||
var parser = new FrontendParser(); | ||
var msg = parser.Parse(bytes, 0, out var length); | ||
Assert.That(msg, Is.TypeOf(expected)); | ||
Assert.That(length, Is.EqualTo(bytes.Length)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Pgnoli.Messages.Frontend.Query; | ||
|
||
namespace Pgnoli.Testing.Messages.Frontend.Query | ||
{ | ||
public class CloseTest | ||
{ | ||
[Test] | ||
public void Write_Default_Success() | ||
{ | ||
var msg = Close.UnnamedPortal.Build(); | ||
var bytes = msg.GetBytes(); | ||
|
||
var reader = new ResourceBytesReader(); | ||
Assert.That(bytes, Is.EqualTo(reader.Read("Frontend.Query.Close.UnnamedPortal"))); | ||
} | ||
|
||
[Test] | ||
public void Read_UnnamedPortal_Success() | ||
{ | ||
var reader = new ResourceBytesReader(); | ||
var bytes = reader.Read("Frontend.Query.Close.UnnamedPortal"); | ||
var msg = new Close(bytes); | ||
|
||
Assert.DoesNotThrow(() => msg.Read()); | ||
Assert.That(msg.Payload.PortalType, Is.EqualTo(PortalType.Portal)); | ||
Assert.That(msg.Payload.Name, Is.EqualTo(string.Empty)); | ||
} | ||
|
||
private static Close.CloseBuilder[] BuilderCases | ||
=> new[] | ||
{ | ||
Close.Portal("the_name"), | ||
Close.PreparedStatement("the_name"), | ||
Close.UnnamedPortal, | ||
Close.UnnamedPreparedStatement | ||
}; | ||
|
||
[Test] | ||
[TestCaseSource(nameof(BuilderCases))] | ||
public void Roundtrip_Close_Success(Close.CloseBuilder builder) | ||
{ | ||
var msg = builder.Build(); | ||
|
||
var bytes = msg.GetBytes(); | ||
Assert.That(bytes, Is.Not.Null); | ||
Assert.That(bytes, Has.Length.GreaterThan(0)); | ||
Assert.That(bytes[0], Is.EqualTo('C')); | ||
|
||
var roundtrip = new Close(bytes); | ||
Assert.DoesNotThrow(() => roundtrip.Read()); | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(msg.Payload.Name, Is.EqualTo(roundtrip.Payload.Name)); | ||
Assert.That(msg.Payload.PortalType, Is.EqualTo(roundtrip.Payload.PortalType)); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.