< Summary

Information
Class: ValidateLib.TabularData.Validation.TabluarDataTableGroupValidator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Validation\TabluarDataTableGroupValidator.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 144
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Validation\TabluarDataTableGroupValidator.cs

#LineLine coverage
 1using ValidateLib.Metadata.Descriptors;
 2using ValidateLib.Metadata.DialectExtraction;
 3using ValidateLib.Metadata.Validators;
 4using ValidateLib.Results;
 5using ValidateLib.TabularData.AnnotatedTabularDataModel;
 6using ValidateLib.TabularData.Parsing;
 7using ValidateLib.TabularData.Validation.ValidationRules;
 8
 9namespace ValidateLib.TabularData.Validation
 10{
 11    /// <summary>
 12    /// Validates table groups.
 13    /// Uses <see cref="TabularDataTableValidator"/>.
 14    /// </summary>
 15    public class TabluarDataTableGroupValidator
 16    {
 117        private TableGroupDescriptor tableGroupDescriptor { get; set; }
 118        protected List<ICellValidationRule>? cellValidationRules { get; set; } = new List<ICellValidationRule>();
 119        protected List<IRowValidationRule>? rowValidationRules { get; set; } = new List<IRowValidationRule>();
 120        public TabluarDataTableGroupValidator(TableGroupDescriptor tableGroup, List<ICellValidationRule>? cellRules = nu
 21        {
 122            tableGroupDescriptor = tableGroup;
 123            rowValidationRules = rowValidationRules;
 124            cellValidationRules = cellRules;
 125        }
 26        /// <summary>
 27        /// Validates table group and adds errors and warnings to <paramref name="validationDetails"/>.
 28        /// </summary>
 29        /// <param name="validationDetails"> where we insert the errors and warnings.</param>
 30        public void ValidateTableGroup(ITableGroupValidationDetail validationDetails)
 31        {
 132            List<Table> tables = PrepareAnnotatedTables();
 33
 134            if (tableGroupDescriptor.tables is not null && tableGroupDescriptor.tables._value is not null)
 35            {
 136                for (int i = 0; i < tableGroupDescriptor.tables!._value!.Count; i++)
 37                {
 138                    var validationDetail = validationDetails.TableValidationDetails[i];
 139                    if (validationDetail.Errors.Count > 0) continue;
 140                    var tableDescriptor = tableGroupDescriptor.tables!._value![i];
 141                    TabularDataTableValidator tableValidator = new TabularDataTableValidator(tableDescriptor, cellValida
 142                    tableValidator.ValidateTable(validationDetail, tableDescriptor.table!);
 43                }
 44            }
 45
 146            var FKRule = new FKReferencedValuesMustMatchValidationRule();
 147            validationDetails.GeneralErrors.AddRange(FKRule.ValidateReferencedValuesFK(tables, tableGroupDescriptor));
 148        }
 49
 50        /// <summary>
 51        /// Prepare annotated tables for the table group validation. This steep needs to be done in advance
 52        /// because we do not know exactly what columns to save during the CSV parsing during the parsing of
 53        /// just one CSV file.
 54        /// </summary>
 55        /// <returns></returns>
 56        public List<Table> PrepareAnnotatedTables()
 57        {
 158            List<Table> tables = new List<Table>();
 159            if (tableGroupDescriptor.tables is not null && tableGroupDescriptor.tables._value is not null)
 60            {
 161                foreach (var tableDescriptor in tableGroupDescriptor.tables._value)
 62                {
 163                    CreateAnnotatedTable(tableDescriptor, tables);
 64                }
 165                SetTheReferencedColumnsToBePartOfFK();
 66            }
 167            return tables;
 68        }
 69
 70        /// <summary>
 71        /// Creates one annotated table. At this point the FK flags of referenced foreign keys are not
 72        /// set completely.
 73        /// </summary>
 74        /// <param name="tableDescriptor"></param>
 75        /// <param name="tables"></param>
 76        private void CreateAnnotatedTable(TableDescriptor tableDescriptor, List<Table> tables)
 77        {
 178            var dialect = DialectExtractor.GetDialect(tableDescriptor);
 179            var flags = FlagsCreator.ExtractFlagsFromDialectDescriptor(dialect);
 180            TabularDataAnnotator tabularDataAnottator = new TabularDataAnnotator(flags);
 181            Table table = new Table();
 182            tabularDataAnottator.AnnotateTable(table, tableDescriptor);
 183            tabularDataAnottator.CreateAnnotatedColumns(table, tableDescriptor);
 184            tableDescriptor.table = table;
 185            tables.Add(table);
 186        }
 87
 88        /// <summary>
 89        /// Iterates over tables and for each one of them checks for the FK descriptor and tries
 90        /// to set the referenced columns to be a part of the foreignKey so they would be
 91        /// saved during the CSV parsing.
 92        /// </summary>
 93        private void SetTheReferencedColumnsToBePartOfFK()
 94        {
 95
 196            foreach (var tableDescriptor in tableGroupDescriptor!.tables!._value!)
 97            {
 198                SetReFerencedColumnsForOneTable(tableDescriptor);
 99            }
 100
 1101        }
 102
 103        /// <summary>
 104        /// Iterates over one's table foreign keys (if they have some) and then sets all the
 105        /// referenced columns from other tables to be a part of foreign (referenced) key.
 106        /// </summary>
 107        /// <param name="tableDescriptor"></param>
 108        private void SetReFerencedColumnsForOneTable(TableDescriptor tableDescriptor)
 109        {
 1110            if (HasTableFK(tableDescriptor))
 111            {
 1112                var foreignKeys = tableDescriptor.tableSchema!._value!.foreignKeys!._value!;
 1113                foreach (var FKDescriptor in foreignKeys)
 114                {
 1115                    TableDescriptor referencedTableDescriptor = ForeignKeyValidator.FindReferencedTable(tableGroupDescri
 1116                    Table referencedTable = referencedTableDescriptor.table!;
 1117                    foreach (var column in referencedTable.columns)
 118                    {
 1119                        if (column.name is null)
 120                            continue;
 1121                        if (FKDescriptor!.reference!._value!.columnReference!._value!.Contains(column.name))
 1122                            column.isPartOfForeignKey = true;
 123                    }
 124                }
 125
 126            }
 1127        }
 128
 129        /// <summary>
 130        /// Checks whether a tableDescriptor has some foreign key descriptor.
 131        /// </summary>
 132        /// <param name="tableDescriptor"></param>
 133        /// <returns></returns>
 134        private bool HasTableFK(TableDescriptor tableDescriptor)
 135        {
 1136            return tableDescriptor.tableSchema is not null &&
 1137                   tableDescriptor.tableSchema._value is not null &&
 1138                   tableDescriptor.tableSchema._value.foreignKeys is not null &&
 1139                   tableDescriptor.tableSchema._value.foreignKeys._value is not null;
 140        }
 141    }
 142
 143
 144}