Untitled conversation
- User
{ [Collection("GraphQL")] public class EventExcludedArtistsTests { private readonly GraphQLHttpClient _graphQLClient; private readonly AreaDbHelper _areaDbHelper; private readonly UserDbHelper _userDbHelper; private readonly VenueDbHelper _venueDbHelper; private readonly ArtistDbHelper _artistDbHelper;
private readonly EventDbHelper _eventDbHelper; private readonly ExcludedArtistsDbHelper _excludedArtistDbHelper; private readonly IDbConnectionFactory _dbConnectionFactory; public EventExcludedArtistsTests(GraphQLFixture fixture) { var userId = CreateUser(fixture).Result; _graphQLClient = fixture.WebApplicationFactory .CreateGraphQLHttpClient() .SetAuthenticatedUser(userId) .SetHeader("Cache-Control", "no-cache"); _areaDbHelper = fixture.WebApplicationFactory.GetDbHelper<AreaDbHelper>(); _userDbHelper = fixture.WebApplicationFactory.GetDbHelper<UserDbHelper>(); _venueDbHelper = fixture.WebApplicationFactory.GetDbHelper<VenueDbHelper>(); _artistDbHelper = fixture.WebApplicationFactory.GetDbHelper<ArtistDbHelper>(); _eventDbHelper = fixture.WebApplicationFactory.GetDbHelper<EventDbHelper>(); _excludedArtistDbHelper = fixture.WebApplicationFactory.GetDbHelper<ExcludedArtistsDbHelper>(); _dbConnectionFactory = fixture.WebApplicationFactory.DbConnectionFactory; } private async Task<int> CreateUser(GraphQLFixture fixture) { var userDbHelper = fixture.WebApplicationFactory.GetDbHelper<UserDbHelper>(); var areaDbHelper = fixture.WebApplicationFactory.GetDbHelper<AreaDbHelper>(); var area = await areaDbHelper.Insert(); var user = await userDbHelper.Insert( user => user.WithEventAccess(EventAccessLevelType.StandardUser).WithAreaId(area.Id) ); return user.Id; } [Fact] public async Task ExcludedArtistsReturnedOnEvent() { // Arrange var venueId = await InsertPrequisiteData(); var artist = await _artistDbHelper.Insert(); var excludedArtist = await _artistDbHelper.Insert(); // Create event with the artists var eventId = await InsertEventInDb(venueId, GenerateLineup(artist, excludedArtist)); // Exclude exludedArtist await _excludedArtistDbHelper.InsertExcludedArtist(excludedArtist.Id, eventId); // Act - get the event var @event = await GetEvent(eventId); // Assert var excludedArtistActual = Assert.Single(@event.ExcludedArtists); Assert.Equal(excludedArtist.Id.ToString(), excludedArtistActual.Id); } [Fact] public async Task ExcludedArtistsFilteredFromLineupAndRevisionsLineup() { // Arrange var venueId = await InsertPrequisiteData(); var artist = await _artistDbHelper.Insert(); var excludedArtist = await _artistDbHelper.Insert(); string inputLineup = GenerateLineup(artist, excludedArtist); string expectedLineup = GenerateLineup(artist) + $" and {excludedArtist.Name}"; // Create event with the artists var eventId = await InsertEventInDb(venueId, inputLineup); // Exclude exludedArtist await _excludedArtistDbHelper.InsertExcludedArtist(excludedArtist.Id, eventId); // Act - get the event var @event = await GetEvent(eventId); // Assert Assert.Equal(expectedLineup, @event.Lineup); var revision = Assert.Single(@event.Revisions); Assert.Equal(HttpUtility.HtmlDecode(expectedLineup), revision.Event.Lineup); } [Fact] public async Task ExcludedArtistsFilteredFromEventArtistsAndRevisionsArtistsArrays() { // Arrange var venueId = await InsertPrequisiteData(); var includedArtist = await _artistDbHelper.Insert(); var excludedArtist = await _artistDbHelper.Insert(); string inputLineup = GenerateLineup(includedArtist, excludedArtist); // Create event with the artists var eventId = await InsertEventInDb(venueId, inputLineup); // Exclude exludedArtist await _excludedArtistDbHelper.InsertExcludedArtist(excludedArtist.Id, eventId); // Act - get the event var @event = await GetEvent(eventId); // Assert var actualArtist = Assert.Single(@event.Artists); Assert.Equal(includedArtist.Id.ToString(), actualArtist.Id); var revision = Assert.Single(@event.Revisions); var revisionArtist = Assert.Single(revision.Event.Artists); Assert.Equal(includedArtist.Id.ToString(), revisionArtist.Id); } [Fact] public async Task ArtistsRemainExcludedWhenEventRevisionIsSet() { // Arrange var venueId = await InsertPrequisiteData(); var includedArtist = await _artistDbHelper.Insert(); var excludedArtist = await _artistDbHelper.Insert(); string inputLineup = GenerateLineup(includedArtist, excludedArtist); // Create event with the artists int eventId = await InsertEventInDb(venueId, inputLineup); // Exclude exludedArtist await _excludedArtistDbHelper.InsertExcludedArtist(excludedArtist.Id, eventId); var @event = await GetEvent(eventId); // Event revision var eventRevisionInput = new EventInputDto { VenueId = venueId, Title = "Event Tile Updated", StartTime = new DateTime(2022, 2, 22), EndTime = new DateTime(2022, 2, 23), Lineup = inputLineup, Visibility = VisibilityType.Public }; // Act await EventRevisionMutationTestHelpers.CreateEventRevisionMutation( _graphQLClient, eventRevisionInput, int.Parse(@event.Id) ); // Assert @event = await GetEvent(eventId); Assert.Equal(2, @event.Revisions.Count()); Assert.Collection( @event.Revisions, item1 => { var a1 = Assert.Single(item1.Event.Artists); Assert.Equal(includedArtist.Id.ToString(), a1.Id); }, item2 => { var a2 = Assert.Single(item2.Event.Artists); Assert.Equal(includedArtist.Id.ToString(), a2.Id); } ); // Check the DB too var eventArtists = await DbHelper.GetAsync<TblEventArtist>( _dbConnectionFactory, t => t.EventId == eventId ); var eventArtistInDb = Assert.Single(eventArtists); Assert.Equal(includedArtist.Id, eventArtistInDb.DJID); } private async Task<EventResponse> GetEvent(int eventId) { var request = GetGraphQLRequest(eventId); var eventResponse = await _graphQLClient.SendQueryAsync( request, () => new { eventResponse = new EventResponse() } ); eventResponse.AssertGraphQLWasSuccessful(); return eventResponse.Data.eventResponse; } private static GraphQLRequest GetGraphQLRequest(int eventId) => new GraphQLRequest { Query = @" query GET_EVENT($eventId: ID!) { eventResponse: event(id: $eventId) { id lineup artists { id name } excludedArtists { id name } revisions { id event { lineup artists { id } } } } } ", Variables = new { eventId } }; private async Task<int> InsertEventInDb(int venueId, string lineup) { var eventInputDto = new EventInputDto { VenueId = venueId, Title = "Event Tile", StartTime = new DateTime(2022, 2, 22), EndTime = new DateTime(2022, 2, 23), Lineup = lineup, SubmissionSource = EventSubmissionSource.SubmitEventPage, Visibility = VisibilityType.Public, }; var @event = await EventMutationTestHelpers.CreateEventMutation( _graphQLClient, eventInputDto ); return int.Parse(@event.Data.Id); } private async Task<int> InsertPrequisiteData() { var areaDto = await _areaDbHelper.Insert(); var userDto = await _userDbHelper.Insert(options => options.WithAreaId(areaDto.Id)); var venueDto = await _venueDbHelper.Insert( options => options.WithUserId(userDto.Id).WithAreaId(areaDto.Id) ); return venueDto.Id; } private static string GenerateLineup(params ArtistDto[] artists) => string.Join(" and ", artists.Select(a => $"<artist id=\"{a.Id}\">{a.Name}</artist>")); private class EventResponse : IdResponse { public string Lineup { get; set; } public IEnumerable<ArtistResponse> Artists { get; set; } public IEnumerable<ArtistResponse> ExcludedArtists { get; set; } public IEnumerable<RevisionResponse> Revisions { get; set; } } private class RevisionResponse : IdResponse { public EventResponse Event { get; set; } } private class ArtistResponse : IdResponse { public string Name { get; set; } } private class IdResponse { public string Id { get; set; } } [Table("tblEventsArtist")] private class TblEventArtist { public int EventId { get; set; } public int DJID { get; set; } }} }
namespace RA.GraphQL.Integration.Tests.Helpers.Database { public class EventDbHelper : BaseDbHelper { private readonly EventFaker _faker;
public EventDbHelper(IDbConnectionFactory dbConnectionFactory) : base(dbConnectionFactory) { _faker = new EventFaker(); } public Task<EventDto> Insert(Func<EventBuilder, EventBuilder> builderOptions) { var eventBuilder = builderOptions.Invoke(_faker.Builder()); return DbHelper.Insert( _dbConnectionFactory, eventBuilder.Generate(), (messageUser, id) => messageUser.Id = id ); } public async Task<EventDto> GetEventDto(int eventId) { var events = await DbHelper.GetAsync<EventDto>(_dbConnectionFactory, x => x.Id == eventId); return events.FirstOrDefault(); }} }
namespace RA.GraphQL.Integration.Tests.Fakers { public class EventFaker : Faker { private static DateTime StartTime = new DateTime(2021, 1, 1, 22, 0, 0); private static DateTime EndTime = StartTime.AddHours(8);
public EventFaker() { // Randomly Generated RuleFor(e => e.CountryId, f => f.Random.Byte()); RuleFor(e => e.AreaId, f => (short)f.Random.Number()); RuleFor(e => e.Date, f => f.Date.Soon()); RuleFor(e => e.Title, f => f.Lorem.Sentence(4)); RuleFor(e => e.Lineup, f => f.Lorem.Sentences()); RuleFor(e => e.Cost, f => f.Commerce.Price()); RuleFor(e => e.Content, f => f.Lorem.Sentences()); RuleFor(e => e.WebsiteOneUrl, f => f.Internet.Url()); RuleFor(e => e.WebsiteOneTitle, f => f.Company.CompanyName()); RuleFor(e => e.FlyerFront, f => f.Internet.Url()); RuleFor(e => e.FlyerBack, f => f.Internet.Url()); RuleFor(e => e.DatePosted, f => f.Date.Past()); RuleFor(e => e.TicketFeeType, f => f.Random.Enum<TicketFeeType>()); // Static RuleFor(e => e.Live, true); RuleFor(e => e.StartTime, StartTime); RuleFor(e => e.EndTime, EndTime); RuleFor(e => e.NewEventForm, false); RuleFor(e => e.SubmissionSource, EventSubmissionSource.SubmitEventPage); RuleFor(e => e.Visibility, VisibilityType.Public); RuleFor(e => e.ApprovalStatus, ApprovalStatusType.Pending); RuleFor(e => e.TicketingSystem, TicketingSystem.Legacy); } public EventBuilder Builder() { return new EventBuilder(this); }}
public class EventBuilder { private EventFaker _faker; private int _venueId; private int _adminUserId; private DateTime? _eventDate; private DateTime? _startTime; private DateTime? _endTime; private bool _lock; private byte? _minimumAge; private short _areaId; private string _cost; private string _content; private bool _newEventForm; private string _lineup; private int _parentEventId; private TicketFeeType? _ticketFeeTypeEnum; private byte _countryId; private DateTime? _embargoDate; private TicketingSystem? _ticketingSystem;
public EventBuilder(EventFaker faker) { _faker = faker; } public EventBuilder WithVenueId(int venueId) { _venueId = venueId; return this; } public EventBuilder WithAdminUserId(int adminUserId) { _adminUserId = adminUserId; return this; } public EventBuilder WithDate(DateTime eventDate) { _eventDate = eventDate; return this; } public EventBuilder WithStartAndEnd(DateTime startTime, DateTime endTime) { _startTime = startTime; _endTime = endTime; return this; } public EventBuilder WithLocked(bool lockFlag) { _lock = lockFlag; return this; } public EventBuilder WithMinimumAge(byte? minimumAge) { _minimumAge = minimumAge; return this; } public EventBuilder WithCost(string cost) { _cost = cost; return this; } public EventBuilder WithContent(string content) { _content = content; return this; } public EventBuilder WithAreaId(short areaId) { _areaId = areaId; return this; } public EventBuilder WithNewEventFormFlag(bool newEventForm) { _newEventForm = newEventForm; return this; } public EventBuilder WithLineup(string lineup) { _lineup = lineup; return this; } public EventBuilder WithEventParentId(int parentEventId) { _parentEventId = parentEventId; return this; } public EventBuilder WithTicketFeeType(TicketFeeType ticketFeeTypeEnum) { _ticketFeeTypeEnum = ticketFeeTypeEnum; return this; } public EventBuilder WithCountryId(byte countryId) { _countryId = countryId; return this; } public EventBuilder WithEmbargoDate(DateTime embargoDate) { _embargoDate = embargoDate; return this; } public EventBuilder WithTicketingSystem(TicketingSystem ticketingSystem) { _ticketingSystem = ticketingSystem; return this; } public EventDto Generate() { var eventDto = _faker.Generate(); if (_eventDate.HasValue) eventDto.Date = _eventDate.Value; if (_startTime.HasValue) eventDto.StartTime = _startTime.Value; if (_endTime.HasValue) eventDto.EndTime = _endTime.Value; if (_ticketFeeTypeEnum.HasValue) eventDto.TicketFeeType = _ticketFeeTypeEnum.Value; eventDto.AdminUserId = _adminUserId; eventDto.VenueId = _venueId; eventDto.Lock = _lock; eventDto.AreaId = _areaId; eventDto.NewEventForm = _newEventForm; eventDto.Lineup = _lineup; eventDto.CountryId = _countryId; if (!string.IsNullOrEmpty(_cost)) eventDto.Cost = _cost; if (_parentEventId != 0) eventDto.ParentEventId = _parentEventId; if (_embargoDate.HasValue) eventDto.EmbargoDate = _embargoDate.Value; if (_ticketingSystem.HasValue) eventDto.TicketingSystem = _ticketingSystem.Value; return eventDto; }} }
I have the EventDbHelper and EventFaker, inside the test ArtistsRemainExcludedWhenEventRevisionIsSet I would like to use these so that DatePosted is populated in the event object and my test stops failing
- Assistant
To ensure that the
DatePostedproperty is populated in your event object and to prevent the testArtistsRemainExcludedWhenEventRevisionIsSetfrom failing, you can leverage theEventDbHelperandEventFaker. This approach allows you to directly insert an event with all necessary properties, includingDatePosted, into the database before running your test assertions.Here's a step-by-step guide on how to modify your test to achieve this:
1. Modify the
InsertEventInDbMethodInstead of using the
InsertEventInDbmethod that relies on GraphQL mutations, you can create a new method (e.g.,InsertEventDirectly) that utilizes theEventDbHelperandEventFakerto insert the event directly into the database. This ensures that all properties, includingDatePosted, are correctly populated.private async Task<int> InsertEventDirectly(int venueId, string lineup) { var eventDto = new EventFaker() .RuleFor(e => e.VenueId, venueId) .RuleFor(e => e.Lineup, lineup) .RuleFor(e => e.DatePosted, f => f.Date.Past()) // Ensure DatePosted is set .Generate(); var insertedEvent = await _eventDbHelper.Insert(e => e .WithVenueId(eventDto.VenueId) .WithLineup(eventDto.Lineup) .WithDatePosted(eventDto.DatePosted) // Set DatePosted using builder ); return insertedEvent.Id; }2. Update the
EventBuilderto IncludeWithDatePostedEnsure that your
EventBuilderwithin theEventFakerhas a method to set theDatePostedproperty:public class EventBuilder { // Existing properties... private DateTime? _datePosted; // Existing methods... public EventBuilder WithDatePosted(DateTime datePosted) { _datePosted = datePosted; return this; } public EventDto Generate() { var eventDto = _faker.Generate(); // Existing conditional assignments... if (_datePosted.HasValue) eventDto.DatePosted = _datePosted.Value; return eventDto; } }3. Update the Test Method
Modify your
ArtistsRemainExcludedWhenEventRevisionIsSettest to use the newInsertEventDirectlymethod. This ensures that the event inserted into the database has theDatePostedproperty set, which should prevent the test from failing due to this missing property.[Fact] public async Task ArtistsRemainExcludedWhenEventRevisionIsSet() { // Arrange var venueId = await InsertPrequisiteData(); var includedArtist = await _artistDbHelper.Insert(); var excludedArtist = await _artistDbHelper.Insert(); string inputLineup = GenerateLineup(includedArtist, excludedArtist); // Create event directly using EventDbHelper to ensure DatePosted is populated int eventId = await InsertEventDirectly(venueId, inputLineup); // Exclude excludedArtist await _excludedArtistDbHelper.InsertExcludedArtist(excludedArtist.Id, eventId); var @event = await GetEvent(eventId); // Event revision var eventRevisionInput = new EventInputDto { VenueId = venueId, Title = "Event Title Updated", StartTime = new DateTime(2022, 2, 22), EndTime = new DateTime(2022, 2, 23), Lineup = inputLineup, Visibility = VisibilityType.Public }; // Act await EventRevisionMutationTestHelpers.CreateEventRevisionMutation( _graphQLClient, eventRevisionInput, int.Parse(@event.Id) ); // Assert @event = await GetEvent(eventId); Assert.Equal(2, @event.Revisions.Count()); Assert.Collection( @event.Revisions, item1 => { var a1 = Assert.Single(item1.Event.Artists); Assert.Equal(includedArtist.Id.ToString(), a1.Id); }, item2 => { var a2 = Assert.Single(item2.Event.Artists); Assert.Equal(includedArtist.Id.ToString(), a2.Id); } ); // Check the DB too var eventArtists = await DbHelper.GetAsync<TblEventArtist>( _dbConnectionFactory, t => t.EventId == eventId ); var eventArtistInDb = Assert.Single(eventArtists); Assert.Equal(includedArtist.Id, eventArtistInDb.DJID); // Optionally, verify DatePosted is set var eventDto = await _eventDbHelper.GetEventDto(eventId); Assert.NotNull(eventDto.DatePosted); }4. Ensure
EventResponseIncludesDatePosted(Optional)If your assertions or other test logic depend on the
DatePostedproperty, ensure that yourEventResponseincludes it. Modify theGetGraphQLRequestmethod to requestDatePosted:private static GraphQLRequest GetGraphQLRequest(int eventId) => new GraphQLRequest { Query = @" query GET_EVENT($eventId: ID!) { eventResponse: event(id: $eventId) { id lineup datePosted // Add this line artists { id name } excludedArtists { id name } revisions { id event { lineup artists { id } } } } } ", Variables = new { eventId } };Also, update the
EventResponseclass to includeDatePosted:private class EventResponse : IdResponse { public string Lineup { get; set; } public DateTime DatePosted { get; set; } // Add this property public IEnumerable<ArtistResponse> Artists { get; set; } public IEnumerable<ArtistResponse> ExcludedArtists { get; set; } public IEnumerable<RevisionResponse> Revisions { get; set; } }5. Verify in the Database (Optional)
To ensure that
DatePostedis correctly set in the database, you can add an assertion at the end of your test:// Verify DatePosted in the database var eventDto = await _eventDbHelper.GetEventDto(eventId); Assert.NotNull(eventDto.DatePosted); Assert.True(eventDto.DatePosted <= DateTime.Now);6. Summary
By directly inserting the event using
EventDbHelperand ensuring thatDatePostedis populated via theEventFaker, you eliminate the dependency on GraphQL mutations for this specific property. This approach ensures that your tests are more reliable and that all necessary properties are correctly set before performing assertions.Here’s a concise summary of the changes:
- Create a Direct Insertion Method: Use
EventDbHelperandEventFakerto insert events with all required properties. - Update the
EventBuilder: Ensure the builder can setDatePosted. - Modify the Test Method: Use the new insertion method and verify
DatePosted. - Optional Enhancements: Include
DatePostedin your GraphQL queries and responses if needed.
Implementing these changes should resolve the issue with the
DatePostedproperty and ensure your test passes successfully. - Create a Direct Insertion Method: Use