Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 81 additions & 56 deletions packages/core-database/__tests__/repositories/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,87 +168,112 @@ describe("Delegate Repository", () => {
});

describe("search", () => {
it("should search by exact username match", () => {
beforeEach(() => {
const wallets = generateWallets();
walletManager.index(wallets);
});

describe("by `username`", () => {
it("should search by exact match", () => {
const username = "username-APnhwwyTbMiykJwYbGhYjNgtHiVJDSEhSn";
const { count, rows } = repository.search({ username });

const { count, rows } = repository.search({
username: "username-APnhwwyTbMiykJwYbGhYjNgtHiVJDSEhSn",
expect(count).toBe(1);
expect(rows).toHaveLength(1);
expect(rows[0].username).toEqual(username);
});

expect(count).toBe(1);
expect(rows).toHaveLength(1);
});
it("should search that username contains the string", () => {
const { count, rows } = repository.search({ username: "username" });

it("should search that username contains the string", () => {
const wallets = generateWallets();
walletManager.index(wallets);
expect(count).toBe(52);
expect(rows).toHaveLength(52);
});

const { count, rows } = repository.search({ username: "username" });
describe('when a username is "undefined"', () => {
it("should return it", () => {
// Index a wallet with username "undefined"
const address = Object.keys(walletManager.byAddress)[0];
walletManager.byAddress[address].username = "undefined";

expect(count).toBe(52);
expect(rows).toHaveLength(52);
});
const username = "undefined";
const { count, rows } = repository.search({ username });

describe("when no results", () => {
it("should be ok", () => {
const { count, rows } = repository.search({
username: "unknown-dummy-username",
expect(count).toBe(1);
expect(rows).toHaveLength(1);
expect(rows[0].username).toEqual(username);
});
});

describe("when the username does not exist", () => {
it("should return no results", () => {
const { count, rows } = repository.search({
username: "unknown-dummy-username",
});

expect(count).toBe(0);
expect(rows).toHaveLength(0);
expect(count).toBe(0);
expect(rows).toHaveLength(0);
});
});
});

it("should be ok with params", () => {
const wallets = generateWallets();
walletManager.index(wallets);
it("should be ok with params", () => {
const { count, rows } = repository.search({
username: "username",
offset: 10,
limit: 10,
});
expect(count).toBe(52);
expect(rows).toHaveLength(10);
});

const { count, rows } = repository.search({
username: "username",
offset: 10,
limit: 10,
it("should be ok with params (no offset)", () => {
const { count, rows } = repository.search({
username: "username",
limit: 10,
});
expect(count).toBe(52);
expect(rows).toHaveLength(10);
});
expect(count).toBe(52);
expect(rows).toHaveLength(10);
});

it("should be ok with params (no offset)", () => {
const wallets = generateWallets();
walletManager.index(wallets);
it("should be ok with params (offset = 0)", () => {
const { count, rows } = repository.search({
username: "username",
offset: 0,
limit: 12,
});
expect(count).toBe(52);
expect(rows).toHaveLength(12);
});

const { count, rows } = repository.search({
username: "username",
limit: 10,
it("should be ok with params (no limit)", () => {
const { count, rows } = repository.search({
username: "username",
offset: 10,
});
expect(count).toBe(52);
expect(rows).toHaveLength(42);
});
expect(count).toBe(52);
expect(rows).toHaveLength(10);
});

it("should be ok with params (offset = 0)", () => {
const wallets = generateWallets();
walletManager.index(wallets);
describe("when searching without params", () => {
it("should return all results", () => {
const { count, rows } = repository.search({});

const { count, rows } = repository.search({
username: "username",
offset: 0,
limit: 12,
expect(count).toBe(52);
expect(rows).toHaveLength(52);
});
expect(count).toBe(52);
expect(rows).toHaveLength(12);
});

it("should be ok with params (no limit)", () => {
const wallets = generateWallets();
walletManager.index(wallets);
describe('when a username is "undefined"', () => {
it("should return all results", () => {
// Index a wallet with username "undefined"
const address = Object.keys(walletManager.byAddress)[0];
walletManager.byAddress[address].username = "undefined";

const { count, rows } = repository.search({
username: "username",
offset: 10,
const { count, rows } = repository.search({});
expect(count).toBe(52);
expect(rows).toHaveLength(52);
});
});
expect(count).toBe(52);
expect(rows).toHaveLength(42);
});
});

Expand Down
5 changes: 4 additions & 1 deletion packages/core-database/src/repositories/delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export class DelegatesRepository {
* @return {Object}
*/
public search(params) {
let delegates = this.getLocalDelegates().filter(delegate => delegate.username.indexOf(params.username) > -1);
let delegates = this.getLocalDelegates();
if (params.hasOwnProperty("username")) {
delegates = delegates.filter(delegate => delegate.username.indexOf(params.username) > -1);
}

if (params.orderBy) {
const orderByField = params.orderBy.split(":")[0];
Expand Down