-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: storage populate method and unit tests
- Loading branch information
Showing
12 changed files
with
213 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { values } from 'lodash'; | ||
|
||
import Realm from 'realm'; | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
import NetworkConfig from '@constants/network'; | ||
|
||
import * as models from '../../models'; | ||
|
||
import { populateDataStore, populateCore, populateNetworks, populateNodes } from '../../models/schemas/populate'; | ||
|
||
describe('Populate', () => { | ||
let instance: Realm; | ||
|
||
beforeAll(async () => { | ||
// get realm instance | ||
instance = new Realm({ schema: values(models), path: './.jest/realmInMemory', inMemory: true }); | ||
}); | ||
|
||
it('should be able to populate networks', () => { | ||
jest.spyOn(instance, 'create'); | ||
|
||
// call the populateNetworks | ||
instance.write(() => { | ||
populateNetworks(instance); | ||
}); | ||
|
||
expect(instance.create).toBeCalledTimes(NetworkConfig.networks.length); | ||
expect(instance.create).toBeCalledWith(models.NetworkModel.schema.name, expect.any(Object)); | ||
}); | ||
|
||
it('should be able to populate nodes', () => { | ||
jest.spyOn(instance, 'create'); | ||
|
||
// call the populateNodes | ||
instance.write(() => { | ||
populateNodes(instance); | ||
}); | ||
|
||
expect(instance.create).toBeCalledWith(models.NodeModel.schema.name, expect.any(Object)); | ||
|
||
// should be able to assign created nodes to the networks nodes and default nodes | ||
const networks = instance.objects<models.NetworkModel>(models.NetworkModel.schema.name); | ||
|
||
// verify we already have network objects | ||
expect(networks.length).toBeGreaterThan(0); | ||
|
||
for (const network of networks) { | ||
expect(network.nodes.length).toBeGreaterThan(0); | ||
expect(network.defaultNode).toBeDefined(); | ||
expect(network.defaultNode).toBeInstanceOf(models.NodeModel); | ||
} | ||
}); | ||
|
||
it('should be able to populate core', () => { | ||
jest.spyOn(instance, 'create'); | ||
|
||
// call the populateCore | ||
instance.write(() => { | ||
populateCore(instance); | ||
}); | ||
|
||
expect(instance.create).toBeCalledWith(models.CoreModel.schema.name, expect.any(Object)); | ||
|
||
const coreObject = instance.objects<models.CoreModel>(models.CoreModel.schema.name)[0]; | ||
|
||
// make sure the core object is created | ||
expect(coreObject).toBeDefined(); | ||
|
||
// verify coreObject network has been defined and also default network from NetworkConfig has been set | ||
expect(coreObject.network).toBeDefined(); | ||
expect(coreObject.network).toBeInstanceOf(models.NetworkModel); | ||
expect(coreObject.network.networkId).toBe(NetworkConfig.defaultNetworkId); | ||
}); | ||
|
||
it('should call functions in correct order', () => { | ||
const populateNetworksMock = jest.spyOn(require('../../models/schemas/populate'), 'populateNetworks'); | ||
const populateNodesMock = jest.spyOn(require('../../models/schemas/populate'), 'populateNodes'); | ||
const populateCoreMock = jest.spyOn(require('../../models/schemas/populate'), 'populateCore'); | ||
|
||
// call populateDataStore | ||
populateDataStore(instance); | ||
|
||
expect( | ||
populateNetworksMock.mock.invocationCallOrder[0] <= populateNodesMock.mock.invocationCallOrder[0] && | ||
populateNodesMock.mock.invocationCallOrder[0] <= populateCoreMock.mock.invocationCallOrder[0], | ||
).toBe(true); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
instance.close(); | ||
Realm.deleteFile({ path: './.jest/realmInMemory' }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Realm from 'realm'; | ||
|
||
import { NetworkModel, NodeModel } from '@store/models/objects'; | ||
|
||
import { NetworkConfig } from '@common/constants'; | ||
|
||
/** | ||
* Populates networks | ||
* | ||
* @param {Realm} realm - The realm to populate networks into. | ||
* @returns {void} | ||
*/ | ||
export const populateNetworks = (realm: Realm): void => { | ||
// default supported networks list | ||
const { networks } = NetworkConfig; | ||
|
||
// create networks | ||
for (let i = 0; i < networks.length; i++) { | ||
realm.create(NetworkModel.schema.name, { | ||
id: new Realm.BSON.ObjectId(), | ||
key: networks[i].key, | ||
networkId: networks[i].networkId, | ||
name: networks[i].name, | ||
nativeAsset: networks[i].nativeAsset, | ||
color: networks[i].color, | ||
type: networks[i].type, | ||
baseReserve: NetworkConfig.baseReserve, | ||
ownerReserve: NetworkConfig.ownerReserve, | ||
amendments: [], | ||
definitionsString: '', | ||
registerAt: new Date(), | ||
updatedAt: new Date(), | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Populates nodes | ||
* | ||
* @param {Realm} realm - The Realm instance to populate nodes for. | ||
* @returns {void} | ||
*/ | ||
export const populateNodes = (realm: Realm): void => { | ||
const networks = realm.objects<NetworkModel>(NetworkModel.schema.name); | ||
|
||
for (let i = 0; i < networks.length; i++) { | ||
const network = networks[i]; | ||
const networkConfig = NetworkConfig.networks.find((net) => net.key === network.key); | ||
const createdNodes: NodeModel[] = []; | ||
|
||
for (let y = 0; y < networkConfig.nodes.length; y++) { | ||
createdNodes.push( | ||
realm.create<NodeModel>(NodeModel.schema.name, { | ||
id: new Realm.BSON.ObjectId(), | ||
endpoint: networkConfig.nodes[y], | ||
registerAt: new Date(), | ||
updatedAt: new Date(), | ||
}), | ||
); | ||
} | ||
|
||
// set the created nodes to the network | ||
// @ts-ignore | ||
network.nodes = createdNodes; | ||
// eslint-disable-next-line prefer-destructuring | ||
network.defaultNode = createdNodes[0]; | ||
} | ||
}; | ||
|
||
/** | ||
* Populates the Core object | ||
* | ||
* @param {Realm} realm - The Realm object to populate. | ||
* @returns {void} | ||
*/ | ||
export const populateCore = (realm: Realm): void => { | ||
// get all networks | ||
const networks = realm.objects<NetworkModel>(NetworkModel.schema.name); | ||
|
||
const { defaultNetworkId } = NetworkConfig; | ||
const selectedNetwork = networks.find((network) => network.networkId === defaultNetworkId); | ||
|
||
realm.create('Core', { | ||
network: selectedNetwork, | ||
}); | ||
}; | ||
|
||
/** | ||
* Populates the given realm with networks, nodes, and core. | ||
* | ||
* @param {Realm} realm - The Realm instance to populate. | ||
*/ | ||
export const populateDataStore = (realm: Realm) => { | ||
[populateNetworks, populateNodes, populateCore].forEach((fn) => { | ||
realm.write(() => { | ||
fn(realm); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.