Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #422 from kysect/test/subject-course-users
Browse files Browse the repository at this point in the history
test: subject course users
  • Loading branch information
FrediKats authored Sep 16, 2022
2 parents c5eac4d + bac788d commit fc32cd0
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Kysect.Shreks.Application.GithubWorkflow.Abstractions;
using Kysect.Shreks.Core.Extensions;
using Kysect.Shreks.Core.Specifications.Github;
using Kysect.Shreks.Application.DatabaseContextExtensions;
using Kysect.Shreks.Application.GithubWorkflow.Abstractions;
using Kysect.Shreks.Core.SubjectCourseAssociations;
using Kysect.Shreks.Core.UserAssociations;
using Kysect.Shreks.DataAccess.Abstractions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -36,12 +36,8 @@ public async Task UpdateOrganizations(CancellationToken cancellationToken)

foreach (GithubSubjectCourseAssociation subjectAssociation in githubSubjectCourseAssociations)
{
List<string> usernames = await _context
.SubjectCourseGroups
.WithSpecification(new GetSubjectCourseGithubUsers(subjectAssociation.SubjectCourse.Id))
.Select(association => association.GithubUsername)
.ToListAsync(cancellationToken);

IReadOnlyCollection<GithubUserAssociation> githubUserAssociations = await _context.SubjectCourses.GetAllGithubUsers(subjectAssociation.SubjectCourse.Id);
var usernames = githubUserAssociations.Select(a => a.GithubUsername).ToList();
await _inviteSender.Invite(subjectAssociation.GithubOrganizationName, usernames);
await GenerateRepositories(_repositoryManager, usernames, subjectAssociation.GithubOrganizationName, subjectAssociation.TemplateRepositoryName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Kysect.Shreks.Core.Study;
using Kysect.Shreks.Core.UserAssociations;
using Microsoft.EntityFrameworkCore;

namespace Kysect.Shreks.Application.DatabaseContextExtensions;

public static class GithubSubjectCourseAssociationExtensions
{
public static async Task<IReadOnlyCollection<GithubUserAssociation>> GetAllGithubUsers(this DbSet<SubjectCourse> subjectCourses, Guid subjectCourseId)
{
return await subjectCourses
.Where(sc => sc.Id == subjectCourseId)
.SelectMany(sc => sc.Groups)
.Select(g => g.StudentGroup)
.SelectMany(g => g.Students)
.Select(u => u.User)
.SelectMany(u => u.Associations)
.OfType<GithubUserAssociation>()
.ToListAsync();
}
}

This file was deleted.

11 changes: 11 additions & 0 deletions Source/Domain/Kysect.Shreks.Core/Users/StudentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Kysect.Shreks.Core.UserAssociations;

namespace Kysect.Shreks.Core.Users;

public static class StudentExtensions
{
public static GithubUserAssociation AddGithubAssociation(this Student student, string githubUsername)
{
return new GithubUserAssociation(student.User, githubUsername);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Kysect.Shreks.Seeding.DatabaseSeeders;

public class SubjectGroupAssociationSeeder : IDatabaseSeeder
{
private readonly IEntityGenerator<SubjectCourseAssociation> _generator;
private readonly IEntityGenerator<GithubSubjectCourseAssociation> _generator;

public SubjectGroupAssociationSeeder(IEntityGenerator<SubjectCourseAssociation> generator)
public SubjectGroupAssociationSeeder(IEntityGenerator<GithubSubjectCourseAssociation> generator)
{
_generator = generator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override SubjectCourse Generate(int index)
var subjectCount = _subjectGenerator.GeneratedEntities.Count;

var deadlineCount = _faker.Random.Int(0, _deadlinePolicyGenerator.GeneratedEntities.Count);

IEnumerable<DeadlinePolicy> deadlines = Enumerable.Range(0, deadlineCount)
.Select(_ => _faker.Random.Int(0, _deadlinePolicyGenerator.GeneratedEntities.Count - 1))
.Select(i => _deadlinePolicyGenerator.GeneratedEntities[i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace Kysect.Shreks.Seeding.EntityGenerators;

public class SubjectCourseAssociationGenerator : EntityGeneratorBase<SubjectCourseAssociation>
public class SubjectCourseAssociationGenerator : EntityGeneratorBase<GithubSubjectCourseAssociation>
{
private readonly IEntityGenerator<SubjectCourse> _subjectCourseGenerator;
private readonly Faker _faker;

public SubjectCourseAssociationGenerator(
EntityGeneratorOptions<SubjectCourseAssociation> options,
EntityGeneratorOptions<GithubSubjectCourseAssociation> options,
IEntityGenerator<SubjectCourse> subjectCourseGenerator,
Faker faker)
: base(options)
Expand All @@ -20,7 +20,7 @@ public SubjectCourseAssociationGenerator(
_faker = faker;
}

protected override SubjectCourseAssociation Generate(int index)
protected override GithubSubjectCourseAssociation Generate(int index)
{
var count = _subjectCourseGenerator.GeneratedEntities.Count;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using FluentAssertions;
using Kysect.Shreks.Application.DatabaseContextExtensions;
using Kysect.Shreks.Core.Study;
using Kysect.Shreks.Core.SubjectCourseAssociations;
using Kysect.Shreks.Core.Users;
using Kysect.Shreks.Seeding.EntityGenerators;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Kysect.Shreks.Tests.DataAccess;

public class GithubSubjectCourseAssociationTest : DataAccessTestBase
{
private readonly IEntityGenerator<GithubSubjectCourseAssociation> _subjectCourseAssociationGenerator;
private readonly IEntityGenerator<StudentGroup> _studentGroupGenerator;
private readonly IEntityGenerator<User> _userGenerator;

public GithubSubjectCourseAssociationTest()
{
_subjectCourseAssociationGenerator = Provider.GetRequiredService<IEntityGenerator<GithubSubjectCourseAssociation>>();
_userGenerator = Provider.GetRequiredService<IEntityGenerator<User>>();
_studentGroupGenerator = Provider.GetRequiredService<IEntityGenerator<StudentGroup>>();
}

[Fact]
public async Task GetSubjectCourseGithubUser_StudentAssociationExists_AssociationShouldReturn()
{
// Arrange
string userAssociation = Guid.NewGuid().ToString();

User user = _userGenerator.Generate();
StudentGroup group = _studentGroupGenerator.Generate();
GithubSubjectCourseAssociation subjectCourseAssociation = _subjectCourseAssociationGenerator.Generate();

var student = new Student(user, group);
student.AddGithubAssociation(userAssociation);
subjectCourseAssociation.SubjectCourse.AddGroup(group);
group.AddStudent(student);

// Act
await Context.Users.AddAsync(user);
await Context.Students.AddAsync(student);
await Context.StudentGroups.AddAsync(group);
await Context.SubjectCourseAssociations.AddAsync(subjectCourseAssociation);
await Context.SaveChangesAsync();

// Assert
IEnumerable<string> organizationUsers = Context
.SubjectCourses
.GetAllGithubUsers(subjectCourseAssociation.SubjectCourse.Id)
.Result
.Select(a => a.GithubUsername);

organizationUsers.Should().Contain(userAssociation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Kysect.Shreks.Tests.DataAccess;

public class SubjectCourseAssociationTest : DataAccessTestBase
{
private readonly IEntityGenerator<SubjectCourseAssociation> _subjectCourseAssociationGenerator;
private readonly IEntityGenerator<GithubSubjectCourseAssociation> _subjectCourseAssociationGenerator;

public SubjectCourseAssociationTest()
{
_subjectCourseAssociationGenerator = Provider.GetRequiredService<IEntityGenerator<SubjectCourseAssociation>>();
_subjectCourseAssociationGenerator = Provider.GetRequiredService<IEntityGenerator<GithubSubjectCourseAssociation>>();
}

[Fact]
public async Task SaveChangesAsync_EntityAdded_NoExceptionThrown()
{
Expand Down

0 comments on commit fc32cd0

Please sign in to comment.