-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsValidDataset.cs
150 lines (126 loc) · 7.32 KB
/
IsValidDataset.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
141
142
143
144
145
146
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BH.oM.Test.CodeCompliance;
using System.IO;
using BH.oM.Data.Library;
using BH.oM.Test;
using BH.oM.Test.Results;
namespace BH.Engine.Test.CodeCompliance.DynamicChecks
{
public static partial class Query
{
public static TestResult IsValidDataset(this string filePath)
{
if (!File.Exists(filePath))
return Create.TestResult(TestStatus.Pass);
string documentationLink = "IsValidDataset";
//Read the dataset
StreamReader sr = new StreamReader(filePath);
string json = sr.ReadToEnd();
sr.Close();
if (json == null)
return Create.TestResult(TestStatus.Pass);
//Check if the dataset deserialises to a dataset object
Dataset ds = BH.Engine.Serialiser.Convert.FromJson(json) as Dataset;
if(ds == null)
{
//Dataset did not deserialise successfully
return Create.TestResult(TestStatus.Error,
new List<Error>() { Create.Error($"Dataset file did not deserialise into a BH.oM.Data.Library.Dataset object successfully. For more information see {Base.Query.DocumentationURL("DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsValidDataset")}",
Create.Location(filePath, Create.LineSpan(1, 1)),
documentationLink,
TestStatus.Error,
"Dataset deserialisation error"
) });
}
if(ds.SourceInformation == null)
{
//Source information is not set
return Create.TestResult(TestStatus.Warning,
new List<Error>() { Create.Error($"Dataset file does not contain any source information. For more information see {Base.Query.DocumentationURL("DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsValidDataset")}",
Create.Location(filePath, Create.LineSpan(1, 1)),
documentationLink,
TestStatus.Warning,
"Dataset source error"
) });
}
DatasetSource dss = new DatasetSource();
dss.Message = "Title: " + ds.SourceInformation.Title + System.Environment.NewLine +
"Author: " + ds.SourceInformation.Author + System.Environment.NewLine +
"Confidence: " + ds.SourceInformation.Confidence.ToString() + System.Environment.NewLine +
"Version: " + ds.SourceInformation.Version + System.Environment.NewLine +
"Source Link: " + ds.SourceInformation.SourceLink + System.Environment.NewLine +
"Publisher: " + ds.SourceInformation.Publisher + System.Environment.NewLine +
"Language: " + ds.SourceInformation.Language + System.Environment.NewLine +
"Schema: " + ds.SourceInformation.Schema + System.Environment.NewLine +
"Location: " + ds.SourceInformation.Location + System.Environment.NewLine +
"Copyright: " + ds.SourceInformation.Copyright + System.Environment.NewLine +
"Contributors: " + ds.SourceInformation.Contributors + System.Environment.NewLine +
"Item Reference: " + ds.SourceInformation.ItemReference + System.Environment.NewLine;
dss.Status = TestStatus.Pass;
TestResult result = new TestResult() { Status = TestStatus.Pass };
result.Information.Add(dss);
List<Error> errors = new List<Error>();
if (ds.SourceInformation.Author == null || ds.SourceInformation.Author == "")
{
//Source information does not contain an author
errors.Add(Create.Error($"Dataset file does not contain an author in the source information. For more information see {Base.Query.DocumentationURL("DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsValidDataset")}",
Create.Location(filePath, Create.LineSpan(1, 1)),
documentationLink,
TestStatus.Warning,
"Dataset source author error"
));
result.Status = TestStatus.Warning;
}
if (ds.SourceInformation.Title == null || ds.SourceInformation.Title == "")
{
//Source information does not contain an author
errors.Add(Create.Error($"Dataset file does not contain a title in the source information. For more information see {Base.Query.DocumentationURL("DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsValidDataset")}",
Create.Location(filePath, Create.LineSpan(1, 1)),
documentationLink,
TestStatus.Warning,
"Dataset source title error"
));
result.Status = TestStatus.Warning;
}
if (ds.SourceInformation.Confidence == Confidence.Undefined)
{
//Source confidence has not been set
errors.Add(Create.Error($"Dataset confidence level has not been correctly set. For more information see {Base.Query.DocumentationURL("DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsValidDataset")}",
Create.Location(filePath, Create.LineSpan(1, 1)),
documentationLink,
TestStatus.Error,
"Dataset confidence error"
));
result.Status = TestStatus.Error;
}
if (errors.Count > 0)
result.Information.AddRange(errors.Select(x => x as ITestInformation));
return result;
}
}
}