Exemple #1
0
        public void CreateGroup(string name, int length)
        {
            if (_groupStore.Exists(x => x.Name == name) || _textStore.Exists(x => x.Name == name))
            {
                throw new ArgumentException($"Cannot create group because another item in the database already has the name {name}.");
            }
            IGroupModel model = _modelFactory.GetGroupModel(name, length);

            _groupStore.Add(model);
        }
        public void Initialize()
        {
            _modelFactory = new ModelFactory();

            var countsOne         = new int[] { 1, 2, 3, 4, 5 };
            var countsTwo         = new int[] { 7, 8, 9, 10, 11 };
            var countsThree       = new int[] { 100, 101, 102, 103, 104 };
            var countsWrongLength = new int[] { 55 };

            var groupCountsBadInitialization = new int[] { 7, 7, 7, 7, 7 };

            var countsWithQuotesOne         = _modelFactory.GetSingleCountModel(countsOne);
            var countsWithQuotesTwo         = _modelFactory.GetSingleCountModel(countsTwo);
            var countsWithQuotesThree       = _modelFactory.GetSingleCountModel(countsThree);
            var countsWithQuotesWrongLength = _modelFactory.GetSingleCountModel(countsWrongLength);

            var countsWithoutQuotesOne         = _modelFactory.GetSingleCountModel((int[])countsOne.Clone());
            var countsWithoutQuotesTwo         = _modelFactory.GetSingleCountModel((int[])countsTwo.Clone());
            var countsWithoutQuotesThree       = _modelFactory.GetSingleCountModel((int[])countsThree.Clone());
            var countsWithoutQuotesWrongLength = _modelFactory.GetSingleCountModel((int[])countsWrongLength.Clone());

            badCountInitialization = _modelFactory.GetSingleCountModel(groupCountsBadInitialization);

            var flexibleCountsOne         = _modelFactory.GetFlexibleCountModel(countsWithQuotesOne, countsWithoutQuotesOne);
            var flexibleCountsTwo         = _modelFactory.GetFlexibleCountModel(countsWithQuotesTwo, countsWithoutQuotesTwo);
            var flexibleCountsThree       = _modelFactory.GetFlexibleCountModel(countsWithQuotesThree, countsWithoutQuotesThree);
            var flexibleCountsWrongLength = _modelFactory.GetFlexibleCountModel(countsWithQuotesWrongLength, countsWithoutQuotesWrongLength);

            textOne         = _modelFactory.GetTextModel("text one", flexibleCountsOne);
            textTwo         = _modelFactory.GetTextModel("text two", flexibleCountsTwo);
            textThree       = _modelFactory.GetTextModel("text three", flexibleCountsThree);
            textWrongLength = _modelFactory.GetTextModel("text wrong length", flexibleCountsWrongLength);

            groupOne = _modelFactory.GetGroupModel("group one", 5);
            groupTwo = _modelFactory.GetGroupModel("group two", 5);
        }
        public IGroupModel GetOne(Expression <Func <Grouping, bool> > criteria)
        {
            Grouping group = _db.Groupings.FirstOrDefault(criteria);

            if (group == null)
            {
                return(null);
            }
            IGroupModel      output  = _modelFactory.GetGroupModel(group.Name, UniversalConstants.CountSize);
            IQueryable <int> TextIds = _db.Texts.Join(
                _db.Text_Grouping,
                text => text.Id,
                text_grouping => text_grouping.TextId,
                (text, text_grouping) => new { TextId = text_grouping.TextId, GroupingId = text_grouping.GroupingId }
                ).Where(x => x.GroupingId == group.Id)
                                       .Select(x => (int)x.TextId);

            foreach (Text text in _db.Texts.Where(x => TextIds.Contains((int)x.Id)))
            {
                ITextModel textModel = _textStore.GetOne(x => x.Id == text.Id);
                if (textModel == null)
                {
                    throw new ArgumentException("Cannot get group since it contains a reference to a nonexistant text.");
                }
                output.Add(textModel);
            }
            foreach (Grouping_Grouping groupGroup in _db.Grouping_Grouping.Where(x => x.ParentId == group.Id))
            {
                IGroupModel groupModel = GetOne(x => x.Id == groupGroup.ChildId);
                if (groupModel == null)
                {
                    throw new ArgumentException("Cannot get group since it contains a reference to a nonexistant child group.");
                }
                output.Add(groupModel);
            }
            return(output);
        }
        public void TestAdd()
        {
            string name;

            _uniqueNames.TryPop(out name);
            IGroupModel model = _modelFactory.GetGroupModel(name, UniversalConstants.CountSize);

            _groupStore.Add(model);
            Grouping result = _db.Groupings.FirstOrDefault(x => x.Name == name);

            Assert.IsNotNull(result);
            if (result != null)
            {
                _db.Groupings.Remove(result);
                _db.SaveChanges();
            }
        }