Skip to content

Commit

Permalink
Provide the ability to run ParatextChecks and cleanup JSON serializing (
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil authored Aug 16, 2024
1 parent c9a163c commit a8e19f8
Show file tree
Hide file tree
Showing 36 changed files with 1,084 additions and 410 deletions.
75 changes: 75 additions & 0 deletions c-sharp-tests/Checks/CheckInputRangeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Paranext.DataProvider.Checks;
using SIL.Scripture;

namespace TestParanextDataProvider.Checks;

public class CheckInputRangeTests
{
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 1, 4, true)] // In range (single book)
[TestCase("pid1", "GEN 1:1", "EXO 10:1", "pid1", 1, 4, true)] // In range (multiple books)
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid2", 1, 4, false)] // Different project
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 2, 4, false)] // Book outside range (single book)
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 1, 11, false)] // Chapter after range
[TestCase("pid1", "GEN 2:1", "GEN 10:1", "pid1", 1, 1, false)] // Chapter before range
[TestCase("pid1", "GEN 1:1", null, "pid1", 1, 11, true)] // No end, normal in range
[TestCase("pid1", "GEN 1:1", null, "pid1", 2, 11, false)] // No end, different book
[TestCase("pid1", "GEN 1:1", "LEV 10:1", "pid1", 5, 1, false)] // Book after range (multiple books)
[TestCase("pid1", "EXO 1:1", "LEV 10:1", "pid1", 1, 1, false)] // Book before range (multiple books)
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 0, 4, true)] // Book 0, chapter in range
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 0, 20, true)] // Book 0, chapter outside of range
[TestCase("pid1", "GEN 1:1", "GEN 10:1", "pid1", 1, 0, true)] // Book in range, Chapter 0
public void IsWithinRange_IdentifiesOutOfRange(
string verseRefProjectId,
string verseRefStart,
string? verseRefEnd,
string projectId,
int bookNum,
int chapterNum,
bool expectedResult
)
{
VerseRef vrefStart = new(verseRefStart);
VerseRef? vrefEnd = string.IsNullOrEmpty(verseRefEnd) ? null : new VerseRef(verseRefEnd);
CheckInputRange checkInputRange = new(verseRefProjectId, vrefStart, vrefEnd);
Assert.That(
checkInputRange.IsWithinRange(projectId, bookNum, chapterNum),
Is.EqualTo(expectedResult)
);
}

[TestCase(null, 1, 1, 1, 1, "projectId")]
[TestCase("", 1, 1, 1, 1, "empty string")]
[TestCase("projectId", 1, 0, 1, 10, "start.ChapterNum must be > 0")]
[TestCase("projectId", 1, 1, 1, 0, "end.ChapterNum must be > 0")]
[TestCase("projectId", 3, 1, 1, 1, "end must come after start")]
public void CheckInputRange_Constructor_ThrowsWithBadInputs(
string? projectId,
int bookNumStart,
int chapterNumStart,
int? bookNumEnd,
int? chapterNumEnd,
string expectedPartialMessage
)
{
string exceptionMessage = string.Empty;
try
{
var checkInputRange = new CheckInputRange(
projectId!,
new VerseRef(bookNumStart, chapterNumStart, 1),
bookNumEnd.HasValue && chapterNumEnd.HasValue
? new VerseRef(bookNumEnd.Value, chapterNumEnd.Value, 1)
: null
);

// This line is to just get linters to not complain about unused variables
checkInputRange.ToString();
}
catch (ArgumentException ex)
{
exceptionMessage = ex.Message;
}

Assert.That(exceptionMessage, Does.Contain(expectedPartialMessage));
}
}
27 changes: 27 additions & 0 deletions c-sharp-tests/Checks/CheckLocationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Paranext.DataProvider.Checks;
using SIL.Scripture;

namespace TestParanextDataProvider.Checks;

public class CheckLocationTests
{
[TestCase("GEN 1:1", 2, "GEN 1:1", 2, true)]
[TestCase("GEN 1:1", 2, "NUM 1:1", 2, false)]
[TestCase("GEN 1:1", 1, "GEN 1:1", 2, false)]
public void Equality_Objects_ComparedByValue(
string verseRef1,
int offset1,
string verseRef2,
int offset2,
bool expectedResult
)
{
VerseRef vref1 = new(verseRef1);
CheckLocation item1 = new(vref1, offset1);
VerseRef vref2 = new(verseRef2);
CheckLocation item2 = new(vref2, offset2);
Assert.That(item1 == item2, Is.EqualTo(expectedResult));
Assert.That(item1.Equals(item2), Is.EqualTo(expectedResult));
Assert.That(item1.GetHashCode() == item2.GetHashCode(), Is.EqualTo(expectedResult));
}
}
46 changes: 46 additions & 0 deletions c-sharp-tests/Checks/CheckRunResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Paranext.DataProvider.Checks;
using SIL.Scripture;

namespace TestParanextDataProvider.Checks;

