From 9486c2a4171e7b699dbacfa18cd411d0cfb85442 Mon Sep 17 00:00:00 2001 From: Aref Shafaei Date: Wed, 25 Oct 2023 18:28:27 -0700 Subject: [PATCH] add test cases for duplicate check in related API --- js/reference.js | 1 + .../conf/reference_schema_2/schema.json | 349 +++++++++++++++++- .../reference/tests/02.related_reference.js | 49 ++- 3 files changed, 392 insertions(+), 7 deletions(-) diff --git a/js/reference.js b/js/reference.js index e3d30dcf6..dfa51e79f 100644 --- a/js/reference.js +++ b/js/reference.js @@ -2596,6 +2596,7 @@ fkName = _sourceColumnHelpers.generateForeignKeyName(fkr, true); } + // it it's already added to the visible-columns (inline related) don't add it again. if (currentColumns[fkName]) { continue; } diff --git a/test/specs/reference/conf/reference_schema_2/schema.json b/test/specs/reference/conf/reference_schema_2/schema.json index e2b89e6b9..8b7204568 100644 --- a/test/specs/reference/conf/reference_schema_2/schema.json +++ b/test/specs/reference/conf/reference_schema_2/schema.json @@ -45,7 +45,28 @@ "typename": "text" } }], - "annotations": {} + "annotations": { + "tag:isrd.isi.edu,2016:visible-columns" : { + "*": [ + "id_1", "id_2", "id_3", + ["reference_schema_2", "fk_inbound_related_table_1_to_reference_table_no_order"], + { + "source": [ + {"inbound": ["reference_schema_2", "fk_inbound_related_table_2_to_reference_table_no_order"]}, + "RID" + ] + }, + ["reference_schema_2", "fk_assoc_table_1_to_reference_table_no_order"], + { + "source": [ + {"inbound": ["reference_schema_2", "fk_assoc_table_2_to_reference_table_no_order"]}, + {"outbound": ["reference_schema_2", "fk_assoc_table_2_to_assoc_related_table_2"]}, + "RID" + ] + } + ] + } + } }, "related_reference_no_order": { "kind": "table", @@ -245,6 +266,332 @@ "type": {"typename": "text"} } ] + }, + "inbound_related_table_1": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "inbound_related_table_1", + "keys": [ + {"unique_columns": ["id"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "fk_inbound_related_table_1_to_reference_table_no_order"]], + "foreign_key_columns": [{ + "table_name": "inbound_related_table_1", + "schema_name": "reference_schema_2", + "column_name": "id" + }], + "referenced_columns": [{ + "table_name": "reference_table_no_order", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }] + } + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "inbound_related_table_2": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "inbound_related_table_2", + "keys": [ + {"unique_columns": ["id"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "fk_inbound_related_table_2_to_reference_table_no_order"]], + "foreign_key_columns": [{ + "table_name": "inbound_related_table_2", + "schema_name": "reference_schema_2", + "column_name": "id" + }], + "referenced_columns": [{ + "table_name": "reference_table_no_order", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }] + } + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_table_1": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_table_1", + "keys": [ + {"unique_columns": ["id_1", "id_2"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "fk_assoc_table_1_to_reference_table_no_order"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_1", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }], + "referenced_columns": [{ + "table_name": "reference_table_no_order", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }] + }, + { + "comment": null, + "names": [["reference_schema_2", "fk_assoc_table_1_to_assoc_related_table_1"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_1", + "schema_name": "reference_schema_2", + "column_name": "id_2" + }], + "referenced_columns": [{ + "table_name": "assoc_related_table_1", + "schema_name": "reference_schema_2", + "column_name": "id" + }] + } + ], + "column_definitions": [ + { + "name": "id_1", + "nullok": false, + "type": {"typename": "text"} + }, + { + "name": "id_2", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_related_table_1": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_related_table_1", + "keys": [ + {"unique_columns": ["id"]} + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_table_2": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_table_2", + "keys": [ + {"unique_columns": ["id_1", "id_2"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "fk_assoc_table_2_to_reference_table_no_order"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_2", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }], + "referenced_columns": [{ + "table_name": "reference_table_no_order", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }] + }, + { + "comment": null, + "names": [["reference_schema_2", "fk_assoc_table_2_to_assoc_related_table_2"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_2", + "schema_name": "reference_schema_2", + "column_name": "id_2" + }], + "referenced_columns": [{ + "table_name": "assoc_related_table_2", + "schema_name": "reference_schema_2", + "column_name": "id" + }] + } + ], + "column_definitions": [ + { + "name": "id_1", + "nullok": false, + "type": {"typename": "text"} + }, + { + "name": "id_2", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_related_table_2": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_related_table_2", + "keys": [ + {"unique_columns": ["id"]} + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "table_w_vis_fks_and_vis_cols": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "table_w_vis_fks_and_vis_cols", + "keys": [ + {"unique_columns": ["id"]} + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ], + "annotations": { + "tag:isrd.isi.edu,2016:visible-columns": { + "detailed": [ + "id", + ["reference_schema_2", "inbound_related_to_table_w_vis_fks_fk1"], + ["reference_schema_2", "assoc_table_to_table_w_vis_fks_fk1"] + ] + }, + "tag:isrd.isi.edu,2016:visible-foreign-keys": { + "detailed": [ + { + "source": [{"inbound": ["reference_schema_2", "inbound_related_to_table_w_vis_fks_fk1"]}, "RID"] + }, + { + "source": [ + {"inbound": ["reference_schema_2", "assoc_table_to_table_w_vis_fks_fk1"]}, + {"outbound": ["reference_schema_2", "assoc_table_to_table_w_vis_fks_fk2"]}, + "RID" + ] + } + ] + } + } + }, + "inbound_related_to_table_w_vis_fks": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "inbound_related_to_table_w_vis_fks", + "keys": [ + {"unique_columns": ["id"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "inbound_related_to_table_w_vis_fks_fk1"]], + "foreign_key_columns": [{ + "table_name": "inbound_related_to_table_w_vis_fks", + "schema_name": "reference_schema_2", + "column_name": "id" + }], + "referenced_columns": [{ + "table_name": "table_w_vis_fks_and_vis_cols", + "schema_name": "reference_schema_2", + "column_name": "id" + }] + } + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_table_to_table_w_vis_fks": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_table_to_table_w_vis_fks", + "keys": [ + {"unique_columns": ["id_1", "id_2"]} + ], + "foreign_keys": [ + { + "comment": null, + "names": [["reference_schema_2", "assoc_table_to_table_w_vis_fks_fk1"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_to_table_w_vis_fks", + "schema_name": "reference_schema_2", + "column_name": "id_1" + }], + "referenced_columns": [{ + "table_name": "table_w_vis_fks_and_vis_cols", + "schema_name": "reference_schema_2", + "column_name": "id" + }] + }, + { + "comment": null, + "names": [["reference_schema_2", "assoc_table_to_table_w_vis_fks_fk2"]], + "foreign_key_columns": [{ + "table_name": "assoc_table_to_table_w_vis_fks", + "schema_name": "reference_schema_2", + "column_name": "id_2" + }], + "referenced_columns": [{ + "table_name": "assoc_related_to_table_w_vis_fks", + "schema_name": "reference_schema_2", + "column_name": "id" + }] + } + ], + "column_definitions": [ + { + "name": "id_1", + "nullok": false, + "type": {"typename": "text"} + }, + { + "name": "id_2", + "nullok": false, + "type": {"typename": "text"} + } + ] + }, + "assoc_related_to_table_w_vis_fks": { + "kind": "table", + "schema_name": "reference_schema_2", + "table_name": "assoc_related_to_table_w_vis_fks", + "keys": [ + {"unique_columns": ["id"]} + ], + "column_definitions": [ + { + "name": "id", + "nullok": false, + "type": {"typename": "text"} + } + ] } }, "comment": null, diff --git a/test/specs/reference/tests/02.related_reference.js b/test/specs/reference/tests/02.related_reference.js index dfc5901f8..555180bbd 100644 --- a/test/specs/reference/tests/02.related_reference.js +++ b/test/specs/reference/tests/02.related_reference.js @@ -4,6 +4,7 @@ exports.execute = function(options) { describe('.related, ', function() { var catalog_id = process.env.DEFAULT_CATALOG, schemaName = "reference_schema", + schemaName2 = "reference_schema_2", tableName = "reference_table", inboundTableName = "inbound_related_reference_table", associationTableWithToName = "association_table_with_toname", @@ -121,9 +122,8 @@ exports.execute = function(options) { describe("related reference list, ", function () { describe('when visible foreign keys are not defined, ', function() { - var schemaName2 = "reference_schema_2", - tableName2 = "reference_table_no_order", - related2; + var tableName2 = "reference_table_no_order", + ref2, related2; var noOrderUri = options.url + "/catalog/" + catalog_id + "/entity/" + schemaName2 + ":" + tableName2; @@ -131,15 +131,29 @@ exports.execute = function(options) { options.ermRest.resolve(noOrderUri, { cid: "test" }).then(function(response) { - related2 = response.contextualize.detailed.related; + ref2 = response.contextualize.detailed; + related2 = ref2.related; done(); }, function(err) { done.fail(err); }); }); - it('should include all foreign keys.', function() { + it('should include all foreign keys and exclude the ones already added to visible-columns.', function() { expect(related2.length).toBe(4); + + // just a sanity check that the visible-columns is correctly added. + // the actual tests for .columns API are done in a separate spec + expect(ref2.columns.length).toBe(7); + expect(ref2.columns.map((col) => { + return col._constraintName ? col._constraintName : col.name + })).toEqual([ + 'id_1', 'id_2', 'id_3', + ['reference_schema_2', 'fk_inbound_related_table_1_to_reference_table_no_order'].join('_'), + ['reference_schema_2', 'fk_inbound_related_table_2_to_reference_table_no_order'].join('_'), + ['reference_schema_2', 'fk_assoc_table_1_to_reference_table_no_order'].join('_'), + ['reference_schema_2', 'fk_assoc_table_2_to_reference_table_no_order'].join('_') + ]); }); it('should be sorted by displayname.', function() { @@ -159,12 +173,35 @@ exports.execute = function(options) { }); describe("when visible foreign keys are defined, ", function () { + it ('should ignore the ones already added to visible-columns (inline realted).', (done) => { + options.ermRest.resolve(`${options.url}/catalog/${catalog_id}/entity/${schemaName2}:table_w_vis_fks_and_vis_cols`, { + cid: "test" + }).then((response) => { + response = response.contextualize.detailed; + + expect(response.related.length).toBe(0, 'related length missmatch'); + + // just a sanity check that the visible-columns is correctly added. + // the actual tests for .columns API are done in a separate spec + expect(response.columns.length).toBe(3, 'columns length missmatch'); + expect(response.columns.map((col) => { + return col._constraintName ? col._constraintName : col.name + })).toEqual([ + 'id', + ['reference_schema_2', 'inbound_related_to_table_w_vis_fks_fk1'].join('_'), + ['reference_schema_2', 'assoc_table_to_table_w_vis_fks_fk1'].join('_'), + ], 'colum names mismatch'); + + done(); + }).catch((err) => done.fail(err)); + }); + it('should be defined and not empty.', function() { expect(reference.related).toBeDefined(); expect(related).not.toEqual([]); }); - it('should only include visible foreign keys that are defined in the annotation. Should support path.', function() { + it('should only include visible foreign keys that are defined in the annotation. Should support path', function() { expect(related.length).toBe(7); });