Skip to content

Commit

Permalink
test: for widgets/models
Browse files Browse the repository at this point in the history
Add unit tests for Category, Formula and Histogram widget models
  • Loading branch information
AdriSolid authored Jan 29, 2021
1 parent d954d9b commit 64fe1a4
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/tests/widgets/data-mocks/linesForFormula.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export const LINES = (column) => [
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[0, 0],
[0, 1]
]
},
properties: {
[column]: 1
}
},
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[0, 0],
[0, 2]
]
},
properties: {
[column]: 2
}
},
{
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[0, 0],
[0, 4]
]
},
properties: {
[column]: 3
}
}
];
35 changes: 35 additions & 0 deletions src/tests/widgets/data-mocks/pointsForCategories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export const POINTS = (column, operationColumn) => [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [[0, 0]]
},
properties: {
[column]: 'a',
[operationColumn]: 1
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [[0, 1]]
},
properties: {
[column]: 'a',
[operationColumn]: 2
}
},
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [[0, 2]]
},
properties: {
[column]: 'b',
[operationColumn]: 3
}
}
];
56 changes: 56 additions & 0 deletions src/tests/widgets/data-mocks/polygonsForHistogram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const POLYGONS = (operationColumn) => [
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0]
]
]
},
properties: {
[operationColumn]: 1
}
},
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[1, 1],
[1, 2],
[2, 2],
[2, 1],
[1, 1]
]
]
},
properties: {
[operationColumn]: 2
}
},
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[2, 2],
[2, 3],
[3, 3],
[3, 2],
[2, 2]
]
]
},
properties: {
[operationColumn]: 3
}
}
];
149 changes: 149 additions & 0 deletions src/tests/widgets/models/CategoryModel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { minify } from 'pgsql-minify';

import {
getCategories,
buildSqlQueryToGetCategories,
filterViewportFeaturesToGetCategories
} from 'src/widgets/models/CategoryModel';
import { AggregationTypes } from 'src/widgets/AggregationTypes';
import { LayerTypes } from 'src/widgets/LayerTypes';
import { POINTS } from '../data-mocks/pointsForCategories';

describe('getCategories', () => {
test('should thrown an error due to invalid data type', () => {
try {
getCategories({ data: [] });
} catch (err) {
expect(err).toBe('Array is not a valid type to get categories');
}
});

test('should thrown an error if trying to implement client-side-logic with CartoBQTilerLayer', () => {
try {
getCategories({ type: LayerTypes.BQ, viewportFilter: true });
} catch (err) {
expect(err).toBe(
'Category Widget error: BigQuery layers need "viewportFilter" prop set to true.'
);
}
});

describe('buildSqlQueryToGetCategories - simple global operations', () => {
const createArguments = (operation) => ({
data: 'SELECT cartodb_id, storetype, revenue FROM retail_stores',
column: 'storetype',
operationColumn: 'revenue',
operation
});

const buildQuery = (args) => `
WITH all_categories as (
SELECT
${args.column} as category
FROM
(${args.data}) as q
GROUP BY category
),
categories as (
SELECT
${args.column} as category, ${args.operation}(${args.operationColumn}) as value
FROM
(${args.data}) as q
GROUP BY category
)
SELECT
a.category, b.value
FROM
all_categories a
LEFT JOIN categories b ON a.category=b.category
`;

test(AggregationTypes.COUNT, () => {
const args = createArguments(AggregationTypes.COUNT);
const query = buildQuery(args);
expect(buildSqlQueryToGetCategories(args)).toEqual(minify(query));
});

test(AggregationTypes.AVG, () => {
const args = createArguments(AggregationTypes.AVG);
const query = buildQuery(args);
expect(buildSqlQueryToGetCategories(args)).toEqual(minify(query));
});

test(AggregationTypes.SUM, () => {
const args = createArguments(AggregationTypes.SUM);
const query = buildQuery(args);
expect(buildSqlQueryToGetCategories(args)).toEqual(minify(query));
});

test(AggregationTypes.MIN, () => {
const args = createArguments(AggregationTypes.MIN);
const query = buildQuery(args);
expect(buildSqlQueryToGetCategories(args)).toEqual(minify(query));
});

test(AggregationTypes.MAX, () => {
const args = createArguments(AggregationTypes.MAX);
const query = buildQuery(args);
expect(buildSqlQueryToGetCategories(args)).toEqual(minify(query));
});
});

describe('filterViewportFeaturesToGetCategories - simple viewport features filtering', () => {
const createArguments = (operation) => ({
operation,
column: 'storetype',
operationColumn: 'revenue',
viewportFeatures: POINTS('storetype', 'revenue')
});

test(AggregationTypes.COUNT, () => {
const args = createArguments(AggregationTypes.COUNT);
expect(filterViewportFeaturesToGetCategories(args)).toEqual([
{ category: 'a', value: 2 },
{ category: 'b', value: 1 }
]);
});

test(AggregationTypes.AVG, () => {
const args = createArguments(AggregationTypes.AVG);
expect(filterViewportFeaturesToGetCategories(args)).toEqual([
{ category: 'a', value: 1.5 },
{ category: 'b', value: 3 }
]);
});

test(AggregationTypes.SUM, () => {
const args = createArguments(AggregationTypes.SUM);
expect(filterViewportFeaturesToGetCategories(args)).toEqual([
{ category: 'a', value: 3 },
{ category: 'b', value: 3 }
]);
});

test(AggregationTypes.MIN, () => {
const args = createArguments(AggregationTypes.MIN);
expect(filterViewportFeaturesToGetCategories(args)).toEqual([
{ category: 'a', value: 1 },
{ category: 'b', value: 3 }
]);
});

test(AggregationTypes.MAX, () => {
const args = createArguments(AggregationTypes.MAX);
expect(filterViewportFeaturesToGetCategories(args)).toEqual([
{ category: 'a', value: 2 },
{ category: 'b', value: 3 }
]);
});

test('no features', () => {
const testCases = [null, undefined];
for (const tc of testCases) {
expect(filterViewportFeaturesToGetCategories({ viewportFeatures: tc })).toEqual(
[]
);
}
});
});
});
103 changes: 103 additions & 0 deletions src/tests/widgets/models/FormulaModel.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { minify } from 'pgsql-minify';

