From 8588a42c962a7a0d1d71d3d2328b00400731a9f3 Mon Sep 17 00:00:00 2001 From: csc530 <77406318+csc530@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:06:28 -0500 Subject: [PATCH] :white_check_mark: test(add skill): add tests for add skill command Signed-off-by: csc530 <77406318+csc530@users.noreply.github.com> --- .../commands/add/AddSKillTest.cs | 57 +++++++++++++++++++ .../cli/commands/add/AddCommandSettings.cs | 12 ++-- .../cli/commands/add/AddSkillCommand.cs | 38 +++++-------- resume builder/models/Skill.cs | 5 +- 4 files changed, 80 insertions(+), 32 deletions(-) create mode 100644 TestResumeBuilder/commands/add/AddSKillTest.cs diff --git a/TestResumeBuilder/commands/add/AddSKillTest.cs b/TestResumeBuilder/commands/add/AddSKillTest.cs new file mode 100644 index 0000000..7441966 --- /dev/null +++ b/TestResumeBuilder/commands/add/AddSKillTest.cs @@ -0,0 +1,57 @@ +using resume_builder.models; + +namespace TestResumeBuilder.commands.add; + +public class AddSkillTest: TestBase +{ + private readonly string[] cmdArgs = { "add", "skill" }; + + + [Theory] + [MemberData(nameof(AddSkillTestData.GetSkillData), MemberType = typeof(AddSkillTestData))] + public void AddsSkill_WhenAllFieldsAreValid_ShouldSucceed(string skillName, SkillType skillType) + { + var result = TestApp.Run(cmdArgs, skillName, skillType.ToString()); + Assert.Equal(0, result.ExitCode); + } + + [Fact] + public void PromptsUser_WhenFieldsAreEmpty_ShouldFail() + { + Assert.ThrowsAny(() => TestApp.Run(cmdArgs)); + } + + [Theory] + [InlineData(SkillType.Soft)] + [InlineData(SkillType.Hard)] + [InlineData(SkillType.Technical)] + public void ReturnsError_WhenSkillNameIsEmpty_ShouldFail(SkillType skillType) + { + Assert.ThrowsAny(() => TestApp.Run(cmdArgs, "", skillType.ToString())); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] + [InlineData("\n")] + [InlineData("debugging")] + [InlineData( + "r ut erat et ipsum stet sed zzril eos aliquyam sed duo nisl iusto. Clita clita ipsum dolor illum ut ipsum lorem kasd sea dolores rebum et elitr elitr magna. Feugiat takimata amet mi")] + public void ReturnsError_WhenSkillTypeIsInvalid_ShouldFail(string invalidSkillType) + { + Assert.ThrowsAny(() => TestApp.Run(cmdArgs, "skill", invalidSkillType)); + } +} + +public class AddSkillTestData: TestData +{ + public static TheoryData GetSkillData() + { + var skillTypes = Enum.GetValues(); + var skillData = new TheoryData(); + for(var i = 0; i < TestRepetition; i++) + skillData.Add(Faker.Random.Words(), skillTypes[i % skillTypes.Length]); + return skillData; + } +} \ No newline at end of file diff --git a/resume builder/cli/commands/add/AddCommandSettings.cs b/resume builder/cli/commands/add/AddCommandSettings.cs index b68ffc3..910603d 100644 --- a/resume builder/cli/commands/add/AddCommandSettings.cs +++ b/resume builder/cli/commands/add/AddCommandSettings.cs @@ -4,10 +4,12 @@ namespace resume_builder.cli.commands.add; -public class AddCommandSettings : CLISettings +public class AddCommandSettings: CLISettings { - [CommandOption("-i|--interactive")] - [Description("interactive mode")] - [DefaultValue(false)] - public bool Interactive { get; set; } + //todo: change to be user first (interactive) and cmd/pipeable second (non interactive) + //probably make it command level + [CommandOption("-i|--interactive")] + [Description("interactive mode")] + [DefaultValue(false)] + public bool Interactive { get; set; } } \ No newline at end of file diff --git a/resume builder/cli/commands/add/AddSkillCommand.cs b/resume builder/cli/commands/add/AddSkillCommand.cs index f9371cf..9231e81 100644 --- a/resume builder/cli/commands/add/AddSkillCommand.cs +++ b/resume builder/cli/commands/add/AddSkillCommand.cs @@ -1,48 +1,38 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using resume_builder.models; -using resume_builder.models; using Spectre.Console; using Spectre.Console.Cli; -using static resume_builder.Globals; namespace resume_builder.cli.commands.add; -//todo: add interactive mode -public class AddSkillCommand : Command +public class AddSkillCommand: Command { public override int Execute([NotNull] CommandContext context, [NotNull] AddSkillSettings settings) { var skillName = settings.Skill; var skillType = settings.SkillType; - if(settings.Interactive || skillName.IsBlank() || skillType == null) + if(settings.Interactive || skillName.IsBlank() && skillType == null) { skillName = AnsiConsole.Ask("Skill: "); skillType = new SelectionPrompt() - .Title("Skill Type") - .AddChoices(Enum.GetValues()) - .MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]") - .WrapAround() - .Show(AnsiConsole.Console); + .Title("Skill Type") + .AddChoices(Enum.GetValues()) + .MoreChoicesText("[grey](Move up and down to reveal more skill types)[/]") + .WrapAround() + .Show(AnsiConsole.Console); } - try - { - var skill = new Skill(skillName, skillType.Value); - ResumeContext database = new(); - database.Skills.Add(skill); - database.SaveChanges(); - AnsiConsole.MarkupLine($"""✅ [bold]{skill.Type}[/] Skill "[bold]{skill.Name}[/]" added"""); - return ExitCode.Success.ToInt(); - } - catch(Exception e) - { - return PrintError(settings, e); - } + var skill = new Skill(skillName, skillType.Value); + ResumeContext database = new(); + database.Skills.Add(skill); + database.SaveChanges(); + AnsiConsole.MarkupLineInterpolated($"""✅ [bold]{skill.Type}[/] Skill "[bold]{skill.Name}[/]" added"""); + return ExitCode.Success.ToInt(); } } -public class AddSkillSettings : AddCommandSettings +public class AddSkillSettings: AddCommandSettings { [CommandArgument(0, "[skill]")] [Description("The name, abbreviation, or short description of the skill")] diff --git a/resume builder/models/Skill.cs b/resume builder/models/Skill.cs index 6670745..aee12ae 100644 --- a/resume builder/models/Skill.cs +++ b/resume builder/models/Skill.cs @@ -1,6 +1,4 @@ using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using Microsoft.Data.Sqlite; namespace resume_builder.models; @@ -21,7 +19,8 @@ public string Name get => _name; set { - ArgumentException.ThrowIfNullOrWhiteSpace(value); + if(value.IsBlank()) + throw new ArgumentException("Skill name cannot be empty"); _name = value; } }