Skip to content

Commit

Permalink
fix: storage populate method and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
N3TC4T committed Dec 14, 2023
1 parent 864805b commit b4e2d0d
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 89 deletions.
98 changes: 98 additions & 0 deletions src/store/__tests__/integration/populate.test.ts
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' });
});
});
2 changes: 1 addition & 1 deletion src/store/__tests__/integration/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Storage', () => {
const sorted = sortBy(require('../../models/schemas').default, [(v) => v.schemaVersion]);
const latest = sorted.slice(-1)[0];

const populateSpy = jest.spyOn(latest, 'populate');
const populateSpy = jest.spyOn(require('../../models/schemas/populate'), 'populateDataStore');

// initialize the storage
await storage.initialize();
Expand Down
2 changes: 1 addition & 1 deletion src/store/__tests__/repositories/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { values } from 'lodash';
import NetworksConfig from '@constants/network';

import * as models from '../../models';
import { populate as populateDataStore } from '../../models/schemas/latest';
import { populateDataStore } from '../../models/schemas/populate';

import { NetworkRepository } from '../../repositories';
import { NetworkRailsChangesType, NetworkType } from '../../types';
Expand Down
2 changes: 0 additions & 2 deletions src/store/models/schemas/latest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { schemas } from './v15';

export { migration, populate } from './v15';

export const {
CoreSchema,
AccountSchema,
Expand Down
99 changes: 99 additions & 0 deletions src/store/models/schemas/populate.ts
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);
});
});
};
12 changes: 0 additions & 12 deletions src/store/models/schemas/v14/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ const CoreSchema = {
}
}
},

populate: (realm: Realm) => {
// get all networks
const networks = realm.objects('Network') as any;

const { defaultNetworkId } = NetworkConfig;
const selectedNetwork = networks.find((n: any) => n.id === defaultNetworkId);

realm.create('Core', {
network: selectedNetwork,
});
},
};

export default <ExtendedSchemaType>CoreSchema;
7 changes: 0 additions & 7 deletions src/store/models/schemas/v14/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ export const migration = (oldRealm: Realm, newRealm: Realm) => {
AccountSchema.migration(oldRealm, newRealm);
TrustLineSchema.migration(oldRealm, newRealm);
};
export const populate = (realm: Realm) => {
[NetworkSchema, NodeSchema, CoreSchema].forEach((schema) => {
realm.write(() => {
schema.populate(realm);
});
});
};
export const schemas = {
ContactSchema,
CounterPartySchema,
Expand Down
19 changes: 5 additions & 14 deletions src/store/models/schemas/v14/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ const NetworkSchema = {
},
},

/*
Populate networks to the data store
Note: this is necessary in the process of migration and also fresh install
*/
populate: (realm: Realm) => {
migration: (oldRealm: Realm, newRealm: Realm) => {
/* eslint-disable-next-line */
console.log('migrating Network schema to 14');

// default supported networks list
const { networks } = NetworkConfig;

// create networks
for (let i = 0; i < networks.length; i++) {
realm.create(NetworkSchema.schema.name, {
newRealm.create(NetworkSchema.schema.name, {
id: networks[i].networkId,
key: networks[i].key,
name: networks[i].name,
Expand All @@ -56,14 +55,6 @@ const NetworkSchema = {
});
}
},

migration: (oldRealm: Realm, newRealm: Realm) => {
/* eslint-disable-next-line */
console.log('migrating Network schema to 14');

// populate networks
NetworkSchema.populate(newRealm);
},
};

export default <ExtendedSchemaType>NetworkSchema;
18 changes: 6 additions & 12 deletions src/store/models/schemas/v14/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ const NodeSchema = {
},
},

populate: (realm: Realm) => {
const networks = realm.objects('Network') as any;
migration: (oldRealm: Realm, newRealm: Realm) => {
/* eslint-disable-next-line */
console.log('migrating Node schema to v14');

const networks = newRealm.objects('Network') as any;

for (let i = 0; i < networks.length; i++) {
const network = networks[i];
Expand All @@ -31,8 +34,7 @@ const NodeSchema = {

for (let y = 0; y < networkConfig.nodes.length; y++) {
createdNodes.push(
// @ts-ignore
realm.create(NodeSchema.schema.name, {
newRealm.create(NodeSchema.schema.name, {
id: new Realm.BSON.ObjectId(),
endpoint: networkConfig.nodes[y],
registerAt: new Date(),
Expand All @@ -47,14 +49,6 @@ const NodeSchema = {
network.defaultNode = createdNodes[0];
}
},

migration: (oldRealm: Realm, newRealm: Realm) => {
/* eslint-disable-next-line */
console.log('migrating Node schema to v14');

// populate nodes
NodeSchema.populate(newRealm);
},
};

export default <ExtendedSchemaType>NodeSchema;
8 changes: 0 additions & 8 deletions src/store/models/schemas/v15/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,8 @@ const {
/* Exports ==================================================================== */
export const schemaVersion = 15;
export const migration = (oldRealm: Realm, newRealm: Realm) => {
// Note: The order is important for this schema version
NetworkSchema.migration(oldRealm, newRealm);
};
export const populate = (realm: Realm) => {
[NetworkSchema, NodeSchema, CoreSchema].forEach((schema) => {
realm.write(() => {
schema.populate(realm);
});
});
};
export const schemas = {
ContactSchema,
CounterPartySchema,
Expand Down
28 changes: 0 additions & 28 deletions src/store/models/schemas/v15/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,6 @@ const NetworkSchema = {
},
},

/*
Populate networks to the data store
Note: this is necessary in the process of migration and also fresh install
*/
populate: (realm: Realm) => {
// default supported networks list
const { networks } = NetworkConfig;

// create networks
for (let i = 0; i < networks.length; i++) {
realm.create(NetworkSchema.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(),
});
}
},

migration: (oldRealm: Realm, newRealm: Realm) => {
/* eslint-disable-next-line */
console.log('migrating Network schema to 15');
Expand Down
Loading

0 comments on commit b4e2d0d

Please sign in to comment.