Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests, Fix minor bugs, Apply refactorings #1491 #1571

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions visualization/app/codeCharta/ui/codeMap/codeMap.render.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ import { CodeMapArrowService } from "./codeMap.arrow.service"
import { CodeMapNode, Node } from "../../codeCharta.model"
import { StoreService } from "../../state/store.service"
import { isDeltaState } from "../../model/files/files.helper"
import { FileState } from "../../model/files/files"

const ONE_MB = 1024 * 1024

export enum MAP_RESOLUTION_SCALE {
SMALL_MAP = 1,
MEDIUM_MAP = 0.5,
BIG_MAP = 0.25
}
export function getMapResolutionScaleFactor(files: FileState[]) {
let totalFileSize = 0
for (const file of files) {
totalFileSize += file.file.fileMeta.exportedFileSize
}

switch (true) {
case totalFileSize >= 7 * ONE_MB:
return 0.25
case totalFileSize >= 2 * ONE_MB:
return 0.5
default:
return 1
}
}

export class CodeMapRenderService {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
RepeatWrapping,
DoubleSide
} from "three"
import { getMapResolutionScaleFactor, MAP_RESOLUTION_SCALE } from "../codeMap.render.service"
import { getMapResolutionScaleFactor, MAP_RESOLUTION_SCALE } from "../../../util/codeMapHelper"

