public async Task Test04() { // Prepare a well formed entity var dtoForSave = new UnitForSave { Name = "KG", Name2 = "كج", Description = "Kilogram", Description2 = "كيلوجرام", Code = "kg", UnitType = "Mass", BaseAmount = 1, UnitAmount = 1 }; // Save it var dtosForSave = new List <UnitForSave> { dtoForSave }; var response = await Client.PostAsJsonAsync(Url, dtosForSave); // Assert that the response status code is a happy 200 OK Output.WriteLine(await response.Content.ReadAsStringAsync()); Assert.Equal(HttpStatusCode.OK, response.StatusCode); // Assert that the response is well-formed singleton of Unit var responseData = await response.Content.ReadAsAsync <EntitiesResponse <Unit> >(); Assert.Single(responseData.Result); // Assert that the result matches the saved entity Assert.Equal("Unit", responseData.CollectionName); // Retreve the entity from the entities var responseDto = responseData.Result.SingleOrDefault(); Assert.NotNull(responseDto?.Id); Assert.Equal(dtoForSave.Name, responseDto.Name); Assert.Equal(dtoForSave.Name2, responseDto.Name2); Assert.Equal(dtoForSave.Code, responseDto.Code); Assert.Equal(dtoForSave.UnitType, responseDto.UnitType); Assert.Equal(dtoForSave.BaseAmount, responseDto.BaseAmount); Assert.Equal(dtoForSave.UnitAmount, responseDto.UnitAmount); Shared.Set("Unit_kg", responseDto); }
public async Task Test07() { // Prepare a DTO for save, that contains leading and // trailing spaces in some string properties var dtoForSave = new UnitForSave { Name = " KM", // Leading space Name2 = "كم", Code = "km ", // Trailing space Description = "كيلومتر", Description2 = "Kilometer", UnitType = "Mass", BaseAmount = 1, UnitAmount = 1 }; // Call the API var response = await Client.PostAsJsonAsync(Url, new List <UnitForSave> { dtoForSave }); Output.WriteLine(await response.Content.ReadAsStringAsync()); // Confirm that the response is well-formed var responseData = await response.Content.ReadAsAsync <EntitiesResponse <Unit> >(); var responseDto = responseData.Result.FirstOrDefault(); // Confirm the entity was saved Assert.NotEqual(0, responseDto.Id); // Confirm that the leading and trailing spaces have been trimmed Assert.Equal(dtoForSave.Name?.Trim(), responseDto.Name); Assert.Equal(dtoForSave.Code?.Trim(), responseDto.Code); // share the entity, for the subsequent delete test Shared.Set("Unit_km", responseDto); }
public async Task Scenario01() { try { #region Deployment Experiment //// https://stackoverflow.com/questions/10438258/using-microsoft-build-evaluation-to-publish-a-database-project-sqlproj //const string projectPath = ""; //const string connString = ""; ////This Snapshot should be created by our build process using MSDeploy //const string snapshotPath = ""; //var project = ProjectCollection.GlobalProjectCollection.LoadProject(projectPath); //project.Build(); //DacServices dbServices = new DacServices(connString); //DacPackage dbPackage = DacPackage.Load(snapshotPath); //DacDeployOptions dbDeployOptions = new DacDeployOptions(); ////Cut out a lot of options here for configuring deployment, but are all part of DacDeployOptions //dbDeployOptions.SqlCommandVariableValues.Add("debug", "false"); //string dbName = "Tellma.101"; //dbServices.Deploy(dbPackage, dbName, true, dbDeployOptions); #endregion #region Access Token //var tokenResponse = await Client.RequestPasswordTokenAsync(new PasswordTokenRequest //{ // Address = "/connect/token", // ClientId = Services.Utilities.Constants.WebClientName, // ClientSecret = "top-secret", // Scope = Services.Utilities.Constants.ApiResourceName, // What about the others? // UserName = "******", // Password = "******" //}); var tokenResponse = await Client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = "/connect/token", ClientId = "m2m-5d3f198c-287b-49e9-bf7c-5879b6f2a4d8", ClientSecret = "aced45ff3450ff81afb9c73492e069fbc8cd92faae113635ef1fae766b6e4591", Scope = Services.Utilities.Constants.ApiResourceName, }); Assert.False(tokenResponse.IsError, $"Admin authentication failed, Error: {tokenResponse.Error}."); var accessToken = tokenResponse.AccessToken; Assert.NotNull(accessToken); #endregion // Call the protected API var accessTokenFactory = new StaticAccessTokenFactory(accessToken); var client = new TellmaClient(Client, accessTokenFactory); const int totalCount = 34; var clientApp = client.Application(tenantId: 201); #region GetEntities { var response = await clientApp .Units .GetEntities(new GetArguments { Select = $"{nameof(Unit.Name)},{nameof(Unit.CreatedBy)}.{nameof(User.Name)}", OrderBy = nameof(Unit.Id), Top = 5, CountEntities = true }); Assert.Equal(totalCount, response.Count); Assert.NotNull(response.Data); Assert.Equal(5, response.Data.Count); var unit = response.Data[0]; Assert.NotNull(unit.CreatedBy); Assert.Equal("Mohamad Akra", unit.CreatedBy.Name); } #endregion #region GetFact { var response = await clientApp .Units .GetFact(new FactArguments { Select = $"{nameof(Unit.Name)},{nameof(Unit.CreatedBy)}.{nameof(User.Name)}", OrderBy = nameof(Unit.Id), Top = 5, CountEntities = true }); Assert.Equal(totalCount, response.Count); Assert.NotNull(response.Data); Assert.Equal(5, response.Data.Count); var row = response.Data[0]; Assert.Equal(2, row.Count); Assert.Equal("pure", row[0]); Assert.Equal("Mohamad Akra", row[1]); } #endregion #region Get Null { var response = await clientApp .Units .GetFact(new FactArguments { Select = $"null", Top = 1 }); Assert.NotNull(response.Data); var row = Assert.Single(response.Data); var datum = Assert.Single(row); Assert.Null(datum); } #endregion #region GetAggregate { var response = await clientApp .Units .GetAggregate(new GetAggregateArguments { Select = $"Count({nameof(Unit.Id)})", }); var rows = response.Data; var row = Assert.Single(rows); var datum = Assert.Single(row); Assert.Equal(totalCount, Convert.ToInt32(datum)); } #endregion #region GetById { var response = await clientApp .Units .GetById(1, new GetByIdArguments { Select = $"{nameof(Unit.Name)},{nameof(Unit.CreatedBy)}.{nameof(User.Name)}", }); var unit = response.Entity; Assert.Equal("pure", unit.Name); Assert.NotNull(unit.CreatedBy); Assert.Equal("Mohamad Akra", unit.CreatedBy.Name); } #endregion #region Save int id; { var unitForSave = new UnitForSave { Name = "ly", Description = "Lightyear", UnitType = UnitTypes.Distance, Code = "ly", BaseAmount = 1, UnitAmount = 1000 }; var response = await clientApp .Units .Save(new List <UnitForSave> { unitForSave }, new SaveArguments { Expand = nameof(Unit.CreatedBy), ReturnEntities = true }); var unit = Assert.Single(response.Data); Assert.Equal(unitForSave.Name, unit.Name); Assert.Equal(unitForSave.Description, unit.Description); Assert.Equal(unitForSave.UnitType, unit.UnitType); Assert.Equal(unitForSave.Code, unit.Code); Assert.Equal(unitForSave.BaseAmount, unit.BaseAmount); Assert.Equal(unitForSave.UnitAmount, unit.UnitAmount); Assert.NotNull(unit.CreatedBy); Assert.Equal("Integration Tests", unit.CreatedBy.Name); id = unit.Id; Assert.NotEqual(0, id); } #endregion #region Delete { await clientApp.Units.DeleteById(id); } #endregion { var response = await clientApp.Resources(definitionId : 41).GetById(id: 3429, new GetByIdArguments { }); Assert.NotNull(response.Entity); Assert.Equal("Raw Light Speckled Bean", response.Entity.Name); } { var user = await clientApp.Users.GetByIdForSave(1); Assert.NotEmpty(user.Roles); } { var docsClient = clientApp.Documents(definitionId: 29); var doc = await docsClient.GetByIdForSave(19431); Assert.NotEmpty(doc.Lines); doc.PostingDate = doc.PostingDate.Value.AddDays(-1); var response = await docsClient.Save(new List <DocumentForSave> { doc }); Assert.Null(response.Data); } { var response = await clientApp.Units.Deactivate(new List <int> { 41 }); Assert.Null(response.Data); } { var response = await clientApp.Units.Activate(new List <int> { 41 }, new ActivateArguments { ReturnEntities = true }); var unit = Assert.Single(response.Data); Assert.True(unit.IsActive); } //var settings = await response.Content.ReadAsAsync<Versioned<SettingsForClient>>(); //Assert.Equal("Soreti Trading", settings.Data.ShortCompanyName); } catch (TellmaException ex) { _output.WriteLine(ex.ToString()); throw; } }