Skip to content

Commit

Permalink
Fix for column tests not rendering on quoted columns (#425)
Browse files Browse the repository at this point in the history
* (#201) Fix for column tests not rendering on quoted columns

* Add support for bigquery quotes

* use backticks for databricks and spark; add compat module and test
  • Loading branch information
drewbanin authored Jun 9, 2023
1 parent deb8cfa commit 2f9b8f9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Docs-20230531-115419.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Docs
body: Fix for column tests not rendering on quoted columns
time: 2023-05-31T11:54:19.687363-04:00
custom:
Author: drewbanin
Issue: "201"
16 changes: 16 additions & 0 deletions src/app/services/compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

function getQuoteChar(project_metadata) {
var backtickDatabases = ['bigquery', 'spark', 'databricks'];
var adapter_type = (project_metadata || {}).adapter_type;

if (backtickDatabases.indexOf(adapter_type) >= 0) {
return '`';
} else {
return '"';
}
}


module.exports = {
getQuoteChar,
}
22 changes: 22 additions & 0 deletions src/app/services/compat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

const compat = require("./compat");

const BACKTICK = '`';
const QUOTE = '"';


test("column quoting", () => {
expect(compat.getQuoteChar({adapter_type: 'bigquery'})).toStrictEqual(BACKTICK);
expect(compat.getQuoteChar({adapter_type: 'spark'})).toStrictEqual(BACKTICK);
expect(compat.getQuoteChar({adapter_type: 'databricks'})).toStrictEqual(BACKTICK);
expect(compat.getQuoteChar({adapter_type: 'postgres'})).toStrictEqual(QUOTE);
expect(compat.getQuoteChar({adapter_type: 'snowflake'})).toStrictEqual(QUOTE);
expect(compat.getQuoteChar({adapter_type: 'redshift'})).toStrictEqual(QUOTE);
expect(compat.getQuoteChar({adapter_type: 'unknown_db'})).toStrictEqual(QUOTE);
});

test("column quoting with invalid adapter", () => {
expect(compat.getQuoteChar({adapter_type: null})).toStrictEqual(QUOTE);
expect(compat.getQuoteChar({})).toStrictEqual(QUOTE);
expect(compat.getQuoteChar(null)).toStrictEqual(QUOTE);
});
11 changes: 10 additions & 1 deletion src/app/services/project_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const angular = require('angular');
const $ = require('jquery');
const _ = require('lodash');
const { getQuoteChar } = require('./compat');

import merge from 'deepmerge';

Expand All @@ -15,6 +16,7 @@ function capitalizeType(type) {
return type.charAt(0).toUpperCase() + type.slice(1);
}


angular
.module('dbt')
.factory('project', ['$q', '$http', function($q, $http) {
Expand Down Expand Up @@ -227,8 +229,15 @@ angular
var model = depends_on[0]
}
var node = project.nodes[model];
var quote_char = getQuoteChar(project.metadata);
var column = _.find(node.columns, function(col, col_name) {
return col_name.toLowerCase() == test_column.toLowerCase();
// strip quotes from start and end of test column if present in both locations
// this is necessary to attach a test to a column when `quote: true` is set for a column
let test_column_name = test_column;
if (test_column.startsWith(quote_char) && test_column.endsWith(quote_char)) {
test_column_name = test_column.substring(1, test_column.length-1);
}
return col_name.toLowerCase() == test_column_name.toLowerCase();
});

if (column) {
Expand Down

0 comments on commit 2f9b8f9

Please sign in to comment.