Esempio n. 1
0
        private static void CanDetectNtoMUnique([NotNull] IFeatureWorkspace workspace)
        {
            const string uniqueFieldName     = "FIELD_UNIQUE";
            const string foreignKeyFieldName = "FOREIGN_KEY_FIELD";

            ITable tableOrig;
            {
                IFieldsEdit fields = new FieldsClass();
                fields.AddField(FieldUtils.CreateOIDField());
                fields.AddField(FieldUtils.CreateField(uniqueFieldName,
                                                       esriFieldType.esriFieldTypeInteger));
                ITable table = TestWorkspaceUtils.CreateSimpleTable(workspace, "RelateUnique1",
                                                                    fields,
                                                                    null);
                tableOrig = table;
            }
            ITable tableRel;
            {
                IFieldsEdit fields = new FieldsClass();
                fields.AddField(FieldUtils.CreateOIDField());
                fields.AddField(FieldUtils.CreateField(foreignKeyFieldName,
                                                       esriFieldType.esriFieldTypeInteger));
                ITable table = TestWorkspaceUtils.CreateSimpleTable(workspace, "RelateUnique2",
                                                                    fields,
                                                                    null);
                tableRel = table;
            }
            IRelationshipClass rel = TestWorkspaceUtils.CreateSimpleMNRelationship(
                workspace, "NToMRelTable", tableOrig, tableRel, uniqueFieldName,
                foreignKeyFieldName);

            {
                ((IWorkspaceEdit)workspace).StartEditing(false);
                for (var i = 0; i < 10; i++)
                {
                    IRow row = tableOrig.CreateRow();
                    row.set_Value(1, i);
                    row.Store();
                }

                for (var i = 0; i < 10; i++)
                {
                    IRow row = tableRel.CreateRow();
                    row.set_Value(1, i);
                    row.Store();
                    rel.CreateRelationship((IObject)tableOrig.GetRow(i + 1),
                                           (IObject)row);
                }

                ((IWorkspaceEdit)workspace).StopEditing(true);
            }

            ITable relTab = TableJoinUtils.CreateQueryTable(rel, JoinType.InnerJoin);

            foreach (bool forceInMemoryTableSort in new[] { true, false })
            {
                var test = new QaUnique(relTab, "RelateUnique1." + uniqueFieldName)
                {
                    ForceInMemoryTableSorting = forceInMemoryTableSort
                };

                var runner = new QaTestRunner(test);
                runner.Execute();
                AssertUtils.NoError(runner);
            }
        }
