From beba61e4e28b4fcb9f260c24364aba2b95687554 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 8 Dec 2023 17:10:00 +0000 Subject: [PATCH] Add sort option (#123) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Add sort option (`--sort`) to allow you to configure the sort by field - Add query metadata option (`--query-metadata`, `-q`) to return the queried inputs - This helps with testing to ensure that the inputs are properly accepted #74 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - Have tested manually and have added unit tests too - I have added an option to include the query metadata to help with seeing if it worked # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that provde my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/123 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/cli.ts | 13 ++++++-- src/contracts/ICliOptions.ts | 2 ++ src/contracts/IReturnResult.ts | 2 ++ src/contracts/QueryResult.ts | 4 +++ src/index.ts | 16 +++++++++ tests/cli.test.ts | 60 +++++++++++++++++++++++++++++++++- 6 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/contracts/QueryResult.ts diff --git a/src/cli.ts b/src/cli.ts index 0de5ab3..6f0639f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,4 +1,4 @@ -import { Command } from "commander"; +import { Command, Option } from "commander"; import randomBunny from "./index"; import ICliOptions from "./contracts/ICliOptions"; import { exit } from "process"; @@ -9,13 +9,15 @@ program .name('random-bunny') .description('Get a random image url from a subreddit of your choosing') .version('2.2') - .option('-s, --subreddit ', 'The subreddit to search', 'rabbits'); + .option('-s, --subreddit ', 'The subreddit to search', 'rabbits') + .option('-q, --query-metadata', 'Include query metadata in result') + .addOption(new Option('--sort ', 'Sort by').default('hot').choices(['hot', 'new', 'top'])); program.parse(); const options: ICliOptions = program.opts(); -randomBunny(options.subreddit) +randomBunny(options.subreddit, options.sort) .then((response) => { if (response.IsSuccess) { const result = response.Result!; @@ -31,6 +33,11 @@ randomBunny(options.subreddit) outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Url = ${result.Url}`); + if (options.queryMetadata != null) { + outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); + outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); + } + console.log(outputLines.join("\n")); exit(0); } else { diff --git a/src/contracts/ICliOptions.ts b/src/contracts/ICliOptions.ts index c85971d..ce5ada4 100644 --- a/src/contracts/ICliOptions.ts +++ b/src/contracts/ICliOptions.ts @@ -1,3 +1,5 @@ export default interface ICliOptions { subreddit: string, + sort: string, + queryMetadata?: boolean, } \ No newline at end of file diff --git a/src/contracts/IReturnResult.ts b/src/contracts/IReturnResult.ts index 780069d..b5f5303 100644 --- a/src/contracts/IReturnResult.ts +++ b/src/contracts/IReturnResult.ts @@ -1,8 +1,10 @@ import IError from "./IError.js"; import IRedditResult from "./IRedditResult.js"; +import QueryResult from "./QueryResult.js"; export default interface IReturnResult { IsSuccess: boolean; + Query: QueryResult; Result?: IRedditResult; Error?: IError; } \ No newline at end of file diff --git a/src/contracts/QueryResult.ts b/src/contracts/QueryResult.ts new file mode 100644 index 0000000..ee97e30 --- /dev/null +++ b/src/contracts/QueryResult.ts @@ -0,0 +1,4 @@ +export default interface QueryResult { + subreddit: string, + sortBy: string, +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 26195c4..29f8ecf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h if (!result) { return { IsSuccess: false, + Query: { + subreddit: subreddit, + sortBy: sortBy, + }, Error: { Code: ErrorCode.FailedToFetchReddit, Message: ErrorMessages.FailedToFetchReddit, @@ -38,6 +42,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h if (!json) { return { IsSuccess: false, + Query: { + subreddit: subreddit, + sortBy: sortBy, + }, Error: { Code: ErrorCode.UnableToParseJSON, Message: ErrorMessages.UnableToParseJSON, @@ -56,6 +64,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h if (dataWithImages.length == 0) { return { IsSuccess: false, + Query: { + subreddit: subreddit, + sortBy: sortBy, + }, Error: { Code: ErrorCode.NoImageResultsFound, Message: ErrorMessages.NoImageResultsFound, @@ -83,6 +95,10 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h return { IsSuccess: true, + Query: { + subreddit: subreddit, + sortBy: sortBy, + }, Result: redditResult }; } \ No newline at end of file diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 6d105d3..aa68d3b 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -88,7 +88,65 @@ describe('subreddit', () => { expect(subreddit).toBe('Horses'); }, 5000); -}) +}); + +describe('sort', () => { + test('GIVEN --sort is not supplird, EXPECT sort to be defaulted', async () => { + const result = await cli(['-q'], '.'); + + const sortBy = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')! + .split(' = ')[1]; + + expect(sortBy).toBe('hot'); + }, 5000); + + test('GIVEN --sort is supplied WITH a valid input, EXPECT sort to be used', async () => { + const result = await cli(['-q', '--sort', 'new'], '.'); + + const sortBy = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')! + .split(' = ')[1]; + + expect(sortBy).toBe('new'); + }, 5000); + + test('GIVEN --sort is supplied WITH an invalid input, EXPECT error', async () => { + const result = await cli(['-q', '--sort', 'invalid'], '.'); + + expect(result.code).toBe(1); + expect(result.stderr).toBe("error: option '--sort ' argument 'invalid' is invalid. Allowed choices are hot, new, top.\n"); + }); +}); + +describe('query-metadata', () => { + test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => { + const result = await cli([], '.'); + + const query = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query')); + + expect(query).toBeUndefined(); + }, 5000); + + test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => { + const result = await cli(['--query-metadata'], '.'); + + const query = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query')); + + expect(query).toBeDefined(); + }, 5000); + + test('GIVEN -q is not supplied, EXPECT no query metadata returned', async () => { + const result = await cli(['-q'], '.'); + + const query = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query')); + + expect(query).toBeDefined(); + }, 5000); +}); function cli(args: string[], cwd: string): Promise { return new Promise(resolve => {