From b95888d14bafc6e32088d28c6ef78106183b265a Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Fri, 22 Feb 2019 10:36:24 +0100 Subject: [PATCH] Invalidate Comunica caches after update queries Closes RubenVerborgh/LDflex-Comunica#10 --- src/ComunicaUpdateEngine.js | 3 +++ test/ComunicaUpdateEngine-test.js | 6 ++++++ test/__mocks__/ldflex-comunica.js | 3 +++ 3 files changed, 12 insertions(+) diff --git a/src/ComunicaUpdateEngine.js b/src/ComunicaUpdateEngine.js index 7aa5f88..67375d3 100644 --- a/src/ComunicaUpdateEngine.js +++ b/src/ComunicaUpdateEngine.js @@ -33,6 +33,9 @@ export default class ComunicaUpdateEngine extends ComunicaEngine { if (!response.ok) throw new Error(`Update query failed (${response.status}): ${response.statusText}`); + // Invalidate Comunica's internal caches, as they may have changed because of the update + await this._engine.invalidateHttpCache(document); + // Mock Comunica's response for bindings as a Immutable.js object. return { value: { size: 1, values: () => ({ next: () => ({ value: { ok: true } }) }) } }; } diff --git a/test/ComunicaUpdateEngine-test.js b/test/ComunicaUpdateEngine-test.js index d3f7297..e24a9f9 100644 --- a/test/ComunicaUpdateEngine-test.js +++ b/test/ComunicaUpdateEngine-test.js @@ -37,6 +37,12 @@ describe('a ComunicaUpdateEngine instance', () => { expect(args[1]).toHaveProperty('body'); expect(args[1].body).toEqual('INSERT DATA { <> <> <> }'); }); + + it('invalidates the document cache', () => { + expect(engine._engine.invalidateHttpCache).toHaveBeenCalledTimes(1); + const args = engine._engine.invalidateHttpCache.mock.calls[0]; + expect(args[0]).toEqual('https://example.org/'); + }); }); describe('Inserting an invalid query', () => { diff --git a/test/__mocks__/ldflex-comunica.js b/test/__mocks__/ldflex-comunica.js index e456871..dedbdd1 100644 --- a/test/__mocks__/ldflex-comunica.js +++ b/test/__mocks__/ldflex-comunica.js @@ -1,6 +1,9 @@ export default class ComunicaEngineMock { constructor(subject, source) { this._source = source; + this._engine = { + invalidateHttpCache: jest.fn(() => Promise.resolve(true)), + }; } getDocument() {