import {
getFormula,
buildSqlQueryToGetFormula,
filterViewportFeaturesToGetFormula
} from 'src/widgets/models/FormulaModel';
import { AggregationTypes } from 'src/widgets/AggregationTypes';
import { LayerTypes } from 'src/widgets/LayerTypes';
import { LINES } from '../data-mocks/linesForFormula';

describe('getFormula', () => {
test('should thrown an error due to invalid data type', () => {
try {
getFormula({ data: [] });
} catch (err) {
expect(err).toBe('Array is not a valid type to get formula');
}
});

test('should thrown an error if trying to implement client-side-logic with CartoBQTilerLayer', () => {
try {
getFormula({ type: LayerTypes.BQ, viewportFilter: true });
} catch (err) {
expect(err).toBe(
'Formula Widget error: BigQuery layers need "viewportFilter" prop set to true.'
);
}
});

describe('buildSqlQueryToGetFormula - simple global operations', () => {
const createArguments = (operation) => ({
data: 'SELECT * FROM stores',
column: 'revenue',
operation
});

const buildQuery = (type) =>
`SELECT ${type}(revenue) as value FROM (SELECT * FROM stores) as q`;

test(AggregationTypes.COUNT, () => {
const args = createArguments(AggregationTypes.COUNT);
const query = buildQuery(args.operation);
expect(buildSqlQueryToGetFormula(args)).toEqual(minify(query));
});

test(AggregationTypes.AVG, () => {
const args = createArguments(AggregationTypes.AVG);
const query = buildQuery(args.operation);
expect(buildSqlQueryToGetFormula(args)).toEqual(minify(query));
});

test(AggregationTypes.SUM, () => {
const args = createArguments(AggregationTypes.SUM);
const query = buildQuery(args.operation);
expect(buildSqlQueryToGetFormula(args)).toEqual(minify(query));
});

test(AggregationTypes.MIN, () => {
const args = createArguments(AggregationTypes.MIN);
const query = buildQuery(args.operation);
expect(buildSqlQueryToGetFormula(args)).toEqual(minify(query));
});

test(AggregationTypes.MAX, () => {
const args = createArguments(AggregationTypes.MAX);
const query = buildQuery(args.operation);
expect(buildSqlQueryToGetFormula(args)).toEqual(minify(query));
});
});

describe('filterViewportFeaturesToGetFormula - simple viewport features filtering', () => {
const createArguments = (operation) => ({
operation,
column: 'revenue',
viewportFeatures: LINES('revenue')
});

test(AggregationTypes.COUNT, () => {
const args = createArguments(AggregationTypes.COUNT);
expect(filterViewportFeaturesToGetFormula(args)).toEqual([{ value: 3 }]);
});

test(AggregationTypes.AVG, () => {
const args = createArguments(AggregationTypes.AVG);
expect(filterViewportFeaturesToGetFormula(args)).toEqual([{ value: 2 }]);
});

test(AggregationTypes.SUM, () => {
const args = createArguments(AggregationTypes.SUM);
expect(filterViewportFeaturesToGetFormula(args)).toEqual([{ value: 6 }]);
});

test('no features', () => {
const testCases = [null, undefined];
for (const tc of testCases) {
expect(filterViewportFeaturesToGetFormula({ viewportFeatures: tc })).toEqual([
{ value: null }
]);
}
});
});
});
Loading

0 comments on commit 64fe1a4

Please sign in to comment.