Skip to content

Commit

Permalink
feat: added test of trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Oct 6, 2023
1 parent 5d50436 commit f73e25d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 70 deletions.
79 changes: 10 additions & 69 deletions src/lib/features/instance-stats/getProductionChanges.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ const mockRawEventDaysAgo = (
days: number,
environment: string = 'production',
) => {
const day = subDays(new Date(), days);
return {
type: 'FEATURE_UPDATED',
created_by: 'testrunner',
environment,
feature_name: 'test.feature',
announced: true,
};
};

beforeAll(async () => {
Expand Down Expand Up @@ -78,77 +84,12 @@ test('should handle intervals of activity', async () => {
});
});

/*
test('an event being saved should add a count to the table', async () => {
const users = await db
.rawDatabase('events')
.insert(mockRawEventDaysAgo(mockEventDaysAgo(100))
.returning('id');
const userId = users[0].id;
await db
.rawDatabase('personal_access_tokens')
.insert(mockTokenDaysAgo(userId, 31));
await db.rawDatabase('events').insert(mockRawEventDaysAgo(70));

expect(getActiveUsers()).resolves.toEqual({
last7: 0,
expect(getProductionChanges()).resolves.toEqual({
last30: 0,
last60: 1,
last90: 1,
});
});
test('should prioritize user seen_at if newer then token seen_at', async () => {
const users = await db
.rawDatabase('users')
.insert(mockEventDaysAgo(14))
.returning('id');
const userId = users[0].id;
await db
.rawDatabase('personal_access_tokens')
.insert([
mockTokenDaysAgo(userId, 31),
mockTokenDaysAgo(userId, 61),
mockTokenDaysAgo(userId, 91),
]);
expect(getActiveUsers()).resolves.toEqual({
last7: 0,
last30: 1,
last60: 1,
last60: 0,
last90: 1,
});
});
test('should handle multiple users and with multiple tokens', async () => {
const users = await db
.rawDatabase('users')
.insert([
mockEventDaysAgo(5),
mockEventDaysAgo(10),
mockEventDaysAgo(20),
mockEventDaysAgo(40),
mockEventDaysAgo(70),
mockEventDaysAgo(100),
])
.returning('id');
await db
.rawDatabase('personal_access_tokens')
.insert([
mockTokenDaysAgo(users[0].id, 31),
mockTokenDaysAgo(users[1].id, 61),
mockTokenDaysAgo(users[1].id, 15),
mockTokenDaysAgo(users[1].id, 55),
mockTokenDaysAgo(users[2].id, 4),
mockTokenDaysAgo(users[3].id, 91),
mockTokenDaysAgo(users[4].id, 91),
]);
expect(getActiveUsers()).resolves.toEqual({
last7: 2,
last30: 3,
last60: 4,
last90: 5,
});
});
*/
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ exports.up = function(db, cb) {
CREATE FUNCTION unleash_update_stat_environment_changes_counter() RETURNS trigger AS $unleash_update_changes_counter$
BEGIN
INSERT INTO stat_environment_updates(day, environment, updates) SELECT DATE_TRUNC('Day', NEW.created_at), COALESCE(NEW.environment, 'unknown'), 1 ON CONFLICT (day, environment) DO UPDATE SET updates = EXCLUDED.updates + 1;
IF NEW.environment IS NOT NULL THEN
INSERT INTO stat_environment_updates(day, environment, updates) SELECT DATE_TRUNC('Day', NEW.created_at), NEW.environment, 1 ON CONFLICT (day, environment) DO UPDATE SET updates = EXCLUDED.updates + 1;
END IF;
return null;
END;
$unleash_update_changes_counter$ LANGUAGE plpgsql;
Expand Down

0 comments on commit f73e25d

Please sign in to comment.