Skip to content

Commit a13fbfc

Browse files
committed
Improved reliability of tests:
1 parent c4289dd commit a13fbfc

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PostgresPersistenceEngine extends AbstractPersistenceEngine {
6565
`;
6666

6767
const args = [persistenceKey, offset, limit, tags].filter(x => x !== undefined);
68-
const result = this.db.then(db => db.any(query, args).then(results => results.map(PostgresPersistenceEngine.mapDbModelToDomainModel)));
68+
const result = this.db.then(db => db.any(query, args)).then(results => results.map(PostgresPersistenceEngine.mapDbModelToDomainModel));
6969
return new Result(result);
7070
}
7171

lib/schema/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports.create = (tablePrefix = '') => `
1+
module.exports.create = (tablePrefix) => `
22
CREATE TABLE IF NOT EXISTS ${tablePrefix}event_journal (
33
ordering BIGSERIAL NOT NULL PRIMARY KEY,
44
persistence_key VARCHAR(255) NOT NULL,
@@ -19,5 +19,5 @@ module.exports.create = (tablePrefix = '') => `
1919
);
2020
`;
2121

22-
module.exports.destroy = (tablePrefix = '') => `DROP TABLE IF EXISTS ${tablePrefix}event_journal CASCADE;
22+
module.exports.destroy = (tablePrefix) => `DROP TABLE IF EXISTS ${tablePrefix}event_journal CASCADE;
2323
DROP TABLE IF EXISTS ${tablePrefix}snapshot_store CASCADE;`;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "nact-persistence-postgres",
3-
"version": "6.0.1",
3+
"version": "6.0.2",
44
"description": "nact-persistence-postgres ⇒ nact + postresql = your services have never been so persistent",
55
"main": "lib/index.js",
66
"scripts": {
77
"test": "yarn run lint && nyc mocha ./test/*.js",
88
"interactive-cover": "nyc --reporter=html mocha ./test/*.js && open ./coverage/index.html",
99
"lint": "semistandard",
10+
"lint-fix": "semistandard --fix",
1011
"coverage": "nyc report --reporter=text-lcov | coveralls",
1112
"prepush": "yarn run test"
1213
},
@@ -48,4 +49,4 @@
4849
"peerDependencies": {
4950
"nact": "^6.0.0"
5051
}
51-
}
52+
}

