public void AddKey_Success() { //Arrange Exception exception = null; Locker locker = null; string lockerFilePath = Path.Combine(TestContext.DeploymentDirectory, "lockerforadding.bin"); AddArguments arguments = new AddArguments { LockerPath = lockerFilePath, Password = "******", Key = "somekey", Value = "somevalue" }; //Act try { Program.AddKey(arguments); locker = Program.GetLocker(arguments); } catch (Exception e) { exception = e; } //Assert Assert.IsNull(exception, $"Was not expecting an exception [{exception}]"); Assert.IsNotNull(locker, "Was expecting to have a locker"); Assert.AreEqual(1, locker.Keys.Count, "Was expecting to have 1 key"); Assert.AreEqual("somekey", locker.Keys.First().Key, "key name mismatch"); Assert.AreEqual("somevalue", locker.Keys.First().Value, "value name mismatch"); }
protected async Task <IActionResult> AddAsync(AddArguments <TEntity> arguments) { var entry = await this.context.AddAsync(arguments.Entity); await this.context.SaveChangesAsync(); return(await arguments.GetAction(entry.Entity.Id)); }
public async Task AddAsyncAddsBookToDataStoreAndReturnsId() { var context = Substitute.For <BookContext>(new DbContextOptions <BookContext>()); context.Books = Enumerable.Empty <BookEntity>().AsQueryable().BuildMockDbSet(); var service = GetBookService(context); var arguments = new AddArguments(new BookDetail("title", "author", "series"), new BookInformation(Rating.FourStars, "description", new Image("type", new byte[] { 1 }))); context.Books.AddAsync(Arg.Any <BookEntity>()) .ReturnsForAnyArgs((EntityEntry <BookEntity>) default)
public void Add(AddArguments arguments) { configuration.Upsert(arguments.ToEntryName(), arguments.ToModel()); if (arguments.Use) { configuration.UseApp(arguments.Name); log.WriteLine("> App added and selected."); } else { log.WriteLine("> App added."); } }
private async Task <object> ParseAddArguments(string[] arguments, Message message) { var args = new AddArguments(); var optionSet = new OptionSet(); var help = false; string error = null; optionSet.Add("?", "help", h => help = h != null); args.Items = optionSet.Parse(arguments).Select(s => s.ToSentenceCase()).ToList(); // Checking if at least one item is supplied if (!args.Items.Any()) { error = "At least one item id or name must be given."; } // Showing help if (help || error != null) { string m = "```"; // Adding error if (error != null) { m += $"Error:\n{error}\n\n"; } m += "Usage:\n" + "!item add <item-id | item-name>```"; await message.Channel.SendMessage(m); return(false); } return(args); }
private async Task AddItem(AddArguments args, Message m) { using (Database db = new Database()) { item item; var message = ""; // Removing item already in the DB for (var i = args.Items.Count() - 1; i >= 0; i--) { string itemReadable = args.Items.ElementAt(i); int temp; // Checking if the string is an int if (int.TryParse(itemReadable, out temp)) { item = await db.items.FindAsync(temp); } else { item = await db.items.FirstOrDefaultAsync(it => it.Name == itemReadable); } // Checking if item is in DB if (item != null) { message += $"**{item.Name}** already exists in the database.\n"; args.Items.Remove(itemReadable); } } var tasks = new Task <item> [args.Items.Count]; // Fetching item data for (var i = 0; i < args.Items.Count; i++) { tasks[i] = RSUtil.GetItemForDynamic(args.Items.ElementAt(i)); } // Waiting for all items to complete await Task.Run(() => { try { Task.WaitAll(tasks); } catch (Exception) { } }); // Adding items foreach (var t in tasks) { // Skipping if task errored if (t.Status != TaskStatus.RanToCompletion) { continue; } item = t.Result; db.items.Add(item); message += $"**{item.Name}** has added to the database.\n"; // Removing completed from array args.Items.Remove(item.Name); args.Items.Remove(item.Id.ToString()); } // Outputting message for those that errors foreach (var s in args.Items) { message += $"**{s}** could not be added to the database.\n"; } // Saving if (await db.SaveChangesAsync() < 0) { message += "There was an error saving the items to the database.\n"; } await m.Channel.SendMessage(message.Substring(0, message.Length - 1)); } }
public void Add(AddArguments arguments) { Configuration.Upsert(arguments.ToEntryName(), arguments.ToModel()); Console.WriteLine("> App added."); }
private async Task AddRecipe(AddArguments a, Message message) { using (var db = new Database()) { // Checking user autorization if (message.User.Id != 136856172203474944 && await db.users.FindAsync(message.User.Id) == null) { await message.Channel.SendMessage("You do not have permission to use that command."); return; } // Getting items foreach (var stack in a.Inputs) { await stack.GetItemFromDb(db); } var snowflake = TimeUtil.GenerateSnowflake(0, (ushort)(message.User.Id % 4095)); // Doing the stuff, the good stuff await db.Database.Transaction(async() => { var recipe = new recipe(); var itemVoid = new item { Id = 0 }; // Putting in dummy for inputs and putputs if (!a.Inputs.Any()) { a.Inputs.Add(new AddArguments.Stack { Item = itemVoid, Quantity = 0 }); } if (!a.Outputs.Any()) { a.Outputs.Add(new AddArguments.Stack { Item = itemVoid, Quantity = 0 }); } foreach (var i in a.Inputs) { recipe.inputs.Add(new input { ItemId = i.Item.Id, recipe = recipe, Quantity = i.Quantity }); } foreach (var o in a.Outputs) { recipe.outputs.Add(new output { ItemId = o.Item.Id, recipe = recipe, Quantity = o.Quantity }); } recipe.Id = snowflake; recipe.UserId = message.User.Id; recipe.Name = a.Name; recipe.Exp = a.Exp; recipe.Units = a.Units; recipe.Level = a.Level; recipe.Skill = (sbyte)a.Type; recipe.Extra = a.Extra; db.recipes.Add(recipe); if (await db.SaveChangesAsync() <= 0) { throw new Exception("Recipe could not be saved."); } }); await message.Channel.SendMessage($"Recipe **{a.Name}** has been successfully added."); } }
private async Task <object> ParseAddArguments(string[] args, Message message) { AddArguments a = new AddArguments { Inputs = new List <AddArguments.Stack>(), Outputs = new List <AddArguments.Stack>() }; FluentCommandLineParser parser = new FluentCommandLineParser(); bool helpCalled = false; Action <string, string> transformer = (type, s) => { // Checking if input is formatted correctly if (!s.Contains(":")) { throw new FormatException($"{type} not formatted correctly <itemName|itemId>:<quantity>"); } // Parsing input string[] t = s.Split(':'); int qty = int.Parse(t[1]); string item = t[0].ToSentenceCase(); if (qty < 1) { throw new Exception("Quantity must be greater than 0."); } a.Inputs.Add(new AddArguments.Stack { Id = item, Quantity = qty }); }; parser.Setup <string>('n', "name") .Required() .Callback(n => { // Validating if (n.Trim().Length < 4) { throw new Exception("Name must have at more than 4 characters"); } a.Name = n; }); parser.Setup <double>('e', "exp") .Required() .Callback(e => { // Validating if (e < 0) { throw new Exception("Exp must be greater than 0."); } a.Exp = e; }); parser.Setup <double>('u', "units") .Required() .Callback(u => { // Validating if (u < 0) { throw new Exception("Units must be greater than 0."); } a.Units = u; }); parser.Setup <int>('l', "level") .Required() .Callback(l => { // Validating if (l < 1 || l > 120) { throw new Exception("Level must be between 1 and 120."); } a.Level = (sbyte)l; }); parser.Setup <AddArguments.Skill>('s', "skill") .Required() .Callback(s => a.Type = s); parser.Setup <int>('E', "extra") .SetDefault(0) .Callback(c => { // Validating if (c < 0) { throw new Exception("Extra must be greater than -1."); } a.Extra = c; }); parser.Setup <List <string> >('i', "input") .Callback(i => i.ForEach(input => transformer("Input", input))); parser.Setup <List <string> >('o', "output") .Callback(i => i.ForEach(output => transformer("Output", output))); parser.Setup <bool>('?', "help") .Callback(h => helpCalled = h); var r = parser.Parse(args); if (r.HasErrors || helpCalled) { if (!helpCalled) { await message.Channel.SendMessage($"**Error:**```{r.ErrorText}```"); return(false); } var m = ""; m += "**Usage:**" + "```!recipe add <-n|name> <-e|exp> <-l|level> <-u|units> <-s|skill> [-E|extra] [-i|input] [-o|output]```\n" + "**Options:**\n" + "`-n`, `--name` **REQUIRED** [string]\n" + "The name of the training method. Usually named after the output.\n" + "\n" + "`-e`, `--exp` **REQUIRED** [double (> 0)]" + "The amount of exp this recipe produces.\n" + "\n" + "`-l`, `--level` **REQUIRED** [int (1-120)]\n" + "The level required to do this recipe.\n" + "\n" + "`-u`, `--units` **REQUIRED** [double (> 0)]\n" + "The about of times this recipe can be done in one hour. Fractions are allowed.\n" + "\n" + "`-s`, `--skill` **REQUIRED** [enum]\n" + "The skill the recipe is for.\n" + "\n" + "`-E`, `--extra` *OPTIONAL* [int (> -1)]\n" + "Any extra cost the recipe might have from items that are not tradable but buyable from a shop\n" + "like cleansing crystals.\n" + "\n" + "`-i`, `--input` *OPTIONAL* [custom]\n" + "A list of inputs for the recipe if there are any. Format: <itemName|itemId>:<quantity>\n" + "\tEg. --input \"Oak logs:1\" \"Yew logs:2\" would make the recipe required 1 oak log and 2 yew logs.\n" + "\n" + "`-o`, `--output` *OPTIONAL* [custom]\n" + "A list of outputs for the recipe if there are any. Format <itemName|itemId>:<quantity>\n" + "\tEg. --output \"Oak logs:1\" \"Yew logs:2\" would make the recipe output 1 oak log and 2 yew logs.\n"; await message.Channel.SendMessage(m); return(false); } return(a); }