Skip to content

Commit

Permalink
✅ test(add skill): add tests for add skill command
Browse files Browse the repository at this point in the history
Signed-off-by: csc530 <[email protected]>
  • Loading branch information
csc530 committed Dec 22, 2023
1 parent 5287265 commit 8588a42
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 32 deletions.
57 changes: 57 additions & 0 deletions TestResumeBuilder/commands/add/AddSKillTest.cs
Original file line number Diff line number Diff line change
@@ -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<Exception>(() => TestApp.Run(cmdArgs));
}

[Theory]
[InlineData(SkillType.Soft)]
[InlineData(SkillType.Hard)]
[InlineData(SkillType.Technical)]
public void ReturnsError_WhenSkillNameIsEmpty_ShouldFail(SkillType skillType)
{
Assert.ThrowsAny<Exception>(() => 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<Exception>(() => TestApp.Run(cmdArgs, "skill", invalidSkillType));
}
}

public class AddSkillTestData: TestData
{
public static TheoryData<string, SkillType> GetSkillData()
{
var skillTypes = Enum.GetValues<SkillType>();
var skillData = new TheoryData<string, SkillType>();
for(var i = 0; i < TestRepetition; i++)
skillData.Add(Faker.Random.Words(), skillTypes[i % skillTypes.Length]);
return skillData;
}
}
12 changes: 7 additions & 5 deletions resume builder/cli/commands/add/AddCommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
38 changes: 14 additions & 24 deletions resume builder/cli/commands/add/AddSkillCommand.cs
Original file line number Diff line number Diff line change
@@ -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<AddSkillSettings>
public class AddSkillCommand: Command<AddSkillSettings>
{
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<string>("Skill: ");
skillType = new SelectionPrompt<SkillType>()
.Title("Skill Type")
.AddChoices(Enum.GetValues<SkillType>())
.MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
.WrapAround()
.Show(AnsiConsole.Console);
.Title("Skill Type")
.AddChoices(Enum.GetValues<SkillType>())
.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);

Check warning on line 26 in resume builder/cli/commands/add/AddSkillCommand.cs

View workflow job for this annotation

GitHub Actions / qodana

Possible null reference argument for a parameter.

Possible null reference argument for parameter 'name' in 'resume_builder.models.Skill.Skill'
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")]
Expand Down
5 changes: 2 additions & 3 deletions resume builder/models/Skill.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.Sqlite;

namespace resume_builder.models;

Expand All @@ -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;
}
}
Expand Down

0 comments on commit 8588a42

Please sign in to comment.