public class CheckRunResultTests
{
[TestCase("checkId", "projectId", "message", "GEN 1:1", 1, "GEN 1:2", 5, true)]
[TestCase("ABC", "projectId", "message", "GEN 1:1", 1, "GEN 1:2", 5, false)]
[TestCase("checkId", "ABC", "message", "GEN 1:1", 1, "GEN 1:2", 5, false)]
[TestCase("checkId", "projectId", "ABC", "GEN 1:1", 1, "GEN 1:2", 5, false)]
[TestCase("checkId", "projectId", "message", "GEN 1:2", 1, "GEN 1:2", 5, false)]
[TestCase("checkId", "projectId", "message", "GEN 1:1", 2, "GEN 1:2", 5, false)]
[TestCase("checkId", "projectId", "message", "GEN 1:1", 1, "GEN 1:3", 5, false)]
[TestCase("checkId", "projectId", "message", "GEN 1:1", 1, "GEN 1:2", 7, false)]
public void Equality_Objects_ComparedByValue(
string checkId2,
string projectId2,
string message2,
string verseRefStart2,
int offsetStart2,
string verseRefEnd2,
int offsetEnd2,
bool expectedResult
)
{
VerseRef vrefStart1 = new("GEN 1:1");
CheckLocation start1 = new(vrefStart1, 1);
VerseRef vrefEnd1 = new("GEN 1:2");
CheckLocation end1 = new(vrefEnd1, 5);
CheckRunResult checkRunResult1 = new("checkId", "projectId", "message", start1, end1);

VerseRef vrefStart2 = new(verseRefStart2);
CheckLocation start2 = new(vrefStart2, offsetStart2);
VerseRef vrefEnd2 = new(verseRefEnd2);
CheckLocation end2 = new(vrefEnd2, offsetEnd2);
CheckRunResult checkRunResult2 = new(checkId2, projectId2, message2, start2, end2);

Assert.That(checkRunResult1 == checkRunResult2, Is.EqualTo(expectedResult));
Assert.That(checkRunResult1.Equals(checkRunResult2), Is.EqualTo(expectedResult));
Assert.That(
checkRunResult1.GetHashCode() == checkRunResult2.GetHashCode(),
Is.EqualTo(expectedResult)
);
}
}
21 changes: 21 additions & 0 deletions c-sharp-tests/Checks/ParatextCheckDetailsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Paranext.DataProvider.Checks;
using Paranext.DataProvider.JsonUtils;
using Paratext.Data.Checking;

namespace TestParanextDataProvider.Checks;

public class ParatextCheckDetailsTests
{
[Test]
public void Serialization_Json_MatchesExpectations()
{
var checkDetails = new ParatextCheckDetails(CheckType.Capitalization);
checkDetails.EnabledProjectIds.Add("testProjectId");
Assert.That(
checkDetails.SerializeToJson(),
Is.EqualTo(
"""{"checkName":"Capitalization","checkDescription":"Capitalization","checkId":"Capitalization","enabledProjectIds":["testProjectId"]}"""
)
);
}
}
3 changes: 2 additions & 1 deletion c-sharp-tests/DummyPapiClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Paranext.DataProvider.JsonUtils;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.Messages;
using Paranext.DataProvider.MessageTransports;
Expand Down Expand Up @@ -221,7 +222,7 @@ out var validValues
// failure. Can keep the original values

