| | | 1 | | using Newtonsoft.Json.Linq; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | | 8 | | using ValidateLib.Metadata.Descriptors; |
| | | 9 | | using ValidateLib.Metadata.Properties; |
| | | 10 | | |
| | | 11 | | namespace NormalizationTests |
| | | 12 | | { |
| | | 13 | | public class NaturalLanguagePropertiesTests |
| | | 14 | | { |
| | | 15 | | public readonly string testFilesDirectory; |
| | 1 | 16 | | public NaturalLanguagePropertiesTests() |
| | 1 | 17 | | { |
| | 1 | 18 | | testFilesDirectory = Path.Combine(GetProjectDirectory(), "TestFiles", "NaturalLanguagePropertiesTests"); |
| | 1 | 19 | | } |
| | | 20 | | static string GetProjectDirectory() |
| | 1 | 21 | | { |
| | 1 | 22 | | string? currentDirectory = Directory.GetCurrentDirectory(); |
| | | 23 | | |
| | 1 | 24 | | while (!string.IsNullOrEmpty(currentDirectory)) |
| | 1 | 25 | | { |
| | 1 | 26 | | string[] projectFiles = Directory.GetFiles(currentDirectory, "*.csproj"); |
| | | 27 | | |
| | 1 | 28 | | if (projectFiles.Length > 0) |
| | 1 | 29 | | return currentDirectory; |
| | 1 | 30 | | currentDirectory = Directory.GetParent(currentDirectory)?.FullName; |
| | 1 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | throw new Exception("Could not find project directory."); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | |
| | | 37 | | [Fact] |
| | | 38 | | public void NaturalLanguagePropertyStringToObjectEnLanguageTest() |
| | 1 | 39 | | { |
| | 1 | 40 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile1.json"); |
| | 1 | 41 | | string jsonString = File.ReadAllText(testFilePath); |
| | 1 | 42 | | JObject jsonObject = JObject.Parse(jsonString); |
| | | 43 | | |
| | 1 | 44 | | var warnings = new List<Warning>(); |
| | 1 | 45 | | Context context = Context.GetContextFromJToken(jsonObject,warnings, testFilePath); |
| | | 46 | | |
| | | 47 | | |
| | 1 | 48 | | var property = ((JObject)jsonObject!["tableSchema"]!["columns"]![0]!)!.Property("titles")!; |
| | 1 | 49 | | NaturalLanguageProperty.Normalize(property?.Value!, context, property); |
| | | 50 | | |
| | 1 | 51 | | var titlesObject = property!.Value; |
| | | 52 | | |
| | 1 | 53 | | Assert.Equal(typeof(JObject), titlesObject.GetType()); |
| | 1 | 54 | | Assert.Equal("GID", titlesObject!["en"]![0]!.ToString()); |
| | | 55 | | |
| | | 56 | | |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | [Fact] |
| | | 60 | | public void NaturalLanguagePropertyStringToObjectUndLanguageTest() |
| | 1 | 61 | | { |
| | 1 | 62 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile2.json"); |
| | 1 | 63 | | string jsonString = File.ReadAllText(testFilePath); |
| | 1 | 64 | | JObject jsonObject = JObject.Parse(jsonString); |
| | | 65 | | |
| | 1 | 66 | | var warnings = new List<Warning>(); |
| | 1 | 67 | | Context context = Context.GetContextFromJToken(jsonObject, warnings, testFilePath); |
| | | 68 | | |
| | | 69 | | |
| | 1 | 70 | | var property = ((JObject)jsonObject!["tableSchema"]!["columns"]![0]!)!.Property("titles"); |
| | 1 | 71 | | NaturalLanguageProperty.Normalize(property?.Value!, context, property); |
| | | 72 | | |
| | 1 | 73 | | var titlesObject = property!.Value; |
| | | 74 | | |
| | 1 | 75 | | Assert.Equal(typeof(JObject), titlesObject.GetType()); |
| | 1 | 76 | | Assert.Equal("GID", titlesObject!["und"]![0]!.ToString()); |
| | | 77 | | |
| | | 78 | | |
| | 1 | 79 | | } |
| | | 80 | | |
| | | 81 | | [Fact] |
| | | 82 | | public void NaturalLanguagePropertyMultipleStringToObjectUndLanguageTest() |
| | 1 | 83 | | { |
| | 1 | 84 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile3.json"); |
| | 1 | 85 | | string jsonString = File.ReadAllText(testFilePath); |
| | 1 | 86 | | JObject jsonObject = JObject.Parse(jsonString); |
| | | 87 | | |
| | 1 | 88 | | var warnings = new List<Warning>(); |
| | 1 | 89 | | Context context = Context.GetContextFromJToken(jsonObject, warnings, testFilePath); |
| | | 90 | | |
| | | 91 | | |
| | 1 | 92 | | var property = ((JObject)jsonObject!["tableSchema"]!["columns"]![0]!)!.Property("titles"); |
| | 1 | 93 | | NaturalLanguageProperty.Normalize(property?.Value!, context, property); |
| | | 94 | | |
| | 1 | 95 | | var titlesObject = property!.Value; |
| | | 96 | | |
| | 1 | 97 | | Assert.Equal(typeof(JObject), titlesObject.GetType()); |
| | 1 | 98 | | Assert.Equal("GID", titlesObject!["und"]![0]!.ToString()); |
| | 1 | 99 | | Assert.Equal("PID", titlesObject!["und"]![1]!.ToString()); |
| | 1 | 100 | | Assert.Equal("XD", titlesObject!["und"]![2]!.ToString()); |
| | | 101 | | |
| | | 102 | | |
| | 1 | 103 | | } |
| | | 104 | | |
| | | 105 | | [Fact] |
| | | 106 | | public void NaturalLanguageArrayTitlesEnLanguageTest() |
| | 1 | 107 | | { |
| | 1 | 108 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile4.json"); |
| | 1 | 109 | | string jsonString = File.ReadAllText(testFilePath); |
| | 1 | 110 | | JObject jsonObject = JObject.Parse(jsonString); |
| | | 111 | | |
| | 1 | 112 | | var warnings = new List<Warning>(); |
| | 1 | 113 | | Context context = Context.GetContextFromJToken(jsonObject, warnings, testFilePath); |
| | | 114 | | |
| | | 115 | | |
| | 1 | 116 | | var property = ((JObject)jsonObject!["tableSchema"]!["columns"]![0]!)!.Property("titles"); |
| | 1 | 117 | | NaturalLanguageProperty.Normalize(property?.Value!, context, property); |
| | | 118 | | |
| | 1 | 119 | | var titlesObject = property!.Value; |
| | | 120 | | |
| | 1 | 121 | | Assert.Equal(typeof(JObject), titlesObject.GetType()); |
| | | 122 | | |
| | | 123 | | |
| | 1 | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |