Skip to content

Commit

Permalink
chore(core-snapshots): change codec properties style to decode/encode…
Browse files Browse the repository at this point in the history
…<Entity>
  • Loading branch information
sebastijankuzner committed May 4, 2020
1 parent fe8bb6d commit 894a633
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 90 deletions.
42 changes: 21 additions & 21 deletions __tests__/unit/core-snapshots/codecs/codec.test.ts
Expand Up @@ -23,9 +23,9 @@ beforeEach(() => {
})

describe("Codec", () => {
describe("blocksEncode", () => {
describe("encodeBlock", () => {
it("should be ok", async () => {
let encoded = codec.blocksEncode(appendPrefix("Block_", Assets.blocks[1]));
let encoded = codec.encodeBlock(appendPrefix("Block_", Assets.blocks[1]));

expect(encoded).toBeDefined();
});
Expand All @@ -35,79 +35,79 @@ describe("Codec", () => {
Block_id: "123"
}

expect(() => {codec.blocksEncode(corruptedBlock)}).toThrow();
expect(() => {codec.encodeBlock(corruptedBlock)}).toThrow();
});
})

describe("blocksDecode", () => {
describe("decodeBlock", () => {
it("should be ok", async () => {
let encoded = codec.blocksEncode(appendPrefix("Block_", Assets.blocks[1]));
let encoded = codec.encodeBlock(appendPrefix("Block_", Assets.blocks[1]));

expect(encoded).toBeDefined();

let decoded = codec.blocksDecode(encoded);
let decoded = codec.decodeBlock(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.blocksDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeBlock(Buffer.from(""))}).toThrow();
});
})

describe("transactionsEncode", () => {
describe("encodeTransaction", () => {
it("should be ok", async () => {
let encoded = codec.transactionsEncode(Assets.transactions[0]);
let encoded = codec.encodeTransaction(Assets.transactions[0]);

expect(encoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.transactionsEncode(undefined)}).toThrow();
expect(() => {codec.encodeTransaction(undefined)}).toThrow();
});
})

describe("transactionsDecode", () => {
describe("decodeTransaction", () => {
it("should be ok", async () => {
let encoded = codec.transactionsEncode(appendPrefix("Transaction_", Assets.transactions[0]));
let encoded = codec.encodeTransaction(appendPrefix("Transaction_", Assets.transactions[0]));

expect(encoded).toBeDefined();

let decoded = codec.transactionsDecode(encoded);
let decoded = codec.decodeTransaction(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.transactionsDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeTransaction(Buffer.from(""))}).toThrow();
});
})

describe("roundsEncode", () => {
describe("encodeRound", () => {
it("should be ok", async () => {
let encoded = codec.roundsEncode(appendPrefix("Round_", Assets.rounds[0]));
let encoded = codec.encodeRound(appendPrefix("Round_", Assets.rounds[0]));

expect(encoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.roundsEncode(undefined)}).toThrow();
expect(() => {codec.encodeRound(undefined)}).toThrow();
});
})

describe("roundsDecode", () => {
describe("decodeRound", () => {
it("should be ok", async () => {
let encoded = codec.roundsEncode(appendPrefix("Round_", Assets.rounds[0]));
let encoded = codec.encodeRound(appendPrefix("Round_", Assets.rounds[0]));

expect(encoded).toBeDefined();

let decoded = codec.roundsDecode(encoded);
let decoded = codec.decodeRound(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.roundsDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeRound(Buffer.from(""))}).toThrow();
});
})
});
42 changes: 21 additions & 21 deletions __tests__/unit/core-snapshots/codecs/json-codec.test.ts
Expand Up @@ -23,87 +23,87 @@ beforeEach(() => {
})

describe("Codec", () => {
describe("blocksEncode", () => {
describe("encodeBlock", () => {
it("should be ok", async () => {
let encoded = codec.blocksEncode(appendPrefix("Block_", Assets.blocks[1]));
let encoded = codec.encodeBlock(appendPrefix("Block_", Assets.blocks[1]));

expect(encoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.blocksEncode(undefined)}).toThrow();
expect(() => {codec.encodeBlock(undefined)}).toThrow();
});
})

describe("blocksDecode", () => {
describe("decodeBlock", () => {
it("should be ok", async () => {
let encoded = codec.blocksEncode(appendPrefix("Block_", Assets.blocks[1]));
let encoded = codec.encodeBlock(appendPrefix("Block_", Assets.blocks[1]));

expect(encoded).toBeDefined();

let decoded = codec.blocksDecode(encoded);
let decoded = codec.decodeBlock(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.blocksDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeBlock(Buffer.from(""))}).toThrow();
});
})

describe("transactionsEncode", () => {
describe("encodeTransaction", () => {
it("should be ok", async () => {
let encoded = codec.transactionsEncode(Assets.transactions[0]);
let encoded = codec.encodeTransaction(Assets.transactions[0]);

expect(encoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.transactionsEncode(undefined)}).toThrow();
expect(() => {codec.encodeTransaction(undefined)}).toThrow();
});
})

describe("transactionsDecode", () => {
describe("decodeTransaction", () => {
it("should be ok", async () => {
let encoded = codec.transactionsEncode(appendPrefix("Transaction_", Assets.transactions[0]));
let encoded = codec.encodeTransaction(appendPrefix("Transaction_", Assets.transactions[0]));

expect(encoded).toBeDefined();

let decoded = codec.transactionsDecode(encoded);
let decoded = codec.decodeTransaction(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.transactionsDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeTransaction(Buffer.from(""))}).toThrow();
});
})

describe("roundsEncode", () => {
describe("encodeRound", () => {
it("should be ok", async () => {
let encoded = codec.roundsEncode(appendPrefix("Round_", Assets.rounds[0]));
let encoded = codec.encodeRound(appendPrefix("Round_", Assets.rounds[0]));

expect(encoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.roundsEncode(undefined)}).toThrow();
expect(() => {codec.encodeRound(undefined)}).toThrow();
});
})

describe("roundsDecode", () => {
describe("decodeRound", () => {
it("should be ok", async () => {
let encoded = codec.roundsEncode(appendPrefix("Round_", Assets.rounds[0]));
let encoded = codec.encodeRound(appendPrefix("Round_", Assets.rounds[0]));

expect(encoded).toBeDefined();

let decoded = codec.roundsDecode(encoded);
let decoded = codec.decodeRound(encoded);

expect(decoded).toBeDefined();
});

it("should throw error", async () => {
expect(() => {codec.roundsDecode(Buffer.from(""))}).toThrow();
expect(() => {codec.decodeRound(Buffer.from(""))}).toThrow();
});
})
});
@@ -1,5 +1,8 @@
import "jest-extended";

import pluralize from "pluralize";
import { pascalize } from "xcase";

import { Readable } from "stream";
import { dirSync, setGracefulCleanup } from "tmp";
import {decamelize} from "xcase";
Expand Down Expand Up @@ -59,6 +62,26 @@ class DbStream extends Readable {
}
}

const getSingularCapitalizedTableName = (table: string) => {
return pascalize(pluralize.singular(table));
}

const getEncode = (table, codec) => {
if (codec === "json") {
return new JSONCodec()[`encode${getSingularCapitalizedTableName(table)}`];
} else {
return new MessagePackCodec()[`encode${getSingularCapitalizedTableName(table)}`];
}
}

const getDecode = (table, codec) => {
if (codec === "json") {
return new JSONCodec()[`decode${getSingularCapitalizedTableName(table)}`];
} else {
return new MessagePackCodec()[`decode${getSingularCapitalizedTableName(table)}`];
}
}

let sandbox: Sandbox;
let streamWriterFactory: Contracts.Stream.StreamWriterFactory;
let streamReaderFactory: Contracts.Stream.StreamReaderFactory;
Expand Down Expand Up @@ -102,22 +125,6 @@ const cases = [
["rounds", "json", true],
]

const getEncode = (table, codec) => {
if (codec === "json") {
return new JSONCodec()[`${table}Encode`];
} else {
return new MessagePackCodec()[`${table}Encode`];
}
}

const getDecode = (table, codec) => {
if (codec === "json") {
return new JSONCodec()[`${table}Decode`];
} else {
return new MessagePackCodec()[`${table}Decode`];
}
}

describe("StreamReader and StreamWriter", () => {
describe.each(cases)("Table: [%s], Codec: [%s], UseCompression : [%s]", (table, codec, useCompression) => {
let file: string;
Expand Down
Expand Up @@ -56,7 +56,7 @@ describe("ReadProcessor", () => {
it("should read all blocks", async () => {
let path = join(__dirname, "../__fixtures__/1-52/blocks")

let streamReader = new StreamReader(path, false, new JSONCodec().blocksDecode)
let streamReader = new StreamReader(path, false, new JSONCodec().decodeBlock)

await streamReader.open();

Expand Down
12 changes: 6 additions & 6 deletions packages/core-snapshots/src/codecs/json-codec.ts
Expand Up @@ -43,7 +43,7 @@ export class JSONCodec implements Codec {
});
}

public blocksEncode(block): Buffer {
public encodeBlock(block): Buffer {
try {
const blockStringified = JSONCodec.stringify(camelizeKeys(JSONCodec.removePrefix(block, "Block_")));

Expand All @@ -53,15 +53,15 @@ export class JSONCodec implements Codec {
}
}

public blocksDecode(buffer: Buffer): Models.Block {
public decodeBlock(buffer: Buffer): Models.Block {
try {
return JSON.parse(buffer.toString());
} catch (err) {
throw new CodecException.BlockDecodeException(undefined, err.message);
}
}

public transactionsEncode(transaction): Buffer {
public encodeTransaction(transaction): Buffer {
try {
let tmp = JSONCodec.removePrefix(transaction, "Transaction_");
tmp = camelizeKeys(tmp);
Expand All @@ -74,7 +74,7 @@ export class JSONCodec implements Codec {
}
}

public transactionsDecode(buffer: Buffer): Models.Transaction {
public decodeTransaction(buffer: Buffer): Models.Transaction {
try {
const tmp = JSONCodec.parse(buffer.toString());

Expand All @@ -92,15 +92,15 @@ export class JSONCodec implements Codec {
}
}

public roundsEncode(round): Buffer {
public encodeRound(round): Buffer {
try {
return Buffer.from(JSONCodec.stringify(camelizeKeys(JSONCodec.removePrefix(round, "Round_"))));
} catch (err) {
throw new CodecException.RoundEncodeException(round.Round_round, err.message);
}
}

public roundsDecode(buffer: Buffer): Models.Round {
public decodeRound(buffer: Buffer): Models.Round {
try {
return JSON.parse(buffer.toString());
} catch (err) {
Expand Down
12 changes: 6 additions & 6 deletions packages/core-snapshots/src/codecs/message-pack-codec.ts
Expand Up @@ -19,7 +19,7 @@ export class MessagePackCodec implements Codec {
return itemToReturn;
}

public blocksEncode(block: any): Buffer {
public encodeBlock(block: any): Buffer {
try {
const blockCamelized = camelizeKeys(MessagePackCodec.removePrefix(block, "Block_"));

Expand All @@ -29,15 +29,15 @@ export class MessagePackCodec implements Codec {
}
}

public blocksDecode(buffer: Buffer): Models.Block {
public decodeBlock(buffer: Buffer): Models.Block {
try {
return Blocks.Deserializer.deserialize(buffer.toString("hex"), false).data as Models.Block;
} catch (err) {
throw new CodecException.BlockDecodeException(undefined, err.message);
}
}

public transactionsEncode(transaction): Buffer {
public encodeTransaction(transaction): Buffer {
try {
return encode([
transaction.Transaction_id,
Expand All @@ -51,7 +51,7 @@ export class MessagePackCodec implements Codec {
}
}

public transactionsDecode(buffer: Buffer): Models.Transaction {
public decodeTransaction(buffer: Buffer): Models.Transaction {
let transactionId = undefined;
try {
const [id, blockId, sequence, timestamp, serialized] = decode(buffer);
Expand Down Expand Up @@ -87,7 +87,7 @@ export class MessagePackCodec implements Codec {
}
}

public roundsEncode(round): Buffer {
public encodeRound(round): Buffer {
try {
const roundCamelized = camelizeKeys(MessagePackCodec.removePrefix(round, "Round_"));

Expand All @@ -97,7 +97,7 @@ export class MessagePackCodec implements Codec {
}
}

public roundsDecode(buffer: Buffer): Models.Round {
public decodeRound(buffer: Buffer): Models.Round {
let roundRound = undefined;

try {
Expand Down

0 comments on commit 894a633

Please sign in to comment.