Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the synchronization of ignored data in data access #207

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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