Skip to content
Snippets Groups Projects
Commit 2d006e09 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

add a function in our schema metadata lib for `foreignKeyCountsByOriginTable`...

add a function in our schema metadata lib for `foreignKeyCountsByOriginTable` which counts the occurrences of FK relationships by the origin table they come from.  this allows us to know if a given origin table is represented multiple times in a list of FKs.
parent 3ee2dca8
No related branches found
No related tags found
No related merge requests found
......@@ -418,3 +418,21 @@ export function hasLatitudeAndLongitudeColumns(columnDefs) {
}
return hasLatitude && hasLongitude;
}
export function foreignKeyCountsByOriginTable(fks) {
if (fks === null || !Array.isArray(fks)) {
return null;
}
return fks.map(function(fk) {
return ('origin' in fk) ? fk.origin.table.id : null;
}).reduce(function(prev, curr, idx, array) {
if (curr in prev) {
prev[curr]++;
} else {
prev[curr] = 1;
}
return prev;
}, {});
}
......@@ -7,7 +7,8 @@ import {
NUMBER,
BOOLEAN,
LOCATION,
COORDINATE
COORDINATE,
foreignKeyCountsByOriginTable
} from 'metabase/lib/schema_metadata';
describe('schema_metadata', () => {
......@@ -44,4 +45,16 @@ describe('schema_metadata', () => {
expect(getFieldType({ base_type: 'DERP DERP DERP' })).toEqual(undefined)
});
});
describe('foreignKeyCountsByOriginTable', () => {
it('should work with null input', () => {
expect(foreignKeyCountsByOriginTable(null)).toEqual(null)
});
it('should require an array as input', () => {
expect(foreignKeyCountsByOriginTable({})).toEqual(null)
});
it('should count occurrences by origin.table.id', () => {
expect(foreignKeyCountsByOriginTable([{ origin: {table: {id: 123}} }, { origin: {table: {id: 123}} }, { origin: {table: {id: 123}} }, { origin: {table: {id: 456}} }])).toEqual({123: 3, 456: 1})
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment