Skip to content

Commit

Permalink
Add task related files
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuso Valkeejärvi committed Jan 21, 2022
1 parent a404e20 commit 199453f
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# HierarchyTask

Implement functions `findMeterWithId` and `getHierarchyWithoutMeter` in file `src/app/services/meter-hierarchy-helpers.ts`.
Don't modify any other files.

Functions are considered implemented when tests pass. Tests can be run with command `ng test`.
Try to make solution clean and efficient.

Required software:
- nodejs v16 or newer
- npm
- angular CLI (can be installed with command `npm -g i @angular/cli`)

-- TEXT BELOW IS AUTOMATICALLY GENERATED BY ANGULAR CLI

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.1.2.

## Development server
Expand Down
6 changes: 6 additions & 0 deletions src/app/meter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Meter {
id: number;
facilityId: number;
name: string;
subMeters?: Meter[];
}
141 changes: 141 additions & 0 deletions src/app/services/meter-hiearchy-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import * as helpers from './meter-hiearchy-helpers';
import { Meter } from '../meter';

describe('MeterHierarchyHelpers', () => {
let hierarchy: Meter[];

beforeEach(() => {
hierarchy = helpers.getExampleHierarchy();
});

describe('#findMeterWithId', () => {
it('should find meter from top level', () => {
expect(helpers.findMeterWithId(hierarchy, 9)).toEqual({
id: 9, facilityId: 101, name: 'm 8'
});
});

it('should find meter recursively', () => {
expect(helpers.findMeterWithId(hierarchy, 46)).toEqual({
id: 46, name: 'meter46', facilityId: 88
});
});

it('should return null when meter is not found', () => {
expect(helpers.findMeterWithId(hierarchy, 987)).toBeNull();
});
});

describe('#getHierarchyWithoutMeter', () => {
it('should remove meter without submeters', () => {
const expectedHierarchy: Meter[] = [
{
id: 5,
facilityId: 4,
name: 'mittari 5',
subMeters: [
{ id: 12, name: 'Meter 12', facilityId: 4 },
{ id: 13, name: 'Meter 13', facilityId: 4 }
]
},
{
id: 11,
facilityId: 88,
name: 'meter11',
subMeters: [
{
id: 33,
name: 'M33',
facilityId: 88,
subMeters: [
{ id: 45, name: 'meter45', facilityId: 88 },
{ id: 46, name: 'meter46', facilityId: 88 },
{ id: 48, name: 'meter48', facilityId: 88 }
]
}
]
},
{
id: 55,
name: 'Mittari 55',
facilityId: 400
}
];

expect(helpers.getHierarchyWithoutMeter(hierarchy, 9)).toEqual(expectedHierarchy);
});

it('should remove meter and its submeters', () => {
const expectedHierarchy: Meter[] = [
{
id: 5,
facilityId: 4,
name: 'mittari 5',
subMeters: [
{ id: 12, name: 'Meter 12', facilityId: 4 },
{ id: 13, name: 'Meter 13', facilityId: 4 }
]
},
{
id: 9,
facilityId: 101,
name: 'm 8'
},
{
id: 55,
name: 'Mittari 55',
facilityId: 400
}
];

expect(helpers.getHierarchyWithoutMeter(hierarchy, 11)).toEqual(expectedHierarchy);
});

it('should remove submeter only', () => {
const expectedHierarchy: Meter[] = [
{
id: 5,
facilityId: 4,
name: 'mittari 5',
subMeters: [
{ id: 13, name: 'Meter 13', facilityId: 4 }
]
},
{
id: 9,
facilityId: 101,
name: 'm 8'
},
{
id: 11,
facilityId: 88,
name: 'meter11',
subMeters: [
{
id: 33,
name: 'M33',
facilityId: 88,
subMeters: [
{ id: 45, name: 'meter45', facilityId: 88 },
{ id: 46, name: 'meter46', facilityId: 88 },
{ id: 48, name: 'meter48', facilityId: 88 }
]
}
]
},
{
id: 55,
name: 'Mittari 55',
facilityId: 400
}
];

expect(helpers.getHierarchyWithoutMeter(hierarchy, 12)).toEqual(expectedHierarchy);
});
});

afterEach(() => {
// Check that operations did not have side effects
expect(hierarchy).toEqual(helpers.getExampleHierarchy());
});
});
60 changes: 60 additions & 0 deletions src/app/services/meter-hiearchy-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Meter } from '../meter';

/**
* Find meter with given id from hierarchy
*
* @returns meter object or null if meter with given id is not found
*/
export function findMeterWithId(hierarchy: Meter[], id: number): Meter | null {
throw Error('Implement this');
}

/**
* Get new hierarchy from which meter with given id (and possible submeters) is deleted
*
* @returns hierarchy without meter with given id
*/
export function getHierarchyWithoutMeter(hierarchy: Meter[], id: number): Meter[] {
throw Error('Implement this');
}

export function getExampleHierarchy(): Meter[] {
return [
{
id: 5,
facilityId: 4,
name: 'mittari 5',
subMeters: [
{ id: 12, name: 'Meter 12', facilityId: 4 },
{ id: 13, name: 'Meter 13', facilityId: 4 }
]
},
{
id: 9,
facilityId: 101,
name: 'm 8'
},
{
id: 11,
facilityId: 88,
name: 'meter11',
subMeters: [
{
id: 33,
name: 'M33',
facilityId: 88,
subMeters: [
{ id: 45, name: 'meter45', facilityId: 88 },
{ id: 46, name: 'meter46', facilityId: 88 },
{ id: 48, name: 'meter48', facilityId: 88 }
]
}
]
},
{
id: 55,
name: 'Mittari 55',
facilityId: 400
}
];
}

0 comments on commit 199453f

Please sign in to comment.