Пример #1
0
        public void Should_CRUD_correctly()
        {
            var user           = new User("email", "Ivan", UserType.Candidate);
            var specialization = new Specialization("Specialization");
            var skills         = new List <Skill> {
                new Skill("Skill1"), new Skill("Skill2"), new Skill("Skill3")
            };


            var options = new DbContextOptionsBuilder()
                          .UseInMemoryDatabase("JinderTest")
                          .Options;

            using var context = new JinderContext(options);

            var dbUser           = DbUser.FromModel(user);
            var dbSpecialization = DbSpecialization.FromModel(specialization);
            var dbSkills         = skills.Select(DbSkill.FromModel).ToList();

            context.Users.Add(dbUser);
            context.Specializations.Add(dbSpecialization);
            context.Skills.AddRange(dbSkills);
            context.SaveChanges();

            user           = context.Users.SingleOrDefault()?.ToModel();
            specialization = context.Specializations.SingleOrDefault()?.ToModel();
            skills         = context.Skills.Select(s => s.ToModel()).ToList();

            Assert.IsNotNull(user);
            Assert.IsNotNull(specialization);
            Assert.IsNotNull(skills.Aggregate((a, b) => (a is null) || (b is null) ? null : a));

            var summary   = new Summary(user, specialization, skills, "Information");
            var dbSummary = DbSummary.FromModel(summary);

            context.Summaries.Add(dbSummary);
            context.SaveChanges();

            summary = context.Summaries.SingleOrDefault()?.ToModel();
            Assert.IsNotNull(summary);

            context.Summaries.Remove(dbSummary);
            context.SaveChanges();

            user           = context.Users.SingleOrDefault()?.ToModel();
            specialization = context.Specializations.SingleOrDefault()?.ToModel();
            skills         = context.Skills.Select(s => s.ToModel()).ToList();
            summary        = context.Summaries.SingleOrDefault()?.ToModel();

            Assert.IsNotNull(user);
            Assert.IsNotNull(specialization);
            Assert.IsNotNull(skills.Aggregate((a, b) => (a is null) || (b is null) ? null : a));
            Assert.IsNull(summary);

            summary   = new Summary(user, specialization, skills, "Information");
            dbSummary = DbSummary.FromModel(summary);

            Assert.DoesNotThrow(() => context.Summaries.Add(dbSummary));
        }
Пример #2
0
        public User Add(User user)
        {
            DbUser dbUser = DbUser.FromModel(user);

            dbUser = _context.Users
                     .Add(dbUser)
                     .Entity;
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw new ArgumentException("Unable to create user with such data!");
            }

            return(dbUser.ToModel());
        }