Esempio n. 2
0
        private static void CanDetectNtoMNonUnique([NotNull] IFeatureWorkspace ws)
        {
            const string uniqueFieldName     = "FIELD_UNIQUE";
            const string foreignKeyFieldName = "FOREIGN_KEY_FIELD";
            const string origTableName       = "OrigTable2";
            const string destTableName       = "DestTable2";
            ITable       originTable;
            {
                IFieldsEdit fields = new FieldsClass();
                fields.AddField(FieldUtils.CreateOIDField());
                fields.AddField(FieldUtils.CreateField(uniqueFieldName,
                                                       esriFieldType.esriFieldTypeInteger));
                ITable table = TestWorkspaceUtils.CreateSimpleTable(
                    ws, origTableName, fields);
                originTable = table;
            }
            ITable destinationTable;
            {
                IFieldsEdit fields = new FieldsClass();
                fields.AddField(FieldUtils.CreateOIDField());
                fields.AddField(FieldUtils.CreateField(
                                    foreignKeyFieldName, esriFieldType.esriFieldTypeInteger));
                ITable table = TestWorkspaceUtils.CreateSimpleTable(
                    ws, destTableName, fields);

                destinationTable = table;
            }

            IRelationshipClass relClass = TestWorkspaceUtils.CreateSimpleMNRelationship(
                ws, "relNonUnique", originTable, destinationTable,
                "OrigFKey", "DestFKey");

            {
                // insert Data
                ((IWorkspaceEdit)ws).StartEditing(false);
                for (var i = 0; i < 10; i++)
                {
                    IRow row = originTable.CreateRow();
                    row.set_Value(1, i);
                    row.Store();
                }

                for (var i = 0; i < 10; i++)
                {
                    IRow row = destinationTable.CreateRow();
                    row.set_Value(1, i);
                    row.Store();

                    relClass.CreateRelationship((IObject)originTable.GetRow(i + 1),
                                                (IObject)row);
                }

                {
                    IRow row = destinationTable.CreateRow();
                    row.set_Value(1, 5);
                    row.Store();
                    relClass.CreateRelationship((IObject)originTable.GetRow(5 + 1),
                                                (IObject)row);
                }
                {
                    IRow row = destinationTable.CreateRow();
                    row.set_Value(1, 5);
                    row.Store();
                    relClass.CreateRelationship((IObject)originTable.GetRow(5 + 1),
                                                (IObject)row);
                }
                {
                    IRow row = destinationTable.CreateRow();
                    row.set_Value(1, 7);
                    row.Store();
                    relClass.CreateRelationship((IObject)originTable.GetRow(7 + 1),
                                                (IObject)row);
                }
                ((IWorkspaceEdit)ws).StopEditing(true);
            }

            ITable relTab = TableJoinUtils.CreateQueryTable(relClass, JoinType.InnerJoin);

            foreach (bool forceInMemoryTableSort in new[] { true, false })
            {
                var test = new QaUnique(relTab, origTableName + "." + uniqueFieldName)
                {
                    ForceInMemoryTableSorting = forceInMemoryTableSort
                };

                test.SetRelatedTables(new[] { originTable, destinationTable });

                var runner = new QaTestRunner(test);
                runner.Execute();
                Assert.AreEqual(5, runner.Errors.Count);
            }
        }
        public void TestRelatedFactory()
        {
            IFeatureWorkspace initTestWs = _testWs;

            try
            {
                _testWs = RelTestWs;

                IFeatureClass fc1 = CreateFeatureClass("TestRelatedFactory",
                                                       esriGeometryType.esriGeometryPolyline);

                var ds1 = (IDataset)fc1;

                IFieldsEdit fields = new FieldsClass();
                fields.AddField(FieldUtils.CreateOIDField());
                fields.AddField(FieldUtils.CreateIntegerField(_fkFieldName));
                fields.AddField(FieldUtils.CreateTextField(_textFieldName, 200));

                string tableName = ds1.Name + "_table";
                ITable table     = TestWorkspaceUtils.CreateSimpleTable(_testWs, tableName, fields);

                var tableDataset = (IDataset)table;

                string             relClassName = "relClass" + Math.Abs(Environment.TickCount);
                IRelationshipClass rel          = TestWorkspaceUtils.CreateSimpleMNRelationship(
                    _testWs, relClassName, table, (ITable)fc1, "fkGrp", "fkFc");

                ((IWorkspaceEdit)_testWs).StartEditing(false);

                IFeature f = AddFeature(fc1,
                                        CurveConstruction.StartLine(0, 0)
                                        .LineTo(4, 0)
                                        .Curve);
                IRow r = AddRow(table, textFieldValue: "A");
                rel.CreateRelationship((IObject)r, f);
                r = AddRow(table, textFieldValue: "B");
                rel.CreateRelationship((IObject)r, f);

                f = AddFeature(fc1,
                               CurveConstruction.StartLine(4, 0)
                               .LineTo(4, 8)
                               .Curve);
                r = AddRow(table, textFieldValue: "A");
                rel.CreateRelationship((IObject)r, f);

                ((IWorkspaceEdit)_testWs).StopEditing(true);

                var     model  = new SimpleModel("model", fc1);
                Dataset mds1   = model.AddDataset(new ModelVectorDataset(ds1.Name));
                Dataset mdsRel = model.AddDataset(new ModelTableDataset(tableDataset.Name));

                var clsDesc   = new ClassDescriptor(typeof(QaRelRegularExpression));
                var tstDesc   = new TestDescriptor("GroupEnds", clsDesc);
                var condition = new QualityCondition("cndGroupEnds", tstDesc);
                QualityConditionParameterUtils.AddParameterValue(condition, "relationTables", mds1);
                QualityConditionParameterUtils.AddParameterValue(condition, "relationTables", mdsRel);
                QualityConditionParameterUtils.AddParameterValue(condition, "relation", relClassName);
                QualityConditionParameterUtils.AddParameterValue(condition, "join", JoinType.InnerJoin);
                QualityConditionParameterUtils.AddParameterValue(condition, "pattern", "A");
                QualityConditionParameterUtils.AddParameterValue(condition, "fieldNames",
                                                                 $"{tableName}.{_textFieldName}");
                QualityConditionParameterUtils.AddParameterValue(condition, "MatchIsError", false);
                //condition.AddParameterValue("PatternDescription", "Hallo");

                var factory = new QaRelRegularExpression {
                    Condition = condition
                };

                IList <ITest> tests =
                    factory.CreateTests(new SimpleDatasetOpener(model.MasterDatabaseWorkspaceContext));
                Assert.AreEqual(1, tests.Count);

                IList <QaError> errors = Run(tests[0], 1000);
                AssertErrors(1, errors);

                IList <InvolvedRow> involvedRows = errors[0].InvolvedRows;
                Assert.AreEqual(2, involvedRows.Count);
                Assert.AreEqual($"{tableName}.{_textFieldName}", errors[0].AffectedComponent);
                Assert.AreEqual("RegularExpression.FieldValueDoesNotMatchRegularExpression",
                                errors[0].IssueCode?.ID);

                // TOP-4945: expected involved dataset name: base table name, not joined table name
                Assert.IsTrue(tableName == involvedRows[0].TableName ||
                              tableName == involvedRows[1].TableName);
            }
            finally
            {
                _testWs = initTestWs;
            }
        }