Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Dec 12, 2024
1 parent ead686b commit 8479b18
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
32 changes: 19 additions & 13 deletions app/analytics/__tests__/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,31 @@ describe("AnalyticsEngineAPI", () => {

describe("getAllCountsByColumn", () => {
test("it should return an array of [column, count] tuples", async () => {
fetch.mockResolvedValue(
// return 2 mocked responses
fetch.mockResolvedValueOnce(
createFetchResponse({
data: [
{
blob4: "CA",
isVisitor: 1,
count: 3,
},
],
}),
);
fetch.mockResolvedValueOnce(
createFetchResponse({
data: [
{
blob4: "CA",
isVisitor: 0,
count: 1,
},
],
}),
);

const result = api.getAllCountsByColumn(
const result = await api.getAllCountsByColumn(
"example.com",
"country",
"7d",
Expand All @@ -316,24 +329,17 @@ describe("AnalyticsEngineAPI", () => {
},
);

expect(fetch).toHaveBeenCalled();
expect(fetch).toHaveBeenCalledTimes(2);
expect(
(fetch as Mock).mock.calls[0][1].body
.replace(/\s+/g, " ") // removes tabs and whitespace from query
.trim(),
).toEqual(
"SELECT blob4, " +
"double1 as isVisitor, " +
"double3 as isBounce, " +
"SUM(_sample_interval) as count " +
"FROM metricsDataset WHERE timestamp >= NOW() - INTERVAL '7' DAY AND timestamp < NOW() AND blob8 = 'example.com' AND blob4 = 'CA' " +
"GROUP BY blob4, double1, double3 " +
"ORDER BY count DESC LIMIT 10",
);
// console.log(result);
expect(await result).toEqual({
CA: {
views: 3,
visitors: 0,
views: 4,
visitors: 3,
bounces: 0,
},
});
Expand Down
7 changes: 3 additions & 4 deletions app/analytics/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export class AnalyticsEngineAPI {
const filterStr = filtersToSql(filters);
const _column = ColumnMappings[column];
const query = `
SELECT ${_column} as column,
SELECT ${_column},
${ColumnMappings.newVisitor} as isVisitor,
SUM(_sample_interval) as count
FROM metricsDataset
Expand All @@ -464,7 +464,6 @@ export class AnalyticsEngineAPI {
LIMIT ${limit * page}`;

type SelectionSet = {
column: string;
count: number;
isVisitor: number;
isBounce: number;
Expand Down Expand Up @@ -497,15 +496,15 @@ export class AnalyticsEngineAPI {
// the query results (pageData)
visitorCountByColumn.forEach(([key, value]) => {
pageData.push({
column: key,
[_column]: key,
count: value,
isVisitor: 1,
} as SelectionSet);
});

const result = pageData.reduce(
(acc, row) => {
const key = row["column"] as string;
const key = row[_column] as string;
if (!Object.hasOwn(acc, key)) {
acc[key] = {
views: 0,
Expand Down

0 comments on commit 8479b18

Please sign in to comment.