Skip to content

Commit

Permalink
feat: add the synchronization of ignored data in data access (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrolland committed Apr 27, 2020
1 parent 3cbc744 commit d77194b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/data-access/src/data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,12 @@ export default class DataAccess implements DataAccessTypes.IDataAccess {
to: synchronizationTo,
});

// Try to get some data previously ignored
const oldEntriesWithMeta = await this.storage.getIgnoredData();

// check if the data returned by getNewDataId are correct
// if yes, the dataIds are indexed with LocationByTopic
await this.pushLocationsWithTopics(newDataWithMeta.entries);
await this.pushLocationsWithTopics(newDataWithMeta.entries.concat(oldEntriesWithMeta));

// The last synced timestamp is the latest one returned by storage
this.lastSyncStorageTimestamp = newDataWithMeta.lastTimestamp;
Expand Down
60 changes: 48 additions & 12 deletions packages/data-access/test/data-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ const transactionDataMock2String = JSON.stringify({
attribut1: 'foo',
attribut2: 'bar',
});
const transactionDataMock3String = JSON.stringify({
attribut1: 'jean',
attribut2: 'bon',
});

const transactionMock1: DataAccessTypes.ITransaction = {
data: transactionDataMock1String,
};
const transactionMock2: DataAccessTypes.ITransaction = {
data: transactionDataMock2String,
};
const transactionMock3: DataAccessTypes.ITransaction = {
data: transactionDataMock3String,
};

const arbitraryId1 = '011111111111111111111111111111111111111111111111111111111111111111';
const arbitraryId2 = '012222222222222222222222222222222222222222222222222222222222222222';
Expand All @@ -63,6 +70,13 @@ const blockWith2tx = RequestDataAccessBlock.pushTransaction(
[arbitraryTopic2],
);

const blockWith1txBis = RequestDataAccessBlock.pushTransaction(
emptyblock,
transactionMock3,
arbitraryId1,
[arbitraryTopic1],
);

const dataIdBlock2tx = 'dataIdBlock2tx';

const getDataResult: StorageTypes.IEntriesWithLastTimestamp = {
Expand All @@ -76,6 +90,15 @@ const getDataResult: StorageTypes.IEntriesWithLastTimestamp = {
lastTimestamp: 0,
};

const dataIdBlock1txBis = 'dataIdBlock1txBis';
const getIgnoredDataResult: StorageTypes.IEntry[] = [
{
content: JSON.stringify(blockWith1txBis),
id: dataIdBlock1txBis,
meta: { state: StorageTypes.ContentState.CONFIRMED, timestamp: 1 },
},
];

const appendResult: any = {
content: '',
id: dataIdBlock2tx,
Expand Down Expand Up @@ -646,19 +669,16 @@ describe('data-access', () => {
});

it('allows to get new transactions after synchronizeNewDataId() call', async () => {
const testData: Promise<StorageTypes.IEntriesWithLastTimestamp> = Promise.resolve(
getDataResult,
);

// We create a fakeStorage where getData() called at initialization returns empty structure
const fakeStorage = {
...defaultFakeStorage,
getData: (options: any): Promise<StorageTypes.IEntriesWithLastTimestamp> => {
getData: async (options: any): Promise<StorageTypes.IEntriesWithLastTimestamp> => {
if (!options) {
return Promise.resolve(emptyDataResult);
}
return testData;
return getDataResult;
},
getIgnoredData: async (): Promise<StorageTypes.IEntry[]> => getIgnoredDataResult,
read: (param: string): any => {
const dataIdBlock2txFake: StorageTypes.IEntry = {
content: JSON.stringify(blockWith2tx),
Expand All @@ -667,6 +687,7 @@ describe('data-access', () => {
};
const result: any = {
dataIdBlock2tx: dataIdBlock2txFake,
dataIdBlock1txBis: getIgnoredDataResult[0],
};
return result[param];
},
Expand All @@ -686,7 +707,7 @@ describe('data-access', () => {
result: { transactions: {} },
});

// Transactions should be available avec synchronization
// Transactions should be available after synchronization
await expect(dataAccess.synchronizeNewDataIds()).to.be.fulfilled;

expect(
Expand All @@ -695,9 +716,12 @@ describe('data-access', () => {
).to.deep.equal({
meta: {
storageMeta: {
[arbitraryId1]: [{ state: StorageTypes.ContentState.CONFIRMED, timestamp: 1 }],
[arbitraryId1]: [
{ state: StorageTypes.ContentState.CONFIRMED, timestamp: 1 },
{ state: StorageTypes.ContentState.CONFIRMED, timestamp: 1 },
],
},
transactionsStorageLocation: { [arbitraryId1]: [dataIdBlock2tx] },
transactionsStorageLocation: { [arbitraryId1]: [dataIdBlock2tx, dataIdBlock1txBis] },
},
result: {
transactions: {
Expand All @@ -707,22 +731,29 @@ describe('data-access', () => {
transaction: transactionMock1,
timestamp: 1,
},
{
state: DataAccessTypes.TransactionState.CONFIRMED,
transaction: transactionMock3,
timestamp: 1,
},
],
},
},
});

expect(
await dataAccess.getChannelsByTopic(arbitraryTopic2),
'result with arbitraryTopic2 wrong',
).to.deep.equal({
meta: {
storageMeta: {
[arbitraryId1]: [{ state: DataAccessTypes.TransactionState.CONFIRMED, timestamp: 1 }],
[arbitraryId1]: [
{ state: DataAccessTypes.TransactionState.CONFIRMED, timestamp: 1 },
{ state: DataAccessTypes.TransactionState.CONFIRMED, timestamp: 1 },
],
[arbitraryId2]: [{ state: DataAccessTypes.TransactionState.CONFIRMED, timestamp: 1 }],
},
transactionsStorageLocation: {
[arbitraryId1]: [dataIdBlock2tx],
[arbitraryId1]: [dataIdBlock2tx, dataIdBlock1txBis],
[arbitraryId2]: [dataIdBlock2tx],
},
},
Expand All @@ -734,6 +765,11 @@ describe('data-access', () => {
transaction: transactionMock1,
timestamp: 1,
},
{
state: DataAccessTypes.TransactionState.CONFIRMED,
transaction: transactionMock3,
timestamp: 1,
},
],
[arbitraryId2]: [
{
Expand Down

0 comments on commit d77194b

Please sign in to comment.