Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Lefebvre committed Oct 25, 2023
1 parent 65ff3e8 commit f7f142e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
69 changes: 68 additions & 1 deletion src/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@ import { getContracts, getChain, getTotalSupply, getBalanceChanges } from "./que

const chain = "eth";
const address = 'dac17f958d2ee523a2206206994597c13d831ec7'
const limit = 1;
const limit = '1';
const symbol = "USDT";
const name = "Tether USD";
const greater_or_equals_by_timestamp = "1697587200";
const less_or_equals_by_timestamp = "1697587100";
const transaction_id = "ab3612eed62a184eed2ae86bcad766183019cf40f82e5316f4d7c4e61f4baa44"

//Test Contract
test("getContracts", () => {
expect(getContracts(new URLSearchParams({ chain, address })).replace(/\s+/g, ''))
.toBe(`SELECT * FROM Contracts JOIN blocks ON blocks.block_id = Contracts.block_id WHERE(chain == '${chain}' AND address == '${address}') ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, ''));
});

//Test Contract with optionals options
test("getContracts Optional", () => {
expect(getContracts(new URLSearchParams({ chain, address, symbol, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, name, limit })).replace(/\s+/g, ''))
.toBe(`SELECT * FROM Contracts JOIN blocks ON blocks.block_id = Contracts.block_id WHERE(chain == '${chain}' AND address == '${address}' AND symbol == '${symbol}' AND name =='${name}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp}) ORDER BY block_number DESC LIMIT ${limit} `.replace(/\s+/g, ''));
});


//Test TotalSupply
test("getTotalSupply", () => {
expect(getTotalSupply(new URLSearchParams({ chain, address })).replace(/\s+/g, ''))
.toBe(`SELECT
Expand All @@ -31,6 +46,32 @@ test("getTotalSupply", () => {
ORDER BY block_number
DESC LIMIT 1 `.replace(/\s+/g, ''));
});

//Test TotalSupply
test("getTotalSupply optional", () => {
expect(getTotalSupply(new URLSearchParams({ chain, address, symbol, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, name, limit })).replace(/\s+/g, ''))
.toBe(`SELECT
TotalSupply.address as address,
TotalSupply.supply as supply,
TotalSupply.id as id,
block_number,
TotalSupply.module_hash as module_hash,
TotalSupply.chain as chain,
Contracts.name as name,
Contracts.symbol as symbol,
Contracts.decimals as decimals,
timestamp,
FROM TotalSupply
JOIN blocks ON blocks.block_id = TotalSupply.block_id
LEFT JOIN Contracts ON Contracts.address = TotalSupply.address
WHERE(TotalSupply.chain == '${chain}' AND TotalSupply.address == '${address}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp} AND symbol == '${symbol}' AND name == '${name}')
ORDER BY block_number
DESC LIMIT ${limit} `.replace(/\s+/g, ''));
});



//Test BalanceChanges
test("getBalanceChanges", () => {
expect(getBalanceChanges(new URLSearchParams({ chain, owner: address })).replace(/\s+/g, ''))
.toBe(`SELECT balance_changes.contract as contract,
Expand All @@ -52,6 +93,32 @@ test("getBalanceChanges", () => {
WHERE(chain == '${chain}' AND owner == '${address}') ORDER BY block_number DESC LIMIT 1 `.replace(/\s+/g, ''))
});




//Test BalanceChanges Optional
test("getBalanceChanges Optional", () => {
expect(getBalanceChanges(new URLSearchParams({ chain, owner: address, transaction_id, greater_or_equals_by_timestamp, less_or_equals_by_timestamp, limit })).replace(/\s+/g, ''))
.toBe(`SELECT balance_changes.contract as contract,
Contracts.name as name,
Contracts.symbol as symbol,
Contracts.decimals as decimals,
balance_changes.owner as owner,
balance_changes.old_balance as old_balance,
balance_changes.new_balance as new_balance,
balance_changes.transaction_id as transaction_id,
balance_changes.id as id,
balance_changes.module_hash as module_hash,
balance_changes.chain as chain,
block_number,
timestamp
FROM balance_changes
JOIN blocks ON blocks.block_id = balance_changes.block_id
LEFT JOIN Contracts ON Contracts.address = balance_changes.contract
WHERE(chain == '${chain}' AND owner == '${address}' AND balance_changes.transaction_id == '${transaction_id}' AND toUnixTimestamp(timestamp) >= ${greater_or_equals_by_timestamp} AND toUnixTimestamp(timestamp) <= ${less_or_equals_by_timestamp}) ORDER BY block_number DESC LIMIT ${limit} `.replace(/\s+/g, ''))
});


test("getChain", () => {
expect(getChain()).toBe(`SELECT DISTINCT chain FROM module_hashes`);
});
22 changes: 17 additions & 5 deletions src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { formatAddress, checkValidAddress, getAddress } from "./utils.js";
import { formatAddress, checkValidAddress, getAddress, parseBlockId, parseTimestamp } from "./utils.js";

const address = "0xdac17f958d2ee523a2206206994597c13d831ec7";

Expand All @@ -14,9 +14,21 @@ test("checkValidAddress", () => {
expect(() => checkValidAddress("foobar")).toThrow("Invalid address");
});

test("parseBlockId", () => {
expect(parseBlockId("0x123") as string).toBe("123");
});

test("parseTimestamp", () => {
expect(parseTimestamp("1697587100")).toBe(1697587100);
expect(parseTimestamp("1697587100000")).toBe(1697587100);
expect(parseTimestamp("awdawd")).toBeNaN();
expect(parseTimestamp(null)).toBeUndefined();
});


test("getAddress", () => {
expect(() => getAddress(new URLSearchParams({address: address}), "address", false)).not.toThrow();
expect(() => getAddress(new URLSearchParams({address: address}), "address", true)).not.toThrow();
expect(() => getAddress(new URLSearchParams({address: ""}), "address", true)).toThrow("Missing [address] parameter");
expect(() => getAddress(new URLSearchParams({address: "foobar"}), "address")).toThrow("Invalid address");
expect(() => getAddress(new URLSearchParams({ address: address }), "address", false)).not.toThrow();
expect(() => getAddress(new URLSearchParams({ address: address }), "address", true)).not.toThrow();
expect(() => getAddress(new URLSearchParams({ address: "" }), "address", true)).toThrow("Missing [address] parameter");
expect(() => getAddress(new URLSearchParams({ address: "foobar" }), "address")).toThrow("Invalid address");
});

0 comments on commit f7f142e

Please sign in to comment.