UNPKG

wallet-storage

Version:

BRC100 conforming wallet, wallet storage and wallet signer components

248 lines 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const TestUtilsWalletStorage_1 = require("../utils/TestUtilsWalletStorage"); const index_client_1 = require("../../src/index.client"); const StorageKnex_1 = require("../../src/storage/StorageKnex"); describe('insert tests', () => { jest.setTimeout(99999999); const storages = []; const chain = 'test'; const env = TestUtilsWalletStorage_1._tu.getEnv(chain); beforeAll(async () => { const localSQLiteFile = await TestUtilsWalletStorage_1._tu.newTmpFile('inserttest.sqlite', false, false, true); const knexSQLite = TestUtilsWalletStorage_1._tu.createLocalSQLite(localSQLiteFile); storages.push(new StorageKnex_1.StorageKnex({ ...StorageKnex_1.StorageKnex.defaultOptions(), chain, knex: knexSQLite })); if (!env.noMySQL) { const knexMySQL = TestUtilsWalletStorage_1._tu.createLocalMySQL('inserttest'); storages.push(new StorageKnex_1.StorageKnex({ ...StorageKnex_1.StorageKnex.defaultOptions(), chain, knex: knexMySQL })); } for (const storage of storages) { await storage.dropAllData(); await storage.migrate('insert tests', '1'.repeat(64)); } }); afterAll(async () => { for (const storage of storages) { await storage.destroy(); } }); test('0 insert ProvenTx', async () => { for (const storage of storages) { const ptx = await TestUtilsWalletStorage_1._tu.insertTestProvenTx(storage); expect(ptx.provenTxId).toBe(1); ptx.provenTxId = 0; // duplicate must throw await expect(storage.insertProvenTx(ptx)).rejects.toThrow(); ptx.provenTxId = 0; ptx.txid = '4'.repeat(64); ptx.provenTxId = await storage.insertProvenTx(ptx); // MySQL counts the failed insertion as a used id, SQLite does not. expect(ptx.provenTxId).toBeGreaterThan(1); } }); test('1 insert ProvenTxReq', async () => { for (const storage of storages) { const ptxreq = await TestUtilsWalletStorage_1._tu.insertTestProvenTxReq(storage); expect(ptxreq.provenTxReqId).toBe(1); ptxreq.provenTxReqId = 0; // duplicate must throw await expect(storage.insertProvenTxReq(ptxreq)).rejects.toThrow(); ptxreq.provenTxReqId = 0; ptxreq.txid = '4'.repeat(64); await storage.insertProvenTxReq(ptxreq); // MySQL counts the failed insertion as a used id, SQLite does not. expect(ptxreq.provenTxReqId).toBeGreaterThan(1); ptxreq.provenTxId = 9999; // non-existent await expect(storage.insertProvenTxReq(ptxreq)).rejects.toThrow(); } }); test('2 insert User', async () => { for (const storage of storages) { const e = await TestUtilsWalletStorage_1._tu.insertTestUser(storage); const id = e.userId; expect(id).toBeGreaterThan(0); e.userId = 0; // duplicate must throw await expect(storage.insertUser(e)).rejects.toThrow(); e.userId = 0; e.identityKey = (0, index_client_1.randomBytesHex)(33); await storage.insertUser(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.userId).toBeGreaterThan(id); } }); test('3 insert Certificate', async () => { for (const storage of storages) { const e = await TestUtilsWalletStorage_1._tu.insertTestCertificate(storage); const id = e.certificateId; expect(id).toBeGreaterThan(0); e.certificateId = 0; // duplicate must throw await expect(storage.insertCertificate(e)).rejects.toThrow(); e.certificateId = 0; e.serialNumber = (0, index_client_1.randomBytesBase64)(33); await storage.insertCertificate(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.certificateId).toBeGreaterThan(id); } }); test('4 insert CertificateField', async () => { for (const storage of storages) { const c = await TestUtilsWalletStorage_1._tu.insertTestCertificate(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestCertificateField(storage, c, 'prize', 'starship'); expect(e.certificateId).toBe(c.certificateId); expect(e.userId).toBe(c.userId); expect(e.fieldName).toBe('prize'); // duplicate must throw await expect(storage.insertCertificateField(e)).rejects.toThrow(); e.fieldName = 'address'; await storage.insertCertificateField(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.fieldName).toBe('address'); } }); test('5 insert OutputBasket', async () => { for (const storage of storages) { const e = await TestUtilsWalletStorage_1._tu.insertTestOutputBasket(storage); const id = e.basketId; expect(id).toBeGreaterThan(0); e.basketId = 0; // duplicate must throw await expect(storage.insertOutputBasket(e)).rejects.toThrow(); e.basketId = 0; e.name = (0, index_client_1.randomBytesHex)(10); await storage.insertOutputBasket(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.basketId).toBeGreaterThan(id); } }); test('6 insert Transaction', async () => { for (const storage of storages) { const { tx: e, user } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); const id = e.transactionId; expect(id).toBeGreaterThan(0); e.transactionId = 0; // duplicate must throw await expect(storage.insertTransaction(e)).rejects.toThrow(); e.transactionId = 0; e.reference = (0, index_client_1.randomBytesBase64)(10); await storage.insertTransaction(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.transactionId).toBeGreaterThan(id); } }); test('7 insert Commission', async () => { for (const storage of storages) { const { tx: t, user } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestCommission(storage, t); const id = e.commissionId; expect(id).toBeGreaterThan(0); e.commissionId = 0; // duplicate must throw await expect(storage.insertCommission(e)).rejects.toThrow(); e.commissionId = 0; const { tx: t2 } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); e.transactionId = t2.transactionId; e.userId = t2.userId; await storage.insertCommission(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.commissionId).toBeGreaterThan(id); } }); test('8 insert Output', async () => { for (const storage of storages) { const { tx: t, user } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestOutput(storage, t, 0, 101); const id = e.outputId; expect(id).toBeGreaterThan(0); expect(e.userId).toBe(t.userId); expect(e.transactionId).toBe(t.transactionId); expect(e.vout).toBe(0); expect(e.satoshis).toBe(101); // duplicate must throw e.outputId = 0; await expect(storage.insertOutput(e)).rejects.toThrow(); e.vout = 1; await storage.insertOutput(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.outputId).toBeGreaterThan(id); } }); test('9 insert OutputTag', async () => { for (const storage of storages) { const u = await TestUtilsWalletStorage_1._tu.insertTestUser(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestOutputTag(storage, u); const id = e.outputTagId; expect(id).toBeGreaterThan(0); expect(e.userId).toBe(u.userId); expect(e.tag).toBeTruthy(); // duplicate must throw e.outputTagId = 0; await expect(storage.insertOutputTag(e)).rejects.toThrow(); e.tag = (0, index_client_1.randomBytesHex)(6); await storage.insertOutputTag(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.outputTagId).toBeGreaterThan(id); } }); test('10 insert OutputTagMap', async () => { for (const storage of storages) { const { tx, user } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); const o = await TestUtilsWalletStorage_1._tu.insertTestOutput(storage, tx, 0, 101); const tag = await TestUtilsWalletStorage_1._tu.insertTestOutputTag(storage, user); const e = await TestUtilsWalletStorage_1._tu.insertTestOutputTagMap(storage, o, tag); expect(e.outputId).toBe(o.outputId); expect(e.outputTagId).toBe(tag.outputTagId); // duplicate must throw await expect(storage.insertOutputTagMap(e)).rejects.toThrow(); const tag2 = await TestUtilsWalletStorage_1._tu.insertTestOutputTag(storage, user); const e2 = await TestUtilsWalletStorage_1._tu.insertTestOutputTagMap(storage, o, tag2); } }); test('11 insert TxLabel', async () => { for (const storage of storages) { const u = await TestUtilsWalletStorage_1._tu.insertTestUser(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestTxLabel(storage, u); const id = e.txLabelId; expect(id).toBeGreaterThan(0); expect(e.userId).toBe(u.userId); expect(e.label).toBeTruthy(); // duplicate must throw e.txLabelId = 0; await expect(storage.insertTxLabel(e)).rejects.toThrow(); e.label = (0, index_client_1.randomBytesHex)(6); await storage.insertTxLabel(e); // MySQL counts the failed insertion as a used id, SQLite does not. expect(e.txLabelId).toBeGreaterThan(id); } }); test('12 insert TxLabelMap', async () => { for (const storage of storages) { const { tx, user } = await TestUtilsWalletStorage_1._tu.insertTestTransaction(storage); const label = await TestUtilsWalletStorage_1._tu.insertTestTxLabel(storage, user); const e = await TestUtilsWalletStorage_1._tu.insertTestTxLabelMap(storage, tx, label); expect(e.transactionId).toBe(tx.transactionId); expect(e.txLabelId).toBe(label.txLabelId); // duplicate must throw await expect(storage.insertTxLabelMap(e)).rejects.toThrow(); const label2 = await TestUtilsWalletStorage_1._tu.insertTestTxLabel(storage, user); const e2 = await TestUtilsWalletStorage_1._tu.insertTestTxLabelMap(storage, tx, label2); } }); test('13 insert MonitorEvent', async () => { for (const storage of storages) { const e = await TestUtilsWalletStorage_1._tu.insertTestMonitorEvent(storage); const id = e.id; expect(id).toBeGreaterThan(0); } }); test('14 insert SyncState', async () => { for (const storage of storages) { const u = await TestUtilsWalletStorage_1._tu.insertTestUser(storage); const e = await TestUtilsWalletStorage_1._tu.insertTestSyncState(storage, u); const id = e.syncStateId; expect(id).toBeGreaterThan(0); } }); }); //# sourceMappingURL=insert.test.js.map