Skip to content

Commit

Permalink
add mock for typeormAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenjohanson committed Dec 23, 2024
1 parent 9ecabd9 commit dc68bc1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
9 changes: 2 additions & 7 deletions teammapper-backend/src/database/typeorm-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,8 @@ Query Patterns:

// Analyze query patterns
const queryPatterns = new Map<string, number>();
this.queryLog.forEach(log => {
// Simplify query by removing specific values
const pattern = log.query.replace(/\b\d+\b/g, 'N')
.replace(/'[^']*'/g, '?')
.replace(/"[^"]*"/g, '?');

queryPatterns.set(pattern, (queryPatterns.get(pattern) || 0) + 1);
this.queryLog.forEach(log => {
queryPatterns.set(log.query, (queryPatterns.get(log.query) || 0) + 1);
});

// Sort patterns by frequency
Expand Down
7 changes: 6 additions & 1 deletion teammapper-backend/src/map/services/maps.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ import {
import { mapMmpNodeToClient } from '../utils/clientServerMapping'
import { truncateDatabase } from 'test/helper'
import { jest } from '@jest/globals'
import { TypeORMAnalyzerService } from 'src/database/typeorm-analyzer.service'

describe('MapsController', () => {
let mapsService: MapsService
let nodesRepo: Repository<MmpNode>
let mapsRepo: Repository<MmpMap>
let moduleFixture: TestingModule
let typeormAnalyzer: jest.Mocked<TypeORMAnalyzerService>

beforeAll(async () => {
// Calling advanceTimers here is very important, as otherwise async ops like await will hang indefinitely
// Ref: https://jestjs.io/docs/jest-object#jestusefaketimersfaketimersconfig
jest.useFakeTimers({ advanceTimers: true })

typeormAnalyzer = {
} as jest.Mocked<TypeORMAnalyzerService>

moduleFixture = await Test.createTestingModule({
imports: [
ConfigModule,
Expand All @@ -41,7 +46,7 @@ describe('MapsController', () => {
getRepositoryToken(MmpNode)
)

mapsService = new MapsService(nodesRepo, mapsRepo)
mapsService = new MapsService(nodesRepo, mapsRepo, typeormAnalyzer)
})

afterAll(async () => {
Expand Down
2 changes: 2 additions & 0 deletions teammapper-backend/src/map/services/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ export class MapsService {
await callbacks[key](changes);
}
}));

this.typeormAnalyzer.logAnalysis();
}

async updateMapOptions(
Expand Down

0 comments on commit dc68bc1

Please sign in to comment.