Skip to content

Commit 4f4127c

Browse files
author
Greg S
committed
WIP #55: added tests for NamespaceHttp.getLinkedX and Alias/AddressAlias/MosaicAlias/EmptyAlias classes
1 parent c503cd7 commit 4f4127c

File tree

6 files changed

+130
-14
lines changed

6 files changed

+130
-14
lines changed

e2e/infrastructure/NamespaceHttp.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,24 @@ describe('NamespaceHttp', () => {
6969
});
7070
});
7171
});
72+
73+
describe('getLinkedMosaicId', () => {
74+
it('should return mosaicId given namespaceId', (done) => {
75+
namespaceHttp.getLinkedMosaicId(namespaceId)
76+
.subscribe((mosaicId) => {
77+
expect(mosaicId).to.be.null;
78+
done();
79+
});
80+
});
81+
});
82+
83+
describe('getLinkedAddress', () => {
84+
it('should return address given namespaceId', (done) => {
85+
namespaceHttp.getLinkedAddress(namespaceId)
86+
.subscribe((address) => {
87+
expect(address).to.be.null;
88+
done();
89+
});
90+
});
91+
});
7292
});

src/infrastructure/NamespaceHttp.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
167167

168168
/**
169169
* Gets the MosaicId from a MosaicAlias
170-
* @param alias - String containing the address alias
170+
* @param namespaceId - the namespaceId of the namespace
171171
* @returns Observable<MosaicId | null>
172172
*/
173-
public getLinkedMosaicId(alias: string): Observable<MosaicId | null> {
173+
public getLinkedMosaicId(namespaceId: NamespaceId): Observable<MosaicId | null> {
174174
return this.getNetworkTypeObservable().pipe(
175175
mergeMap((networkType) => observableFrom(
176-
this.namespaceRoutesApi.getNamespace(alias)).pipe(
176+
this.namespaceRoutesApi.getNamespace(namespaceId.toHex())).pipe(
177177
map((namespaceInfoDTO) => {
178178

179179
if (namespaceInfoDTO.namespace.alias.type === AliasType.Mosaic) {
@@ -186,13 +186,13 @@ export class NamespaceHttp extends Http implements NamespaceRepository {
186186

187187
/**
188188
* Gets the Address from a AddressAlias
189-
* @param alias - String containing the address alias
189+
* @param namespaceId - the namespaceId of the namespace
190190
* @returns Observable<Address>
191191
*/
192-
public getLinkedAddress(alias: string): Observable<Address | null> {
192+
public getLinkedAddress(namespaceId: NamespaceId): Observable<Address | null> {
193193
return this.getNetworkTypeObservable().pipe(
194194
mergeMap((networkType) => observableFrom(
195-
this.namespaceRoutesApi.getNamespace(alias)).pipe(
195+
this.namespaceRoutesApi.getNamespace(namespaceId.toHex())).pipe(
196196
map((namespaceInfoDTO) => {
197197

198198
if (namespaceInfoDTO.namespace.alias.type === AliasType.Address) {

src/infrastructure/NamespaceRepository.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ export interface NamespaceRepository {
6464

6565
/**
6666
* Gets the MosaicId from a MosaicAlias
67-
* @param alias - String containing the address alias
67+
* @param namespaceId - the namespaceId of the namespace
6868
* @returns Observable<MosaicId | null>
6969
*/
70-
getLinkedMosaicId(alias: string): Observable<MosaicId | null>;
70+
getLinkedMosaicId(namespaceId: NamespaceId): Observable<MosaicId | null>;
7171

7272
/**
7373
* Gets the Address from a AddressAlias
74-
* @param alias - String containing the address alias
74+
* @param namespaceId - the namespaceId of the namespace
7575
* @returnsObservable<Address | null>
7676
*/
77-
getLinkedAddress(alias: string): Observable<Address | null>;
77+
getLinkedAddress(namespaceId: NamespaceId): Observable<Address | null>;
7878
}

test/model/namespace/Alias.spec.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2018 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {deepEqual} from 'assert';
18+
import {expect} from 'chai';
19+
import {Address} from '../../../src/model/account/Address';
20+
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
21+
import {MosaicId} from '../../../src/model/mosaic/MosaicId';
22+
import {AddressAlias} from '../../../src/model/namespace/AddressAlias';
23+
import {Alias} from '../../../src/model/namespace/Alias';
24+
import {AliasType} from '../../../src/model/namespace/AliasType';
25+
import {EmptyAlias} from '../../../src/model/namespace/EmptyAlias';
26+
import {MosaicAlias} from '../../../src/model/namespace/MosaicAlias';
27+
import {UInt64} from '../../../src/model/UInt64';
28+
29+
describe('Alias', () => {
30+
let emptyAliasDTO;
31+
let addressAliasDTO;
32+
let mosaicAliasDTO;
33+
let address;
34+
let address2;
35+
36+
before(() => {
37+
address = Address.createFromRawAddress('SCTVW23D2MN5VE4AQ4TZIDZENGNOZXPRPRLIKCF2');
38+
address2 = Address.createFromRawAddress('SARNASAS2BIAB6LMFA3FPMGBPGIJGK6IJETM3ZSP');
39+
emptyAliasDTO = {
40+
type: 0,
41+
};
42+
mosaicAliasDTO = {
43+
type: AliasType.Mosaic,
44+
mosaicId: new MosaicId([481110499, 231112638]),
45+
};
46+
addressAliasDTO = {
47+
type: AliasType.Address,
48+
address,
49+
};
50+
});
51+
52+
it('should create an EmptyAlias object', () => {
53+
const alias = new EmptyAlias();
54+
expect(alias.type).to.be.equal(AliasType.None);
55+
});
56+
57+
it('should create a AddressAlias object', () => {
58+
const alias = new AddressAlias(addressAliasDTO.type, addressAliasDTO.address);
59+
expect(alias.type).to.be.equal(AliasType.Address);
60+
expect(alias.address).to.be.equal(addressAliasDTO.address);
61+
});
62+
63+
it('should create a MosaicAlias object', () => {
64+
const alias = new MosaicAlias(mosaicAliasDTO.type, mosaicAliasDTO.mosaicId);
65+
expect(alias.type).to.be.equal(AliasType.Mosaic);
66+
expect(alias.mosaicId).to.be.equal(mosaicAliasDTO.mosaicId);
67+
});
68+
69+
it('should compare addresses in AddressAlias.equals()', () => {
70+
const alias1 = new AddressAlias(addressAliasDTO.type, addressAliasDTO.address);
71+
const alias2 = new AddressAlias(addressAliasDTO.type, addressAliasDTO.address);
72+
const alias3 = new AddressAlias(addressAliasDTO.type, address2);
73+
74+
expect(alias1.equals(alias2)).to.be.equal(true);
75+
expect(alias1.equals(alias3)).to.be.equal(false);
76+
});
77+
78+
it('should compare mosaicIds in MosaicAlias.equals()', () => {
79+
const alias1 = new MosaicAlias(mosaicAliasDTO.type, mosaicAliasDTO.mosaicId);
80+
const alias2 = new MosaicAlias(mosaicAliasDTO.type, mosaicAliasDTO.mosaicId);
81+
const alias3 = new MosaicAlias(mosaicAliasDTO.type, new MosaicId([481110498, 231112637]));
82+
83+
expect(alias1.equals(alias2)).to.be.equal(true);
84+
expect(alias1.equals(alias3)).to.be.equal(false);
85+
});
86+
});

test/model/namespace/NamespaceInfo.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {deepEqual} from 'assert';
1818
import {expect} from 'chai';
1919
import {PublicAccount} from '../../../src/model/account/PublicAccount';
2020
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
21+
import {MosaicId} from '../../../src/model/mosaic/MosaicId';
2122
import {NamespaceId} from '../../../src/model/namespace/NamespaceId';
2223
import {NamespaceInfo} from '../../../src/model/namespace/NamespaceInfo';
2324
import {UInt64} from '../../../src/model/UInt64';
@@ -41,7 +42,7 @@ describe('NamespaceInfo', () => {
4142
parentId: new NamespaceId([0, 0]),
4243
startHeight: new UInt64([1, 0]),
4344
type: 0,
44-
alias: {type: 0},
45+
alias: {type: 1, mosaicId: new MosaicId([481110499, 231112638])},
4546
},
4647
};
4748
subNamespaceDTO = {
@@ -90,7 +91,8 @@ describe('NamespaceInfo', () => {
9091
expect(namespaceInfo.owner.publicKey).to.be.equal(rootNamespaceDTO.namespace.owner);
9192
deepEqual(namespaceInfo.startHeight, rootNamespaceDTO.namespace.startHeight);
9293
deepEqual(namespaceInfo.endHeight, rootNamespaceDTO.namespace.endHeight);
93-
deepEqual(namespaceInfo.alias, rootNamespaceDTO.namespace.alias);
94+
expect(namespaceInfo.alias.type).to.be.equal(rootNamespaceDTO.namespace.alias.type);
95+
expect(namespaceInfo.alias.mosaicId).to.be.equal(rootNamespaceDTO.namespace.alias.mosaicId);
9496
});
9597

9698
it('should return the NamespaceId in string format', () => {
@@ -146,6 +148,16 @@ describe('NamespaceInfo', () => {
146148
expect(namespaceInfo.id).to.be.equal(subNamespaceDTO.namespace.level1);
147149
});
148150

151+
it('hasAlias() should return false when the Namespace alias has type 1', () => {
152+
const namespaceInfo = createRootFromDTO(rootNamespaceDTO);
153+
expect(namespaceInfo.hasAlias()).to.be.equal(true);
154+
});
155+
156+
it('hasAlias() should return false when the Namespace alias has type 0', () => {
157+
const namespaceInfo = createSubnamespaceFromDTO(subNamespaceDTO);
158+
expect(namespaceInfo.hasAlias()).to.be.equal(false);
159+
});
160+
149161
// region functions
150162
function createRootFromDTO(dto): NamespaceInfo {
151163
return new NamespaceInfo(

test/model/transaction/AddressAliasTransaction.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ describe('AddressAliasTransaction', () => {
5151

5252
const signedTransaction = addressAliasTransaction.signWith(account);
5353

54-
console.log(signedTransaction.payload);
55-
5654
expect(signedTransaction.payload.substring(
5755
240,
5856
signedTransaction.payload.length,

0 commit comments

Comments
 (0)