export interface BoxMeasures {
x: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,45 @@ describe("fileChooserController", () => {

const file1 = {
name: "invalid.and.missing.md5.checksum.cc.json",
size: 42
size: 43
} as File

const file2 = {
name: "invalid.and.empty.md5.checksum.cc.json",
size: 42
size: 44
} as File

const file3 = {
name: "invalid.and.nulled.md5.checksum.cc.json",
size: 42
size: 45
} as File

fileChooserController["addNameDataPair"](file0, '{"fileChecksum":"invalid-but-present-md5-checksum"}')

expect(fileChooserController["files"].length).toBe(1)
expect(fileChooserController["files"][0].fileName).toBe("invalid.with.md5.checksum.cc.json")
expect(fileChooserController["files"][0].fileSize).toBe(42)
expect(fileChooserController["files"][0].content.fileChecksum).toBe("invalid-but-present-md5-checksum")

fileChooserController["addNameDataPair"](file1, "{}")

expect(fileChooserController["files"].length).toBe(2)
expect(fileChooserController["files"][1].fileName).toBe("invalid.and.missing.md5.checksum.cc.json")
expect(fileChooserController["files"][1].fileSize).toBe(43)
expect(fileChooserController["files"][1].content.fileChecksum).toBe("99914b932bd37a50b983c5e7c90ae93b")

fileChooserController["addNameDataPair"](file2, '{"fileChecksum":""}')

expect(fileChooserController["files"].length).toBe(3)
expect(fileChooserController["files"][2].fileName).toBe("invalid.and.empty.md5.checksum.cc.json")
expect(fileChooserController["files"][2].fileSize).toBe(44)
expect(fileChooserController["files"][2].content.fileChecksum).toBe("21a6f66227ae28300d656b8107765e7f")

fileChooserController["addNameDataPair"](file3, '{"fileChecksum":null}')

expect(fileChooserController["files"].length).toBe(4)
expect(fileChooserController["files"][3].fileName).toBe("invalid.and.nulled.md5.checksum.cc.json")
expect(fileChooserController["files"][3].fileSize).toBe(45)
expect(fileChooserController["files"][3].content.fileChecksum).toBe("44f0fdb79d97053b25dce38611c117f0")
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`AggregationGenerator multipleService aggregation of four maps 1`] = `
Object {
"fileMeta": Object {
"apiVersion": "1.2",
"exportedFileSize": 1800000,
"exportedFileSize": 1200000,
"fileChecksum": "md5-file1;md5-file2;md5-file1;md5-file2;md5-file1;md5-file2",
"fileName": "file_aggregation_of_file1_and_file2_and_file1_and_file2",
"projectName": "project_aggregation_of_Sample_Project_and_Sample_Project_and_Sample_Project_and_Sample_Project",
Expand Down
1 change: 1 addition & 0 deletions visualization/app/codeCharta/util/aggregationGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,6 @@ export class AggregationGenerator {
private static resetVariables() {
this.projectNameArray = []
this.fileNameArray = []
this.fileSizesSum = 0
}
}
137 changes: 137 additions & 0 deletions visualization/app/codeCharta/util/codeMapHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { CCFile } from "../codeCharta.model"
import packageJson from "../../../package.json"
import { getMapResolutionScaleFactor, MAP_RESOLUTION_SCALE } from "./codeMapHelper"
import { FileSelectionState, FileState } from "../model/files/files"

describe("CodeMapHelper", () => {
describe("getMapResolutionScaleFactor", () => {
it("should get map resolution scale factor SMALL for a single and multiple maps", () => {
const smallFile1 = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1024 // 1MB
}
} as CCFile

const smallFile2 = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1023 * 1024 // nearly 1 MB
}
} as CCFile

const fileStateSingle: FileState[] = [{ file: smallFile1, selectedAs: FileSelectionState.Single }]

expect(getMapResolutionScaleFactor(fileStateSingle)).toBe(MAP_RESOLUTION_SCALE.SMALL_MAP)

const fileStateMultiple: FileState[] = [
{
file: smallFile1,
selectedAs: FileSelectionState.Partial
},
{
file: smallFile2,
selectedAs: FileSelectionState.Partial
}
]

expect(getMapResolutionScaleFactor(fileStateMultiple)).toBe(MAP_RESOLUTION_SCALE.SMALL_MAP)
})

it("should get map resolution scale factor MEDIUM for a single map", () => {
const oneMBFile = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 2048 * 1024 // 2MB
}
} as CCFile

const fileStateSingle: FileState[] = [{ file: oneMBFile, selectedAs: FileSelectionState.Single }]

expect(getMapResolutionScaleFactor(fileStateSingle)).toBe(MAP_RESOLUTION_SCALE.MEDIUM_MAP)
})

it("should get map resolution scale factor MEDIUM for multiple maps", () => {
const almostOneMBFile = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1023 * 1024 // nearly one MB
}
} as CCFile

const sixMBFile = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1024 * 1024 * 6 // 6MB
}
} as CCFile

const fileStateMultiple: FileState[] = [
{ file: almostOneMBFile, selectedAs: FileSelectionState.Partial },
{ file: sixMBFile, selectedAs: FileSelectionState.Partial }
]

expect(getMapResolutionScaleFactor(fileStateMultiple)).toBe(MAP_RESOLUTION_SCALE.MEDIUM_MAP)
})

it("should get map resolution scale factor BIG for single map", () => {
const bigFile1 = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1024 * 1024 * 20 // 20MB
}
} as CCFile

const fileStateSingle: FileState[] = [{ file: bigFile1, selectedAs: FileSelectionState.Partial }]

expect(getMapResolutionScaleFactor(fileStateSingle)).toBe(MAP_RESOLUTION_SCALE.BIG_MAP)
})

it("should get map resolution scale factor BIG for multiple maps", () => {
const fiveMBFile = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1024 * 1024 * 5 // 5MB
}
} as CCFile

const twoMBFile = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 1024 * 1024 * 2 // 2MB
}
} as CCFile

const fileStateMultiple: FileState[] = [
{ file: fiveMBFile, selectedAs: FileSelectionState.Partial },
{ file: twoMBFile, selectedAs: FileSelectionState.Partial }
]

expect(getMapResolutionScaleFactor(fileStateMultiple)).toBe(MAP_RESOLUTION_SCALE.BIG_MAP)
})
})
})
22 changes: 22 additions & 0 deletions visualization/app/codeCharta/util/codeMapHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { hierarchy, HierarchyNode } from "d3-hierarchy"
import { BlacklistItem, BlacklistType, CodeMapNode, MarkedPackage } from "../codeCharta.model"
import ignore from "ignore"
import { FileState } from "../model/files/files"
import { getSelectedFilesSize } from "./fileHelper"

export function getAnyCodeMapNodeFromPath(path: string, root: CodeMapNode) {
const matchingNode = hierarchy(root).find(({ data }) => data.path === path)
Expand Down Expand Up @@ -110,3 +112,23 @@ export function isBlacklisted(node: CodeMapNode) {
export function isLeaf(node: CodeMapNode | HierarchyNode<unknown>) {
return node.children === undefined || node.children.length === 0
}

export enum MAP_RESOLUTION_SCALE {
SMALL_MAP = 1,
MEDIUM_MAP = 0.5,
BIG_MAP = 0.25
}

export function getMapResolutionScaleFactor(files: FileState[]) {
const oneMB = 1024 * 1024
const totalFilesSizeKB = getSelectedFilesSize(files)

switch (true) {
Copy link
Contributor

@shaman-apprentice shaman-apprentice Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Didn't know switch(true) is a thing. I would further refactor it to

if (totalFilesSizeKB >= 7 * oneMB) return MAP_RESOLUTION_SCALE.BIG_MAP
if (totalFilesSizeKB >= 2 * oneMB) return MAP_RESOLUTION_SCALE.MEDIUM_MAP
return MAP_RESOLUTION_SCALE.SMALL_MAP

case totalFilesSizeKB >= 7 * oneMB:
return MAP_RESOLUTION_SCALE.BIG_MAP
case totalFilesSizeKB >= 2 * oneMB:
return MAP_RESOLUTION_SCALE.MEDIUM_MAP
default:
return MAP_RESOLUTION_SCALE.SMALL_MAP
}
}
9 changes: 9 additions & 0 deletions visualization/app/codeCharta/util/deltaGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ describe("deltaGenerator", () => {
expect(result.map).toMatchSnapshot()
})

it("should sum the size of the comparison and reference File", () => {
NodeDecorator.decorateMapWithPathAttribute(fileA)
NodeDecorator.decorateMapWithPathAttribute(fileB)

const result = DeltaGenerator.getDeltaFile(fileA, fileB)

expect(result.fileMeta.exportedFileSize).toBe(fileA.fileMeta.exportedFileSize + fileB.fileMeta.exportedFileSize)
})

it("checking delta calculation between two attribute lists", () => {
const a = { a: 100, b: 10, c: 1 }
const b = { a: 110, b: 11, c: 0 }
Expand Down
57 changes: 55 additions & 2 deletions visualization/app/codeCharta/util/fileHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ExportBlacklistType, ExportCCFile } from "../codeCharta.api.model"
import { AttributeTypeValue, BlacklistType, NameDataPair } from "../codeCharta.model"
import { getCCFile } from "./fileHelper"
import { AttributeTypeValue, BlacklistType, CCFile, NameDataPair } from "../codeCharta.model"
import { getCCFile, getSelectedFilesSize } from "./fileHelper"
import { TEST_FILE_CONTENT } from "./dataMocks"
import { clone } from "./clone"
import { FileSelectionState, FileState } from "../model/files/files"
import packageJson from "../../../package.json"

describe("FileHelper", () => {
let fileContent: ExportCCFile
Expand Down Expand Up @@ -58,4 +60,55 @@ describe("FileHelper", () => {
expect(result.settings.fileSettings.attributeTypes).toEqual({ nodes: {}, edges: {} })
})
})

describe("getSelectedFilesSize", () => {
it("should sum up sizes of selected files", () => {
const file1 = {
fileMeta: {
fileName: "file1",
fileChecksum: "invalid-md5-checksum-1",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 10
}
} as CCFile

const file2 = {
fileMeta: {
fileName: "file2",
fileChecksum: "invalid-md5-checksum-2",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 10
}
} as CCFile

const unselectedFile = {
fileMeta: {
fileName: "unselectedFile",
fileChecksum: "invalid-md5-checksum-3",
projectName: "Sample Project",
apiVersion: packageJson.codecharta.apiVersion,
exportedFileSize: 10
}
} as CCFile

const severalFiles: FileState[] = [
{
file: file1,
selectedAs: FileSelectionState.Partial
},
{
file: file2,
selectedAs: FileSelectionState.Comparison
},
{
file: unselectedFile,
selectedAs: FileSelectionState.None
}
]

expect(getSelectedFilesSize(severalFiles)).toBe(20)
})
})
})
12 changes: 12 additions & 0 deletions visualization/app/codeCharta/util/fileHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExportBlacklistType, OldAttributeTypes } from "../codeCharta.api.model"
import { AttributeTypes, BlacklistItem, BlacklistType, CCFile, NameDataPair } from "../codeCharta.model"
import { FileSelectionState, FileState } from "../model/files/files"

export function getCCFile(file: NameDataPair): CCFile {
const fileContent = file.content
Expand Down Expand Up @@ -45,3 +46,14 @@ function potentiallyUpdateBlacklistTypes(blacklist): BlacklistItem[] {
}
return blacklist
}

export function getSelectedFilesSize(files: FileState[]) {
let totalFileSize = 0
for (const file of files) {
if (file.selectedAs !== FileSelectionState.None) {
totalFileSize += file.file.fileMeta.exportedFileSize
}
}

return totalFileSize
}
3 changes: 1 addition & 2 deletions visualization/app/codeCharta/util/treeMapGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { hierarchy, HierarchyNode, HierarchyRectangularNode, treemap } from "d3-hierarchy"
import { TreeMapHelper } from "./treeMapHelper"
import { CodeMapNode, DynamicSettings, Node, NodeMetricData, State } from "../codeCharta.model"
import { isLeaf } from "./codeMapHelper"
import { getMapResolutionScaleFactor } from "../ui/codeMap/codeMap.render.service"
import { getMapResolutionScaleFactor, isLeaf } from "./codeMapHelper"

export type SquarifiedTreeMap = { treeMap: HierarchyRectangularNode<CodeMapNode>; height: number; width: number }

Expand Down
Loading