Skip to content

Commit

Permalink
Make Response commands case insensitive (#373)
Browse files Browse the repository at this point in the history
* Make Response commands case insensitive

* Removed the case checking on upsert. Everything is forced to lowercase anyway.

* Simplified delete command
  • Loading branch information
m4-used-rollout authored Dec 14, 2023
1 parent fde8c4d commit c5939fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions TPP.Persistence.MongoDB/Repos/ResponseCommandRepo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using System.Xml.Linq;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using TPP.Model;
Expand Down Expand Up @@ -36,9 +37,10 @@ public async Task<IImmutableList<ResponseCommand>> GetCommands() =>

public async Task<ResponseCommand> UpsertCommand(string command, string response)
{
ResponseCommand newCommand = new(command, response);
var commandLower = command.ToLower();
ResponseCommand newCommand = new(commandLower, response);
ResponseCommand? oldCommand = await Collection.FindOneAndReplaceAsync(
Builders<ResponseCommand>.Filter.Eq(c => c.Command, command),
Builders<ResponseCommand>.Filter.Eq(c => c.Command, commandLower),
newCommand,
new FindOneAndReplaceOptions<ResponseCommand>
{
Expand All @@ -53,7 +55,8 @@ public async Task<ResponseCommand> UpsertCommand(string command, string response

public async Task<bool> RemoveCommand(string command)
{
DeleteResult deleteOneAsync = await Collection.DeleteOneAsync(c => c.Command == command);
var commandLower = command.ToLower();
DeleteResult deleteOneAsync = await Collection.DeleteOneAsync(c => c.Command == command || c.Command == commandLower);
CommandRemoved?.Invoke(this, command);
return deleteOneAsync.DeletedCount > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public async Task persists_and_deletes_successfully()
ResponseCommandRepo repo = new(CreateTemporaryDatabase());
Assert.That(await repo.GetCommands(), Is.Empty);

ResponseCommand command1 = await repo.UpsertCommand("command1", "response 1");
ResponseCommand command1 = await repo.UpsertCommand("Command1", "response 1");
ResponseCommand command2 = await repo.UpsertCommand("command2", "response 2");
Assert.That(await repo.GetCommands(), Is.EquivalentTo(new[] { command1, command2 }));

Assert.That(await repo.RemoveCommand("command1"), Is.True);
Assert.That(await repo.RemoveCommand("COMMAND1"), Is.True);
Assert.That(await repo.RemoveCommand("command1"), Is.False); // already deleted
Assert.That(await repo.GetCommands(), Is.EquivalentTo(new[] { command2 }));
}
Expand All @@ -30,7 +30,7 @@ public async Task updates_existing()

ResponseCommand command1 = await repo.UpsertCommand("command", "response 1");
Assert.That(await repo.GetCommands(), Is.EquivalentTo(new[] { command1 }));
ResponseCommand command2 = await repo.UpsertCommand("command", "response 2");
ResponseCommand command2 = await repo.UpsertCommand("COMMAND", "response 2");
Assert.That(await repo.GetCommands(), Is.EquivalentTo(new[] { command2 }));
}
}
Expand Down

0 comments on commit c5939fd

Please sign in to comment.