-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
455 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Newtonsoft.Json; | ||
using TestParanextDataProvider; | ||
|
||
namespace Paranext.DataProvider.Projects.Tests | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class ProjectSettingsServicesTests | ||
{ | ||
[Test] | ||
public void IsValid_ValidLanguage_ReturnsTrue() | ||
{ | ||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
var newValueJson = JsonConvert.SerializeObject("Spanish"); | ||
var currentValueJson = JsonConvert.SerializeObject("German"); | ||
papiClient.AddSettingValueToTreatAsValid(ProjectSettings.PB_LANGUAGE, | ||
newValueJson, currentValueJson); | ||
var result = ProjectSettingsService.IsValid(papiClient, newValueJson, | ||
currentValueJson, ProjectSettings.PT_LANGUAGE, "", | ||
ProjectType.Paratext); | ||
|
||
Assert.That(result, Is.True); | ||
} | ||
|
||
[Test] | ||
public void IsValid_InvalidSetting_ReturnsFalse() | ||
{ | ||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
var newValueJson = JsonConvert.SerializeObject("Spanish"); | ||
var currentValueJson = JsonConvert.SerializeObject("German"); | ||
var result = ProjectSettingsService.IsValid(papiClient, newValueJson, | ||
currentValueJson, ProjectSettings.PT_LANGUAGE, "", | ||
ProjectType.Paratext); | ||
|
||
Assert.That(result, Is.False); | ||
} | ||
|
||
[Test] | ||
public void IsValid_UnexpectedProjectType_ReturnsFalse() | ||
{ | ||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
var newValueJson = JsonConvert.SerializeObject("Spanish"); | ||
var currentValueJson = JsonConvert.SerializeObject("German"); | ||
papiClient.AddSettingValueToTreatAsValid(ProjectSettings.PB_LANGUAGE, | ||
newValueJson, currentValueJson); | ||
var result = ProjectSettingsService.IsValid(papiClient, newValueJson, | ||
currentValueJson, ProjectSettings.PT_LANGUAGE, "", | ||
"SomethingElse"); | ||
|
||
Assert.That(result, Is.False); | ||
} | ||
|
||
[Test] | ||
public void GetDefault_KnownProperty_ReturnsDefaultValue() | ||
{ | ||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
var result = ProjectSettingsService.GetDefault(papiClient, ProjectSettings.PT_LANGUAGE, | ||
ProjectType.Paratext); | ||
|
||
Assert.That(result, Is.EqualTo($"default value for {ProjectSettings.PB_LANGUAGE}")); | ||
} | ||
|
||
[Test] | ||
public void GetDefault_UnknownProperty_ReturnsNull() | ||
{ | ||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
var result = ProjectSettingsService.GetDefault(papiClient, "wonky.setting", | ||
ProjectType.Paratext); | ||
|
||
Assert.That(result, Is.Null); | ||
} | ||
|
||
[Test] | ||
public void RegisterValidator_NewProperty_ReturnsTrue() | ||
{ | ||
const string settingKey = "testScripture.MonkeyCount"; | ||
(bool result, string? error) MonkeyCountValidator((string newValueJson, string currentValueJson, | ||
string allChangesJson, string projectType) data) | ||
{ | ||
try | ||
{ | ||
var value = JsonConvert.DeserializeObject(data.newValueJson); | ||
var result = true; | ||
string? error = null; | ||
if (value == null) | ||
{ | ||
result = false; | ||
error = "New value must be an integer. It cannot be null"; | ||
} | ||
else if ((long)value <= 4) | ||
{ | ||
result = false; | ||
error = "Mama called the doctor, and the doctor said, you must have at" + | ||
"least 4 monkeys jumping on the bed!"; | ||
} | ||
return (result, error); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return (false, ex.Message); | ||
} | ||
} | ||
|
||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
|
||
// Before registering the validator, IsValid should return true. | ||
Assert.That(ProjectSettingsService.IsValid(papiClient, JsonConvert.SerializeObject(2), | ||
JsonConvert.SerializeObject(5), settingKey, "", ProjectType.Paratext), Is.True); | ||
|
||
var result = ProjectSettingsService.RegisterValidator(papiClient, | ||
settingKey, MonkeyCountValidator); | ||
|
||
Assert.That(result, Is.EqualTo(true)); | ||
|
||
Assert.That(ProjectSettingsService.IsValid(papiClient, JsonConvert.SerializeObject(1), | ||
JsonConvert.SerializeObject(5), settingKey, "", ProjectType.Paratext), Is.False); | ||
Assert.That(ProjectSettingsService.IsValid(papiClient, JsonConvert.SerializeObject(6), | ||
JsonConvert.SerializeObject(5), settingKey, "", ProjectType.Paratext), Is.True); | ||
} | ||
|
||
[Test] | ||
public void RegisterValidator_ExistingProperty_ReturnsFalse() | ||
{ | ||
const string settingKey = "testScripture.Oops"; | ||
(bool result, string? error) OopsValidator((string newValueJson, string currentValueJson, | ||
string allChangesJson, string projectType) data) | ||
{ | ||
return (false, "The Oops property has no valid values. Ha Ha!"); | ||
} | ||
|
||
DummyPapiClient papiClient = new DummyPapiClient(); | ||
|
||
var result = ProjectSettingsService.RegisterValidator(papiClient, | ||
settingKey, OopsValidator); | ||
|
||
Assert.That(result, Is.EqualTo(true)); | ||
|
||
Assert.That(ProjectSettingsService.RegisterValidator(papiClient, | ||
settingKey, OopsValidator), Is.EqualTo(false)); | ||
} | ||
} | ||
} |
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.