await Task.Run(
() => responseCallback(success, JsonSerializer.SerializeToElement(result))
() => responseCallback(success, result.SerializeToJsonElement())
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion c-sharp-tests/DummyScrText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DummyScrText(ProjectDetails projectDetails)
RegistrationInfo.DefaultUser
)
{
_id = HexId.FromStr(projectDetails.Metadata.ID);
_id = HexId.FromStr(projectDetails.Metadata.Id);
projectName = new ProjectName
{
ShortName = projectDetails.Name + _id,
Expand Down
21 changes: 4 additions & 17 deletions c-sharp-tests/JsonUtils/MessageConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Paranext.DataProvider.JsonUtils;
using Paranext.DataProvider.Messages;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

namespace TestParanextDataProvider.JsonUtils;

Expand Down Expand Up @@ -62,14 +61,12 @@ public void Deserialize_UnknownEventType_ProducesMessageEvent()
private static MessageType DeserializeMessageEvent<MessageType>(string messageToDecode)
where MessageType : MessageEvent
{
JsonSerializerOptions so = JsonSerializerOptionsForTesting;

var msg = JsonSerializer.Deserialize<MessageType>(messageToDecode, so);
var msg = messageToDecode.DeserializeFromJson<MessageType>();
Assert.That(msg, Is.Not.Null);
Assert.That(msg!.Event, Is.Not.Null);

string reserializedMessage = JsonSerializer.Serialize(msg, so);
var msg2 = JsonSerializer.Deserialize<MessageType>(reserializedMessage, so);
string reserializedMessage = msg.SerializeToJson();
var msg2 = reserializedMessage.DeserializeFromJson<MessageType>();
Assert.That(msg2, Is.Not.Null);
Assert.That(msg2!.Event, Is.Not.Null);

Expand All @@ -82,14 +79,4 @@ private static MessageType DeserializeMessageEvent<MessageType>(string messageTo

return msg;
}

private static JsonSerializerOptions JsonSerializerOptionsForTesting
{
get
{
JsonSerializerOptions so = SerializationOptions.CreateSerializationOptions();
so.Converters.Add(new MessageConverter());
return so;
}
}
}
2 changes: 1 addition & 1 deletion c-sharp-tests/Projects/LocalParatextProjectsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Initialize_SingleProject_ProjectIsRetrievable(string folder, string
// ScrText always prioritizes the folder name over the Name setting as the "name" even when
// accessing scrText.Settings.Name. So basically name here doesn't get set to anything.
Assert.That(details.Name, Is.EqualTo(folder));
Assert.That(details.Metadata.ID.Equals(id, StringComparison.OrdinalIgnoreCase));
Assert.That(details.Metadata.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
}

private void CreateTempProject(string folder, ProjectDetails details)
Expand Down
20 changes: 10 additions & 10 deletions c-sharp-tests/Projects/ParatextDataProviderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using Newtonsoft.Json;
using Paranext.DataProvider.JsonUtils;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.Messages;
using Paranext.DataProvider.Projects;
Expand Down Expand Up @@ -80,21 +80,21 @@ public async Task GetFunctions_MissingParameter(string function)
0,
1,
0,
"Invalid VerseRef ({\"versification\":\"English\",\"_bookNum\":0,\"_chapterNum\":1,\"_verseNum\":0}): BookNum must be greater than zero and less than or equal to last book"
"SIL.Scripture.VerseRefException: BookNum must be greater than zero and less than or equal to last book"
)]
[TestCase(
"getChapterUSFM",
2,
-1,
0,
"Invalid VerseRef ({\"versification\":\"English\",\"_bookNum\":2,\"_chapterNum\":-1,\"_verseNum\":0}): ChapterNum can not be negative"
"SIL.Scripture.VerseRefException: ChapterNum can not be negative"
)]
[TestCase(
"getVerseUSFM",
2,
1,
-1,
"Invalid VerseRef ({\"versification\":\"English\",\"_bookNum\":2,\"_chapterNum\":1,\"_verseNum\":-1}): VerseNum can not be negative"
"SIL.Scripture.VerseRefException: VerseNum can not be negative"
)]
public async Task GetFunctions_InvalidParameters(
string function,
Expand Down Expand Up @@ -130,7 +130,7 @@ string expectedError
"***", // bookNum: 0
1,
0,
"Invalid VerseRef ({\"book\":\"***\",\"chapterNum\":1,\"verseNum\":0,\"versificationStr\":\"English\"}): BookNum must be greater than zero and less than or equal to last book"
"SIL.Scripture.VerseRefException: BookNum must be greater than zero and less than or equal to last book"
)]
public async Task GetFunctions_FullSerialization_InvalidParameters(
string function,
Expand Down Expand Up @@ -165,7 +165,7 @@ string expectedError
"***", // bookNum: 0
1,
0,
"Invalid VerseRef ({\"book\":\"***\",\"chapterNum\":1,\"verse\":null,\"verseNum\":0,\"versificationStr\":\"English\"}): BookNum must be greater than zero and less than or equal to last book"
"SIL.Scripture.VerseRefException: BookNum must be greater than zero and less than or equal to last book"
)]
public async Task GetFunctions_FullSerializationv2_0_0_InvalidParameters(
string function,
Expand Down Expand Up @@ -674,8 +674,8 @@ public async Task SetProjectSetting_ValidVisibility_Succeeds()
await provider.RegisterDataProvider();

var result = provider.SetProjectSetting(
JsonConvert.SerializeObject(VisibilitySettingName),
JsonConvert.SerializeObject(ProjectVisibility.Public.ToString())
VisibilitySettingName.SerializeToJson(),
ProjectVisibility.Public.ToString().SerializeToJson()
);

Assert.That(result.Success, Is.True);
Expand All @@ -694,8 +694,8 @@ public async Task SetProjectSetting_InvalidVisibility_DoesNotSucceed()
await provider.RegisterDataProvider();

var result = provider.SetProjectSetting(
JsonConvert.SerializeObject(VisibilitySettingName),
JsonConvert.SerializeObject(89)
VisibilitySettingName.SerializeToJson(),
89.SerializeToJson()
);

Assert.That(result.Success, Is.False);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal void CreateTempProject(string folder, ProjectDetails projectDetails)
var settings = new MinimalParatextProjectSettings
{
Name = projectDetails.Name,
Guid = projectDetails.Metadata.ID,
Guid = projectDetails.Metadata.Id,
// Baked-in functional language code. Just needed something that worked for ScrText
// to load. Feel free to change this for testing purposes
LanguageIsoCode = "en:::",
Expand Down
Loading

0 comments on commit a8e19f8

Please sign in to comment.