Преглед изворни кода

HUE-6229 [metadata] Fix issue with nav opt params for tables that have the same name as databases

Johan Ahlen пре 8 година
родитељ
комит
9fc514f

+ 12 - 4
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -1498,13 +1498,21 @@ var ApiHelper = (function () {
     }));
   };
 
-  ApiHelper.prototype.createNavDbTablesJson = function (options) {
+  ApiHelper.prototype.createNavOptDbTablesJson = function (options) {
     var self = this;
     var dbTables = [];
     options.tables.forEach(function (table) {
       var clonedIdentifierChain = table.identifierChain.concat();
-      var database = options.defaultDatabase && !self.containsDatabase(options.sourceType, clonedIdentifierChain[0].name) ? options.defaultDatabase : clonedIdentifierChain.shift().name;
-      dbTables.push(database + '.' + $.map(clonedIdentifierChain, function (identifier) { return identifier.name }).join('.'));
+
+      var databasePrefix;
+      if (clonedIdentifierChain.length > 1 && self.containsDatabase(options.sourceType, clonedIdentifierChain[0].name)) {
+        databasePrefix = clonedIdentifierChain.shift().name + '.';
+      } else if (options.defaultDatabase) {
+        databasePrefix = options.defaultDatabase + '.';
+      } else {
+        databasePrefix = '';
+      }
+      dbTables.push(databasePrefix  + $.map(clonedIdentifierChain, function (identifier) { return identifier.name }).join('.'));
     });
     return ko.mapping.toJSON(dbTables);
   };
@@ -1618,7 +1626,7 @@ var ApiHelper = (function () {
     var data, hash;
     if (options.tables) {
       data = {
-        dbTables: self.createNavDbTablesJson(options)
+        dbTables: self.createNavOptDbTablesJson(options)
       };
       hash = data.dbTables.hashCode();
     } else if (options.database) {

+ 86 - 12
desktop/core/src/desktop/static/desktop/spec/apiHelperSpec.js

@@ -15,49 +15,123 @@
 // limitations under the License.
 (function() {
 
-  describe("apiHelper.js", function() {
+  describe('apiHelper.js', function() {
     var subject = ApiHelper.getInstance({
       i18n: {},
       user: 'testUser'
     });
 
-    it("should be singleton", function() {
+    it('should be singleton', function() {
       var otherHelper = ApiHelper.getInstance();
-      expect(subject == otherHelper).toBeTruthy();
+      expect(subject === otherHelper).toBeTruthy();
     });
 
-    describe("success response that is actually an error", function () {
-      it("should not determine that a success response is an error response if status is 0", function () {
+    describe('success response that is actually an error', function () {
+      it('should not determine that a success response is an error response if status is 0', function () {
         expect(subject.successResponseIsError({ status: 0 })).toBeFalsy();
       });
 
-      it("should determine that a success response is an error response if status is 1", function () {
+      it('should determine that a success response is an error response if status is 1', function () {
         expect(subject.successResponseIsError({ status: 1 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if status is -1", function () {
+      it('should determine that a success response is an error response if status is -1', function () {
         expect(subject.successResponseIsError({ status: -1 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if status is -3", function () {
+      it('should determine that a success response is an error response if status is -3', function () {
         expect(subject.successResponseIsError({ status: -3 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if status is 500", function () {
+      it('should determine that a success response is an error response if status is 500', function () {
         expect(subject.successResponseIsError({ status: 500 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if code is 500", function () {
+      it('should determine that a success response is an error response if code is 500', function () {
         expect(subject.successResponseIsError({ code: 500 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if code is 503", function () {
+      it('should determine that a success response is an error response if code is 503', function () {
         expect(subject.successResponseIsError({ code: 503 })).toBeTruthy();
       });
 
-      it("should determine that a success response is an error response if it contains traceback", function () {
+      it('should determine that a success response is an error response if it contains traceback', function () {
         expect(subject.successResponseIsError({ traceback: {} })).toBeTruthy();
       });
     });
+    
+    describe('NavOpt', function () {
+      describe('Tables JSON generation', function () {
+        it('should add the default database when no database is found in the identifier chain', function () {
+          spyOn(subject, 'containsDatabase').and.callFake(function () {
+            return false;
+          });
+
+          var result = subject.createNavOptDbTablesJson({
+            defaultDatabase: 'default',
+            sourceType: 'hive',
+            tables: [{ identifierChain: [{ name: 'some_table' }] }]
+          });
+
+          expect(result).toEqual('["default.some_table"]');
+        });
+
+        it('should add the database from the identifier chain if found', function () {
+          spyOn(subject, 'containsDatabase').and.callFake(function () {
+            return true;
+          });
+
+          var result = subject.createNavOptDbTablesJson({
+            defaultDatabase: 'default',
+            sourceType: 'hive',
+            tables: [{ identifierChain: [{ name: 'some_db' }, { name: 'some_table' }] }]
+          });
+
+          expect(result).toEqual('["some_db.some_table"]');
+        });
+
+        it('should support tables with same names as databases', function () {
+          spyOn(subject, 'containsDatabase').and.callFake(function () {
+            return true;
+          });
+
+          var result = subject.createNavOptDbTablesJson({
+            defaultDatabase: 'default',
+            sourceType: 'hive',
+            tables: [{ identifierChain: [{ name: 'table_and_db_name' }] }]
+          });
+
+          expect(result).toEqual('["default.table_and_db_name"]');
+        });
+
+        it('should support tables with same names as databases', function () {
+          spyOn(subject, 'containsDatabase').and.callFake(function () {
+            return true;
+          });
+
+          var result = subject.createNavOptDbTablesJson({
+            defaultDatabase: 'default',
+            sourceType: 'hive',
+            tables: [{ identifierChain: [{ name: 'table_and_db_name' }, { name: 'table_and_db_name' }] }]
+          });
+
+          expect(result).toEqual('["table_and_db_name.table_and_db_name"]');
+        });
+
+        it('should support multiple tables some with databases some without', function () {
+          spyOn(subject, 'containsDatabase').and.callFake(function () {
+            return true;
+          });
+
+          var result = subject.createNavOptDbTablesJson({
+            defaultDatabase: 'default',
+            sourceType: 'hive',
+            tables: [{ identifierChain: [{ name: 'a_table_from_default' }] }, { identifierChain: [{ name: 'other_db' }, { name: 'a_table_from_other_db' }] }]
+          });
+
+          expect(result).toEqual('["default.a_table_from_default","other_db.a_table_from_other_db"]');
+        })
+      });
+    })
   });
 })();