| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | using System; |
| 4 | using System.Text; |
| 5 | using System.Collections; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using System.Linq.Expressions; |
| 9 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 10 | using WixToolset.Dtf.WindowsInstaller; |
| 11 | using WixToolset.Dtf.WindowsInstaller.Linq; |
| 12 | |
| 13 | namespace WixToolset.Dtf.Test |
| 14 | { |
| 15 | [TestClass] |
| 16 | public class LinqTest |
| 17 | { |
| 18 | private void InitLinqTestDatabase(QDatabase db) |
| 19 | { |
| 20 | WindowsInstallerUtils.InitializeProductDatabase(db); |
| 21 | WindowsInstallerUtils.CreateTestProduct(db); |
| 22 | |
| 23 | db.Execute( |
| 24 | "INSERT INTO `Feature` (`Feature`, `Title`, `Description`, `Level`, `Attributes`) VALUES ('{0}', '{1}', '{2}', {3}, {4})", |
| 25 | "TestFeature2", |
| 26 | "Test Feature 2", |
| 27 | "Test Feature 2 Description", |
| 28 | 1, |
| 29 | (int) FeatureAttributes.None); |
| 30 | |
| 31 | WindowsInstallerUtils.AddRegistryComponent( |
| 32 | db, "TestFeature2", "MyTestRegComp", |
| 33 | Guid.NewGuid().ToString("B"), |
| 34 | "SOFTWARE\\Microsoft\\DTF\\Test", |
| 35 | "MyTestRegComp", "test"); |
| 36 | WindowsInstallerUtils.AddRegistryComponent( |
| 37 | db, "TestFeature2", "MyTestRegComp2", |
| 38 | Guid.NewGuid().ToString("B"), |
| 39 | "SOFTWARE\\Microsoft\\DTF\\Test", |
| 40 | "MyTestRegComp2", "test2"); |
| 41 | WindowsInstallerUtils.AddRegistryComponent( |
| 42 | db, "TestFeature2", "excludeComp", |
| 43 | Guid.NewGuid().ToString("B"), |
| 44 | "SOFTWARE\\Microsoft\\DTF\\Test", |
| 45 | "MyTestRegComp3", "test3"); |
| 46 | |
| 47 | db.Commit(); |
| 48 | |
| 49 | db.Log = Console.Out; |
| 50 | } |
| 51 | |
| 52 | [TestMethod] |
| 53 | public void LinqSimple() |
| 54 | { |
| 55 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 56 | { |
| 57 | this.InitLinqTestDatabase(db); |
| 58 | |
| 59 | var comps = from c in db.Components |
| 60 | select c; |
| 61 | |
| 62 | int count = 0; |
| 63 | foreach (var c in comps) |
| 64 | { |
| 65 | Console.WriteLine(c); |
| 66 | count++; |
| 67 | } |
| 68 | |
| 69 | Assert.AreEqual<int>(4, count); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | [TestMethod] |
| 74 | public void LinqWhereNull() |
| 75 | { |
| 76 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 77 | { |
| 78 | this.InitLinqTestDatabase(db); |
| 79 | |
| 80 | var features = from f in db.Features |
| 81 | where f.Description != null |
| 82 | select f; |
| 83 | |
| 84 | int count = 0; |
| 85 | foreach (var f in features) |
| 86 | { |
| 87 | Console.WriteLine(f); |
| 88 | Assert.AreEqual<string>("TestFeature2", f.Feature); |
| 89 | count++; |
| 90 | } |
| 91 | |
| 92 | Assert.AreEqual<int>(1, count); |
| 93 | |
| 94 | var features2 = from f in db.Features |
| 95 | where f.Description == null |
| 96 | select f; |
| 97 | |
| 98 | count = 0; |
| 99 | foreach (var f in features2) |
| 100 | { |
| 101 | Console.WriteLine(f); |
| 102 | Assert.AreEqual<string>("TestFeature1", f.Feature); |
| 103 | count++; |
| 104 | } |
| 105 | |
| 106 | Assert.AreEqual<int>(1, count); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | [TestMethod] |
| 111 | public void LinqWhereOperators() |
| 112 | { |
| 113 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 114 | { |
| 115 | this.InitLinqTestDatabase(db); |
| 116 | |
| 117 | for (int i = 0; i < 100; i++) |
| 118 | { |
| 119 | var newFile = db.Files.NewRecord(); |
| 120 | newFile.File = "TestFile" + i; |
| 121 | newFile.Component_ = "TestComponent"; |
| 122 | newFile.FileName = "TestFile" + i + ".txt"; |
| 123 | newFile.FileSize = i % 10; |
| 124 | newFile.Sequence = i; |
| 125 | newFile.Insert(); |
| 126 | } |
| 127 | |
| 128 | var files1 = from f in db.Files where f.Sequence < 40 select f; |
| 129 | Assert.AreEqual<int>(40, files1.AsEnumerable().Count()); |
| 130 | |
| 131 | var files2 = from f in db.Files where f.Sequence <= 40 select f; |
| 132 | Assert.AreEqual<int>(41, files2.AsEnumerable().Count()); |
| 133 | |
| 134 | var files3 = from f in db.Files where f.Sequence > 40 select f; |
| 135 | Assert.AreEqual<int>(59, files3.AsEnumerable().Count()); |
| 136 | |
| 137 | var files4 = from f in db.Files where f.Sequence >= 40 select f; |
| 138 | Assert.AreEqual<int>(60, files4.AsEnumerable().Count()); |
| 139 | |
| 140 | var files5 = from f in db.Files where 40 < f.Sequence select f; |
| 141 | Assert.AreEqual<int>(59, files5.AsEnumerable().Count()); |
| 142 | |
| 143 | var files6 = from f in db.Files where 40 <= f.Sequence select f; |
| 144 | Assert.AreEqual<int>(60, files6.AsEnumerable().Count()); |
| 145 | |
| 146 | var files7 = from f in db.Files where 40 > f.Sequence select f; |
| 147 | Assert.AreEqual<int>(40, files7.AsEnumerable().Count()); |
| 148 | |
| 149 | var files8 = from f in db.Files where 40 >= f.Sequence select f; |
| 150 | Assert.AreEqual<int>(41, files8.AsEnumerable().Count()); |
| 151 | |
| 152 | var files9 = from f in db.Files where f.Sequence == 40 select f; |
| 153 | Assert.AreEqual<int>(40, files9.AsEnumerable().First().Sequence); |
| 154 | |
| 155 | var files10 = from f in db.Files where f.Sequence != 40 select f; |
| 156 | Assert.AreEqual<int>(99, files10.AsEnumerable().Count()); |
| 157 | |
| 158 | var files11 = from f in db.Files where 40 == f.Sequence select f; |
| 159 | Assert.AreEqual<int>(40, files11.AsEnumerable().First().Sequence); |
| 160 | |
| 161 | var files12 = from f in db.Files where 40 != f.Sequence select f; |
| 162 | Assert.AreEqual<int>(99, files12.AsEnumerable().Count()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | [TestMethod] |
| 167 | public void LinqShapeSelect() |
| 168 | { |
| 169 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 170 | { |
| 171 | this.InitLinqTestDatabase(db); |
| 172 | |
| 173 | Console.WriteLine("Running LINQ query 1."); |
| 174 | var features1 = from f in db.Features |
| 175 | select new { Name = f.Feature, |
| 176 | Desc = f.Description }; |
| 177 | |
| 178 | int count = 0; |
| 179 | foreach (var f in features1) |
| 180 | { |
| 181 | Console.WriteLine(f); |
| 182 | count++; |
| 183 | } |
| 184 | |
| 185 | Assert.AreEqual<int>(2, count); |
| 186 | |
| 187 | Console.WriteLine(); |
| 188 | Console.WriteLine("Running LINQ query 2."); |
| 189 | var features2 = from f in db.Features |
| 190 | where f.Description != null |
| 191 | select new { Name = f.Feature, |
| 192 | Desc = f.Description.ToLower() }; |
| 193 | |
| 194 | count = 0; |
| 195 | foreach (var f in features2) |
| 196 | { |
| 197 | Console.WriteLine(f); |
| 198 | Assert.AreEqual<string>("TestFeature2", f.Name); |
| 199 | count++; |
| 200 | } |
| 201 | |
| 202 | Assert.AreEqual<int>(1, count); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | [TestMethod] |
| 207 | public void LinqUpdateNullableString() |
| 208 | { |
| 209 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 210 | { |
| 211 | this.InitLinqTestDatabase(db); |
| 212 | |
| 213 | string newDescription = "New updated feature description."; |
| 214 | |
| 215 | var features = from f in db.Features |
| 216 | where f.Description != null |
| 217 | select f; |
| 218 | |
| 219 | int count = 0; |
| 220 | foreach (var f in features) |
| 221 | { |
| 222 | Console.WriteLine(f); |
| 223 | Assert.AreEqual<string>("TestFeature2", f.Feature); |
| 224 | f.Description = newDescription; |
| 225 | count++; |
| 226 | } |
| 227 | |
| 228 | Assert.AreEqual<int>(1, count); |
| 229 | |
| 230 | var features2 = from f in db.Features |
| 231 | where f.Description == newDescription |
| 232 | select f; |
| 233 | count = 0; |
| 234 | foreach (var f in features2) |
| 235 | { |
| 236 | Console.WriteLine(f); |
| 237 | Assert.AreEqual<string>("TestFeature2", f.Feature); |
| 238 | f.Description = null; |
| 239 | count++; |
| 240 | } |
| 241 | |
| 242 | Assert.AreEqual<int>(1, count); |
| 243 | |
| 244 | var features3 = from f in db.Features |
| 245 | where f.Description == null |
| 246 | select f.Feature; |
| 247 | count = 0; |
| 248 | foreach (var f in features3) |
| 249 | { |
| 250 | Console.WriteLine(f); |
| 251 | count++; |
| 252 | } |
| 253 | |
| 254 | Assert.AreEqual<int>(2, count); |
| 255 | |
| 256 | db.Commit(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | [TestMethod] |
| 261 | public void LinqInsertDelete() |
| 262 | { |
| 263 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 264 | { |
| 265 | this.InitLinqTestDatabase(db); |
| 266 | |
| 267 | var newProp = db.Properties.NewRecord(); |
| 268 | newProp.Property = "TestNewProp1"; |
| 269 | newProp.Value = "TestNewValue"; |
| 270 | newProp.Insert(); |
| 271 | |
| 272 | string prop = (from p in db.Properties |
| 273 | where p.Property == "TestNewProp1" |
| 274 | select p.Value).AsEnumerable().First(); |
| 275 | Assert.AreEqual<string>("TestNewValue", prop); |
| 276 | |
| 277 | newProp.Delete(); |
| 278 | |
| 279 | int propCount = (from p in db.Properties |
| 280 | where p.Property == "TestNewProp1" |
| 281 | select p.Value).AsEnumerable().Count(); |
| 282 | Assert.AreEqual<int>(0, propCount); |
| 283 | |
| 284 | db.Commit(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | [TestMethod] |
| 289 | public void LinqQueryQRecord() |
| 290 | { |
| 291 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 292 | { |
| 293 | this.InitLinqTestDatabase(db); |
| 294 | |
| 295 | var installFilesSeq = (from a in db["InstallExecuteSequence"] |
| 296 | where a["Action"] == "InstallFiles" |
| 297 | select a["Sequence"]).AsEnumerable().First(); |
| 298 | Assert.AreEqual<string>("4000", installFilesSeq); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | [TestMethod] |
| 303 | public void LinqOrderBy() |
| 304 | { |
| 305 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 306 | { |
| 307 | this.InitLinqTestDatabase(db); |
| 308 | |
| 309 | var actions = from a in db.InstallExecuteSequences |
| 310 | orderby a.Sequence |
| 311 | select a.Action; |
| 312 | foreach (var a in actions) |
| 313 | { |
| 314 | Console.WriteLine(a); |
| 315 | } |
| 316 | |
| 317 | var files = from f in db.Files |
| 318 | orderby f.FileSize, f["Sequence"] |
| 319 | where f.Attributes == FileAttributes.None |
| 320 | select f; |
| 321 | |
| 322 | foreach (var f in files) |
| 323 | { |
| 324 | Console.WriteLine(f); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | [TestMethod] |
| 330 | public void LinqTwoWayJoin() |
| 331 | { |
| 332 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 333 | { |
| 334 | this.InitLinqTestDatabase(db); |
| 335 | int count; |
| 336 | |
| 337 | var regs = from r in db.Registries |
| 338 | join c in db["Component"] on r.Component_ equals c["Component"] |
| 339 | where c["Component"] == "MyTestRegComp" && |
| 340 | r.Root == RegistryRoot.UserOrMachine |
| 341 | select new { Reg = r.Registry, Dir = c["Directory_"] }; |
| 342 | |
| 343 | count = 0; |
| 344 | foreach (var r in regs) |
| 345 | { |
| 346 | Console.WriteLine(r); |
| 347 | count++; |
| 348 | } |
| 349 | Assert.AreEqual<int>(1, count); |
| 350 | |
| 351 | var regs2 = from r in db.Registries |
| 352 | join c in db.Components on r.Component_ equals c.Component |
| 353 | where c.Component == "MyTestRegComp" && |
| 354 | r.Root == RegistryRoot.UserOrMachine |
| 355 | select new { Reg = r, Dir = c.Directory_ }; |
| 356 | |
| 357 | count = 0; |
| 358 | foreach (var r in regs2) |
| 359 | { |
| 360 | Assert.IsNotNull(r.Reg.Registry); |
| 361 | Console.WriteLine(r); |
| 362 | count++; |
| 363 | } |
| 364 | Assert.AreEqual<int>(1, count); |
| 365 | |
| 366 | var regs3 = from r in db.Registries |
| 367 | join c in db.Components on r.Component_ equals c.Component |
| 368 | where c.Component == "MyTestRegComp" && |
| 369 | r.Root == RegistryRoot.UserOrMachine |
| 370 | select r; |
| 371 | |
| 372 | count = 0; |
| 373 | foreach (var r in regs3) |
| 374 | { |
| 375 | Assert.IsNotNull(r.Registry); |
| 376 | Console.WriteLine(r); |
| 377 | count++; |
| 378 | } |
| 379 | Assert.AreEqual<int>(1, count); |
| 380 | |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | [TestMethod] |
| 385 | public void LinqFourWayJoin() |
| 386 | { |
| 387 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 388 | { |
| 389 | this.InitLinqTestDatabase(db); |
| 390 | int count; |
| 391 | |
| 392 | IList<string> pretest = db.ExecuteStringQuery( |
| 393 | "SELECT `Feature`.`Feature` " + |
| 394 | "FROM `Feature`, `FeatureComponents`, `Component`, `Registry` " + |
| 395 | "WHERE `Feature`.`Feature` = `FeatureComponents`.`Feature_` " + |
| 396 | "AND `FeatureComponents`.`Component_` = `Component`.`Component` " + |
| 397 | "AND `Component`.`Component` = `Registry`.`Component_` " + |
| 398 | "AND (`Registry`.`Registry` = 'MyTestRegCompReg1')"); |
| 399 | Assert.AreEqual<int>(1, pretest.Count); |
| 400 | |
| 401 | var features = from f in db.Features |
| 402 | join fc in db.FeatureComponents on f.Feature equals fc.Feature_ |
| 403 | join c in db.Components on fc.Component_ equals c.Component |
| 404 | join r in db.Registries on c.Component equals r.Component_ |
| 405 | where r.Registry == "MyTestRegCompReg1" |
| 406 | select f.Feature; |
| 407 | |
| 408 | count = 0; |
| 409 | foreach (var featureName in features) |
| 410 | { |
| 411 | Console.WriteLine(featureName); |
| 412 | count++; |
| 413 | } |
| 414 | Assert.AreEqual<int>(1, count); |
| 415 | |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | [TestMethod] |
| 420 | public void EnumTable() |
| 421 | { |
| 422 | using (QDatabase db = new QDatabase("testlinq.msi", DatabaseOpenMode.Create)) |
| 423 | { |
| 424 | this.InitLinqTestDatabase(db); |
| 425 | int count = 0; |
| 426 | foreach (var comp in db.Components) |
| 427 | { |
| 428 | Console.WriteLine(comp); |
| 429 | count++; |
| 430 | } |
| 431 | Assert.AreNotEqual<int>(0, count); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | [TestMethod] |
| 436 | public void DatabaseAsQueryable() |
| 437 | { |
| 438 | using (Database db = new Database("testlinq.msi", DatabaseOpenMode.Create)) |
| 439 | { |
| 440 | WindowsInstallerUtils.InitializeProductDatabase(db); |
| 441 | WindowsInstallerUtils.CreateTestProduct(db); |
| 442 | |
| 443 | var comps = from c in db.AsQueryable().Components |
| 444 | select c; |
| 445 | |
| 446 | int count = 0; |
| 447 | foreach (var c in comps) |
| 448 | { |
| 449 | Console.WriteLine(c); |
| 450 | count++; |
| 451 | } |
| 452 | |
| 453 | Assert.AreEqual<int>(1, count); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | [TestMethod] |
| 458 | public void EnumProducts() |
| 459 | { |
| 460 | var products = from p in ProductInstallation.AllProducts |
| 461 | where p.Publisher == ".NET Foundation" |
| 462 | select new { Name = p.ProductName, |
| 463 | Ver = p.ProductVersion, |
| 464 | Company = p.Publisher, |
| 465 | InstallDate = p.InstallDate, |
| 466 | PackageCode = p.AdvertisedPackageCode }; |
| 467 | |
| 468 | foreach (var p in products) |
| 469 | { |
| 470 | Console.WriteLine(p); |
| 471 | Assert.IsTrue(p.Company == ".NET Foundation"); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | [TestMethod] |
| 476 | public void EnumFeatures() |
| 477 | { |
| 478 | foreach (var p in ProductInstallation.AllProducts) |
| 479 | { |
| 480 | Console.WriteLine(p.ProductName); |
| 481 | |
| 482 | foreach (var f in p.Features) |
| 483 | { |
| 484 | Console.WriteLine("\t" + f.FeatureName); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | [TestMethod] |
| 490 | public void EnumComponents() |
| 491 | { |
| 492 | var comps = from c in ComponentInstallation.AllComponents |
| 493 | where c.State == InstallState.Local && |
| 494 | c.Product.Publisher == ".NET Foundation" |
| 495 | select c.Path; |
| 496 | |
| 497 | int count = 0; |
| 498 | foreach (var c in comps) |
| 499 | { |
| 500 | if (++count == 100) break; |
| 501 | |
| 502 | Console.WriteLine(c); |
| 503 | } |
| 504 | |
| 505 | Assert.AreEqual<int>(100, count); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | } |