public void SentenceTaskRepositoryTest_SentenceTasks_getTasks_nullException()
 {
     try
     {
         rep.GetTasks(null);
     }
     catch (Exception e)
     {
         Assert.AreEqual("Value cannot be null.", e.Message, string.Format("result != expected"));
     }
 }
Esempio n. 2
0
        public SentenceTask GetTask(string category, string indexes)
        {
            var ParsedIndexes = new List <int>();
            List <SentenceTask> tasks;
            var rand = new Random();

            if (!String.IsNullOrEmpty(indexes))
            {
                foreach (var ch in indexes.Split(','))
                {
                    if (int.TryParse(ch, out int i))
                    {
                        ParsedIndexes.Add(i);
                    }
                }
            }

            if (String.IsNullOrEmpty(category))
            {
                throw new Exception("Invalid model");
            }

            if (!ParsedIndexes.IsCorrect())
            {
                throw new Exception("End of tasks");
            }

            tasks = rep.GetTasks(category, ParsedIndexes).ToList();

            if (tasks == null)
            {
                throw new Exception("Has no tasks");
            }

            if (tasks.Count() == 0)
            {
                throw new Exception("Has no tasks");
            }

            var index = rand.Next(0, tasks.Count());

            return(tasks.Select(x =>
            {
                var arr = x.Sentence.Split(new[] { ' ' });
                var set = new HashSet <int>();
                while (set.Count() != arr.Length)
                {
                    set.Add(rand.Next(0, arr.Length));
                }
                var returnArr = new string[arr.Length];
                var i = 0;
                foreach (var ind in set)
                {
                    returnArr[i] = arr[ind];
                    i++;
                }
                return new SentenceTask {
                    SentenceTaskId = x.SentenceTaskId, Sentence = string.Join(" ", returnArr), Category = x.Category, Translate = x.Translate
                };
            }).ElementAt(index));
        }