|
1 | 1 | using System; |
2 | 2 | using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using JwtIdentity.Common.Helpers; |
3 | 5 | using JwtIdentity.Interfaces; |
4 | 6 | using JwtIdentity.Models; |
5 | 7 | using JwtIdentity.Services; |
@@ -46,5 +48,220 @@ public void GetSurvey_ReturnsNull_WhenGuidDoesNotExist() |
46 | 48 | var result = _service.GetSurvey("notfound"); |
47 | 49 | Assert.That(result, Is.Null); |
48 | 50 | } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public async Task ValidateSurveyForPublishing_ReturnsValid_WhenNoQuestionGroups() |
| 54 | + { |
| 55 | + // Arrange |
| 56 | + var survey = new Survey |
| 57 | + { |
| 58 | + Title = "Test Survey", |
| 59 | + Description = "Test Description", |
| 60 | + Guid = "test-guid", |
| 61 | + Published = false, |
| 62 | + Questions = new System.Collections.Generic.List<Question> |
| 63 | + { |
| 64 | + new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 0 } |
| 65 | + }, |
| 66 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup>() |
| 67 | + }; |
| 68 | + MockDbContext.Surveys.Add(survey); |
| 69 | + MockDbContext.SaveChanges(); |
| 70 | + |
| 71 | + // Act |
| 72 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 73 | + |
| 74 | + // Assert |
| 75 | + Assert.That(isValid, Is.True); |
| 76 | + Assert.That(errorMessage, Is.Empty); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task ValidateSurveyForPublishing_ReturnsInvalid_WhenGroupIsEmpty() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var survey = new Survey |
| 84 | + { |
| 85 | + Title = "Test Survey", |
| 86 | + Description = "Test Description", |
| 87 | + Guid = "test-guid", |
| 88 | + Published = false, |
| 89 | + Questions = new System.Collections.Generic.List<Question> |
| 90 | + { |
| 91 | + new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 } |
| 92 | + }, |
| 93 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup> |
| 94 | + { |
| 95 | + new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default" }, |
| 96 | + new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Empty Group" } |
| 97 | + } |
| 98 | + }; |
| 99 | + MockDbContext.Surveys.Add(survey); |
| 100 | + MockDbContext.SaveChanges(); |
| 101 | + |
| 102 | + // Act |
| 103 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 104 | + |
| 105 | + // Assert |
| 106 | + Assert.That(isValid, Is.False); |
| 107 | + Assert.That(errorMessage, Does.Contain("empty group").IgnoreCase); |
| 108 | + Assert.That(errorMessage, Does.Contain("Empty Group")); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public async Task ValidateSurveyForPublishing_ReturnsInvalid_WhenGroupIsOrphaned() |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + var survey = new Survey |
| 116 | + { |
| 117 | + Title = "Test Survey", |
| 118 | + Description = "Test Description", |
| 119 | + Guid = "test-guid", |
| 120 | + Published = false, |
| 121 | + Questions = new System.Collections.Generic.List<Question> |
| 122 | + { |
| 123 | + new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 }, |
| 124 | + new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 } |
| 125 | + }, |
| 126 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup> |
| 127 | + { |
| 128 | + new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default" }, |
| 129 | + new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Orphaned Group" } |
| 130 | + } |
| 131 | + }; |
| 132 | + MockDbContext.Surveys.Add(survey); |
| 133 | + MockDbContext.SaveChanges(); |
| 134 | + |
| 135 | + // Act |
| 136 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 137 | + |
| 138 | + // Assert |
| 139 | + Assert.That(isValid, Is.False); |
| 140 | + Assert.That(errorMessage, Does.Contain("unreachable group").IgnoreCase); |
| 141 | + Assert.That(errorMessage, Does.Contain("Orphaned Group")); |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedViaNextGroupId() |
| 146 | + { |
| 147 | + // Arrange |
| 148 | + var survey = new Survey |
| 149 | + { |
| 150 | + Title = "Test Survey", |
| 151 | + Description = "Test Description", |
| 152 | + Guid = "test-guid", |
| 153 | + Published = false, |
| 154 | + Questions = new System.Collections.Generic.List<Question> |
| 155 | + { |
| 156 | + new TextQuestion { Id = 1, Text = "Q1", QuestionNumber = 1, QuestionType = QuestionType.Text, GroupId = 1 }, |
| 157 | + new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 } |
| 158 | + }, |
| 159 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup> |
| 160 | + { |
| 161 | + new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default", NextGroupId = 2 }, |
| 162 | + new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Connected Group" } |
| 163 | + } |
| 164 | + }; |
| 165 | + MockDbContext.Surveys.Add(survey); |
| 166 | + MockDbContext.SaveChanges(); |
| 167 | + |
| 168 | + // Act |
| 169 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 170 | + |
| 171 | + // Assert |
| 172 | + Assert.That(isValid, Is.True); |
| 173 | + Assert.That(errorMessage, Is.Empty); |
| 174 | + } |
| 175 | + |
| 176 | + [Test] |
| 177 | + public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedViaTrueFalseBranching() |
| 178 | + { |
| 179 | + // Arrange |
| 180 | + var survey = new Survey |
| 181 | + { |
| 182 | + Title = "Test Survey", |
| 183 | + Description = "Test Description", |
| 184 | + Guid = "test-guid", |
| 185 | + Published = false, |
| 186 | + Questions = new System.Collections.Generic.List<Question> |
| 187 | + { |
| 188 | + new TrueFalseQuestion |
| 189 | + { |
| 190 | + Id = 1, |
| 191 | + Text = "Q1", |
| 192 | + QuestionNumber = 1, |
| 193 | + QuestionType = QuestionType.TrueFalse, |
| 194 | + GroupId = 1, |
| 195 | + BranchToGroupIdOnTrue = 2 |
| 196 | + }, |
| 197 | + new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 } |
| 198 | + }, |
| 199 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup> |
| 200 | + { |
| 201 | + new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default" }, |
| 202 | + new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Connected via True" } |
| 203 | + } |
| 204 | + }; |
| 205 | + MockDbContext.Surveys.Add(survey); |
| 206 | + MockDbContext.SaveChanges(); |
| 207 | + |
| 208 | + // Act |
| 209 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 210 | + |
| 211 | + // Assert |
| 212 | + Assert.That(isValid, Is.True); |
| 213 | + Assert.That(errorMessage, Is.Empty); |
| 214 | + } |
| 215 | + |
| 216 | + [Test] |
| 217 | + public async Task ValidateSurveyForPublishing_ReturnsValid_WhenGroupConnectedViaMultipleChoiceBranching() |
| 218 | + { |
| 219 | + // Arrange |
| 220 | + var mcQuestion = new MultipleChoiceQuestion |
| 221 | + { |
| 222 | + Id = 1, |
| 223 | + Text = "Q1", |
| 224 | + QuestionNumber = 1, |
| 225 | + QuestionType = QuestionType.MultipleChoice, |
| 226 | + GroupId = 1 |
| 227 | + }; |
| 228 | + |
| 229 | + var choiceOption = new ChoiceOption |
| 230 | + { |
| 231 | + Id = 1, |
| 232 | + OptionText = "Option 1", |
| 233 | + MultipleChoiceQuestionId = 1, |
| 234 | + BranchToGroupId = 2 |
| 235 | + }; |
| 236 | + |
| 237 | + var survey = new Survey |
| 238 | + { |
| 239 | + Title = "Test Survey", |
| 240 | + Description = "Test Description", |
| 241 | + Guid = "test-guid", |
| 242 | + Published = false, |
| 243 | + Questions = new System.Collections.Generic.List<Question> |
| 244 | + { |
| 245 | + mcQuestion, |
| 246 | + new TextQuestion { Id = 2, Text = "Q2", QuestionNumber = 2, QuestionType = QuestionType.Text, GroupId = 2 } |
| 247 | + }, |
| 248 | + QuestionGroups = new System.Collections.Generic.List<QuestionGroup> |
| 249 | + { |
| 250 | + new QuestionGroup { Id = 1, SurveyId = 1, GroupNumber = 0, GroupName = "Default" }, |
| 251 | + new QuestionGroup { Id = 2, SurveyId = 1, GroupNumber = 1, GroupName = "Connected via MC" } |
| 252 | + } |
| 253 | + }; |
| 254 | + |
| 255 | + MockDbContext.Surveys.Add(survey); |
| 256 | + MockDbContext.ChoiceOptions.Add(choiceOption); |
| 257 | + MockDbContext.SaveChanges(); |
| 258 | + |
| 259 | + // Act |
| 260 | + var (isValid, errorMessage) = await _service.ValidateSurveyForPublishingAsync(survey.Id); |
| 261 | + |
| 262 | + // Assert |
| 263 | + Assert.That(isValid, Is.True); |
| 264 | + Assert.That(errorMessage, Is.Empty); |
| 265 | + } |
49 | 266 | } |
50 | 267 | } |
0 commit comments