test/postgres-persistence.js

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,69 +31,69 @@ const connectionString = 'postgres://postgres:testpassword@localhost:5431/testdb
3131
describe('PostgresPersistenceEngine', function () {
3232
const db = pgp(connectionString);
3333

34-
afterEach(() => {
35-
db.query(destroy());
36-
});
34+
describe('table creation', () => {
35+
afterEach(async () => {
36+
await db.query(destroy(''));
37+
});
3738

38-
it('should not create database if createIfNotExists is set to false', async function () {
39-
new PostgresPersistenceEngine(connectionString, { createIfNotExists: false });
40-
await delay(300);
41-
const query = `
39+
it('should not create database if createIfNotExists is set to false', async function () {
40+
new PostgresPersistenceEngine(connectionString, { createIfNotExists: false });
41+
await delay(300);
42+
const query = `
4243
SELECT table_schema,table_name
4344
FROM information_schema.tables
4445
WHERE table_name = 'event_journal';`;
45-
await db.none(query);
46-
});
46+
await db.none(query);
47+
});
4748

48-
it('should not be able to create databases with prefixes', async function () {
49-
new PostgresPersistenceEngine(connectionString, { tablePrefix: 'test_prefix_' });
50-
await delay(300);
51-
const query = `
49+
it('should not be able to create databases with prefixes', async function () {
50+
new PostgresPersistenceEngine(connectionString, { tablePrefix: 'test_prefix_' });
51+
await delay(300);
52+
const query = `
5253
SELECT table_schema,table_name
5354
FROM information_schema.tables
5455
WHERE table_name = 'test_prefix_event_journal';`;
55-
56-
await db.one(query);
57-
await db.query(destroy('test_prefix_'));
56+
await db.one(query);
57+
await db.query(destroy('test_prefix_'));
58+
});
5859
});
5960

6061
describe('#persist', function () {
61-
afterEach(() => {
62-
db.query(destroy());
62+
afterEach(async () => {
63+
await db.query(destroy(''));
6364
});
64-
65+
const date = new Date().getTime();
6566
it('should store values in database', async function () {
6667
const engine = new PostgresPersistenceEngine(connectionString);
67-
await retry(async () => {
68-
const event1 = new PersistedEvent({ message: 'hello' }, 1, 'test', ['a', 'b', 'c']);
69-
const event2 = new PersistedEvent(['message', 'goodbye'], 2, 'test');
70-
const event3 = new PersistedEvent({ message: 'hello' }, 1, 'test2');
71-
await engine.persist(event1);
72-
await engine.persist(event2);
73-
await engine.persist(event3);
7468

75-
const result =
69+
const event1 = new PersistedEvent({ message: 'hello' }, 1, 'test', ['a', 'b', 'c'], date);
70+
const event2 = new PersistedEvent(['message', 'goodbye'], 2, 'test', undefined, date);
71+
const event3 = new PersistedEvent({ message: 'hello' }, 1, 'test2', undefined, date);
72+
await engine.persist(event1);
73+
await engine.persist(event2);
74+
await engine.persist(event3);
75+
76+
const result =
7677
(await db.many('SELECT * FROM event_journal WHERE persistence_key = \'test\' ORDER BY sequence_nr'))
7778
.map(PostgresPersistenceEngine.mapDbModelToDomainModel);
7879

79-
result.should.be.lengthOf(2).and.deep.equal([event1, event2]);
80-
const result2 = await db.one('SELECT * FROM event_journal WHERE persistence_key = \'test2\'');
81-
PostgresPersistenceEngine.mapDbModelToDomainModel(result2).should.deep.equal(event3);
82-
}, 7, 50);
80+
result.should.be.lengthOf(2).and.deep.equal([event1, event2]);
81+
const result2 = await db.one('SELECT * FROM event_journal WHERE persistence_key = \'test2\'');
82+
PostgresPersistenceEngine.mapDbModelToDomainModel(result2).should.deep.equal(event3);
8383
});
8484
});
8585

8686
describe('#takeSnapshot', function () {
87-
afterEach(() => {
88-
db.query(destroy());
87+
afterEach(async () => {
88+
await db.query(destroy(''));
8989
});
90-
90+
const date = new Date().getTime();
9191
it('should store values in database', async function () {
9292
const engine = new PostgresPersistenceEngine(connectionString);
9393
await retry(async () => {
94-
const snapshot1 = new PersistedSnapshot({ message: 'hello' }, 1, 'test');
95-
const snapshot2 = new PersistedSnapshot({ message: 'goodbye' }, 2, 'test');
96-
const snapshot3 = new PersistedSnapshot({ message: 'hello' }, 1, 'test2');
94+
const snapshot1 = new PersistedSnapshot({ message: 'hello' }, 1, 'test', date);
95+
const snapshot2 = new PersistedSnapshot({ message: 'goodbye' }, 2, 'test', date);
96+
const snapshot3 = new PersistedSnapshot({ message: 'hello' }, 1, 'test2', date);
9797
await engine.takeSnapshot(snapshot1);
9898
await engine.takeSnapshot(snapshot2);
9999
await engine.takeSnapshot(snapshot3);
@@ -110,21 +110,20 @@ describe('PostgresPersistenceEngine', function () {
110110
});
111111

112112
describe('#latestSnapshot', function () {
113-
const snapshot1 = new PersistedSnapshot({ message: 'hello' }, 1, 'test3');
114-
const snapshot2 = new PersistedSnapshot({ message: 'goodbye' }, 2, 'test3');
115-
const snapshot3 = new PersistedSnapshot({ message: 'hello again' }, 3, 'test3');
113+
const date = new Date().getTime();
114+
const snapshot1 = new PersistedSnapshot({ message: 'hello' }, 1, 'test3', date);
115+
const snapshot2 = new PersistedSnapshot({ message: 'goodbye' }, 2, 'test3', date);
116+
const snapshot3 = new PersistedSnapshot({ message: 'hello again' }, 3, 'test3', date);
116117
let engine;
117118

118119
beforeEach(async () => {
119120
engine = new PostgresPersistenceEngine(connectionString);
120-
await retry(async () => {
121-
await engine.takeSnapshot(snapshot1);
122-
await engine.takeSnapshot(snapshot2);
123-
await engine.takeSnapshot(snapshot3);
124-
}, 7, 50);
121+
await engine.takeSnapshot(snapshot1);
122+
await engine.takeSnapshot(snapshot2);
123+
await engine.takeSnapshot(snapshot3);
125124
});
126-
afterEach(() => {
127-
db.query(destroy());
125+
afterEach(async () => {
126+
await db.query(destroy(''));
128127
});
129128

130129
it('should be able to retrieve latest snapshot', async function () {
@@ -139,21 +138,20 @@ describe('PostgresPersistenceEngine', function () {
139138
});
140139

141140
describe('#events', async function () {
142-
const event1 = new PersistedEvent({ message: 'hello' }, 1, 'test3', ['a', 'b', 'c']);
143-
const event2 = new PersistedEvent({ message: 'goodbye' }, 2, 'test3', ['a']);
144-
const event3 = new PersistedEvent({ message: 'hello again' }, 3, 'test3', ['b', 'c']);
141+
const date = new Date().getTime();
142+
const event1 = new PersistedEvent({ message: 'hello' }, 1, 'test3', ['a', 'b', 'c'], date);
143+
const event2 = new PersistedEvent({ message: 'goodbye' }, 2, 'test3', ['a'], date);
144+
const event3 = new PersistedEvent({ message: 'hello again' }, 3, 'test3', ['b', 'c'], date);
145145
let engine;
146146

147147
beforeEach(async () => {
148148
engine = new PostgresPersistenceEngine(connectionString);
149-
await retry(async () => {
150-
await engine.persist(event1);
151-
await engine.persist(event2);
152-
await engine.persist(event3);
153-
}, 7, 50);
149+
await engine.persist(event1);
150+
await engine.persist(event2);
151+
await engine.persist(event3);
154152
});
155-
afterEach(() => {
156-
db.query(destroy());
153+
afterEach(async () => {
154+
await db.query(destroy(''));
157155
});
158156

159157
it('should be able to retrieve previously persisted events', async function () {

0 commit comments

Comments
 (0)