forked from david-allison/manx-search-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentTests.cs
140 lines (118 loc) · 6.06 KB
/
DocumentTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Manx_Search_Data.TestData;
using Manx_Search_Data.TestUtil;
using NUnit.Framework;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Manx_Search_Data
{
[TestFixture]
public class DocumentTests
{
[DatapointSource]
public Document[] allDocuments = Documents.AllDocuments.ToArray();
[Theory]
public void DefinitionHasName(Document definition)
{
Assert.That(definition.Name, Is.Not.Null, $"{nameof(Document.Name)} shoud be defined");
Assert.That(definition.Name, Is.Not.Empty, $"{nameof(Document.Name)} shoud be non-empty");
}
[Theory]
public void DefinitionHasIdent(Document definition)
{
Assert.That(definition.Ident, Is.Not.Null, $"{nameof(Document.Ident)} shoud be defined. This is the how the document is deined in a web address");
Assert.That(definition.Ident, Is.Not.Empty, $"{nameof(Document.Ident)} shoud be non-empty. This is the how the document is deined in a web address");
Assert.That(definition.Ident, Does.Match("^[A-Za-z0-9\\-]+$"), $"{nameof(Document.Ident)} must only contain letters numbers, or dashes with no spaces.\nThis field is the how the document is defined in a web address");
}
[Theory]
public void DefinitionHasDate(Document definition)
{
Assert.That(definition.CreatedCircaStart, Is.Not.Null, $"Either '{nameof(Document.Created)}' or '{nameof(Document.CreatedCircaStart)}' must be set");
Assert.That(definition.CreatedCircaEnd, Is.Not.Null, $"Either '{nameof(Document.Created)}' or '{nameof(Document.CreatedCircaEnd)}' must be set");
}
[Theory]
public void DefinitionDatesAreValid(Document definition)
{
Assert.That(definition.CreatedCircaStart, Is.LessThanOrEqualTo(definition.CreatedCircaEnd), $"'{nameof(Document.CreatedCircaStart)}' was greater than '{nameof(Document.CreatedCircaEnd)}'");
}
[Theory]
public void DefinitionsHasLinkedCsv(Document definition)
{
Assert.That(definition.CsvFileName, Is.Not.Null, $"'{nameof(Document.CsvFileName)}' must be set");
}
[Theory]
public void PdfLinkIsValidIfDefined(Document definition)
{
var document = AssumeOpenSource(definition, "PDF is not available yet");
Assert.That(document.PdfFileName, Is.Null.Or.Not.Empty, $"'{nameof(Document.PdfFileName)}' must not have an empty value (either delete the line, or set a value)");
if (document.PdfFileName == null)
{
return;
}
Assert.That(File.Exists(document.FullPdfPath), $"'{document.FullPdfPath}' does not exist");
}
[Theory]
public void CsvFileIsReadable(Document definition)
{
AssumeOpenSource(definition, "CSV is not avalable yet");
try
{
var lines = definition.LoadLocalFile();
Assert.That(lines, Is.Not.Empty, "No data detected in CSV");
}
catch (Exception e)
{
Assert.Fail($"{definition} has an invalid CSV: {e}");
}
}
[Theory]
public void CsvFileIsUTF8(Document document)
{
var openSourceDocument = AssumeOpenSource(document, "CSV is not avalable yet");
var lines = document.LoadLocalFile();
// We perform this per-line as we don't want the entire text in the unit test output.
foreach (string line in lines.Select(x => x.English + "|" + x.Manx))
{
// If we don't use Ordinal, then CI fails at index 0 (even though the value is ASCII 65 (A)).
int index = line.IndexOf("�", StringComparison.Ordinal);
if (index == -1)
{
continue;
}
Assert.That(line, Does.Not.Contain("�"), $"The CSV is saved incorrectly. Please open it in Notepad++ and select \"Encoding - Convert to UTF-8\"\nFile: {openSourceDocument.FullCsvPath}. Index: {index}");
}
}
[Theory]
public void CsvFileIsNotChinese(Document document)
{
// PERF: this is a slow test - combine with CsvFileIsUTF8
// Issue: 463 - some files were interpreted to be Chinese, the only way to fix this was to re-save in Excel as UTF-8
var openSourceDocument = AssumeOpenSource(document, "CSV is not avalable yet");
var lines = document.LoadLocalFile();
const string ChineseOrJapanese = "[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]";
foreach (string line in lines.Select(x => x.English + "|" + x.Manx))
{
Assert.That(line, Does.Not.Match(ChineseOrJapanese), $"The CSV may be saved incorrectly (containing Chinese/Japanese Text). Please remake it and save it as CSV (UTF-8) in Excel. Contact David if this fails\"\nFile: {openSourceDocument.FullCsvPath}.");
}
}
[Theory]
public void LicenseExists(Document definition)
{
var openSourceDocument = AssumeOpenSource(definition, "license is not avalable yet");
Assert.That(File.Exists(openSourceDocument.LicenseLink), $"{nameof(OpenSourceDocument.LicenseLink)} does not exist");
}
/// <summary>We currently have files which are not yet licensed for usage on GitHub, some checks cannot be run on these yet</summary>
private static OpenSourceDocument AssumeOpenSource(Document definition, string reasonAsClosedSource)
{
if (ClosedSourceDocuments.documents.Contains(definition))
{
// We don't use "Assume" here as we want clean test output
// TODO: Figure out getting dotnet test to list clean output
Assert.Pass($"Skipping - document was closed source - {reasonAsClosedSource}");
}
return (OpenSourceDocument)definition;
}
}
}