コード例 #1
0
        public void SqlMakeTest_Simple()
        {
            /* Overview:
             *
             *   This is a set of very simple tests.
             *
             *   Three files are created:
             *
             *     - A user-defined table type (udtt).
             *     - A dependent stored procedure.
             *       This object references the udtt.
             *       SQL Server requires that the udtt exist before
             *       this dependent sp can be compiled, and - conversely -
             *       the dependent sp has to be dropped before the udtt
             *       can be dropped.
             *     - An independent stored procedure (sp).
             *       It's "independent" in that it has no dependencies
             *       on the udtt.
             *
             *   Tests 1 thru 7 create, modify and delete the above source files,
             *   then run the make algorithm to ensure it behaves correctly.
             *   (The "File Tests" section.)
             *
             *   Tests 8 thru 11 exercise the make algorithm by dropping and recreating
             *   the corresponding SQL Server objects for the above stored procedures and udtt.
             *   (The "SQL Server Object Tests" section.)
             *
             * Implementation Note:
             *
             *   NUnit doesn't guarantee that individual test methods will be
             *   run in any kind of order.  But these particular tests are
             *   tightly coupled to one another via the source files they work with.
             *   That requires the tests to be run in a specific order.
             *   Therefore, all of the tests are contained in one test method
             *   where their execution order can be explicitly stated.
             *
             *   The tests are very similar to one another, so they're differentiated
             *   from each other by a number in the error message they return.
             *   This makes it easier to tell which test failed. */

            /*******************************************
            *
            *  File Tests
            *
            *  These tests manipulate the source files to see if the
            *  make algorithm creates (or ignores) the correct corresponding
            *  SQL Server objects.
            *
            *******************************************/

            /*****************************************************************************************
             * 1. Initial environment: create and make all three files.
             *    All three should exist on the server as version 1. */
            CreateIndependentSPFile(FileVersion.One);
            CreateDependentSPFile(FileVersion.One);
            CreateUdttFile(FileVersion.One);

            var make = new Make(_connection);

            make.AddDirectory(_tempFolder, "*.sql", SearchOption.AllDirectories);
            make.LogEvent   += (_, e) => _log.WriteLine(EventLogEntryType.Information, e.GetMessage());
            make.ErrorEvent += (_, e) => _log.WriteLine(EventLogEntryType.Error, e.GetException().GetAllExceptionMessages());
            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "1. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "1. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "1. No udtt V1.");

            /* The Assert.IsTrue calls only check for the existence of objects on the server.
             * Those assertions cannot tell if the make algorithm did too much work.
             * There are two kinds of extra work:
             *   compiling an object more than once, and
             *   compiling an object when it doesn't have to
             * The make algorithm will throw an exception if it tries to compile an object more than once.
             * But the algorithm can't check itself to ensure it's not compiling an object when it doesn't have to.
             * After all, that's the make algorithm's only purpose,
             * and that's a bug that has to be checked for by theses tests.
             * That's what the CheckCompiledObjects() method does. */
            CheckCompiledObjects(1, make.CompiledObjectNames, new[] { _independentSpObjectName, _dependentSpObjectName, _udttObjectName });

            /*****************************************************************************************
             * 2. Update the indpendent stored procedure to version 2.
             *    Assert that only that server object is a version 2.
             *    The dependent stored procedure and udtt should still
             *    be a version 1. */

            CreateIndependentSPFile(FileVersion.Two);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.Two), "2. No independent V2.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "2. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "2. No udtt V1.");
            CheckCompiledObjects(1, make.CompiledObjectNames, new[] { _independentSpObjectName });

            /*****************************************************************************************
             * 3. Delete the independent stored procedure file.
             *    Its corresponding server object should remain untouched
             *    by the make algorithm (i.e. make should never drop an object
             *    if its file doesn't exist).  All of the previous assertions
             *    should still be true. */

            File.Delete(_independentSpFilename);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.Two), "3. No independent V2.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "3. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "3. No udtt V1.");
            CheckCompiledObjects(3, make.CompiledObjectNames, _emptyStringList);

            /*****************************************************************************************
             * 4. Restore the independent stored procedure file at version 1, and
             *    update the dependent stored procedure file to version 2. */

            CreateIndependentSPFile(FileVersion.One);
            CreateDependentSPFile(FileVersion.Two);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "4. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.Two), "4. No dependent V2.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "4. No udtt V1.");
            CheckCompiledObjects(4, make.CompiledObjectNames, new[] { _independentSpObjectName, _dependentSpObjectName });

            /*****************************************************************************************
             * 5. Delete the dependent stored procedure file.
             *    Its corresponding server object should remain untouched
             *    by the make algorithm (i.e. make should never drop an object
             *    if its file doesn't exist).  All of the previous assertions
             *    should still be true. */

            File.Delete(_dependentSpFilename);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "5. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.Two), "5. No dependent V2.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "5. No udtt V1.");
            CheckCompiledObjects(5, make.CompiledObjectNames, _emptyStringList);

            /*****************************************************************************************
             * 6. Restore the dependent stored procedure file at version 1, and
             *    update the udtt file to version 2. */

            CreateDependentSPFile(FileVersion.One);
            CreateUdttFile(FileVersion.Two);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "6. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "6. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.Two), "6. No udtt V2.");
            CheckCompiledObjects(6, make.CompiledObjectNames, new[] { _dependentSpObjectName, _udttObjectName });

            /*****************************************************************************************
             * 7. Delete the udtt file.
             *    Its corresponding server object should remain untouched
             *    by the make algorithm (i.e. make should never drop an object
             *    if its file doesn't exist).  All of the previous assertions
             *    should still be true. */

            File.Delete(_udttFilename);

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "7. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "7. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.Two), "7. No udtt V2.");
            CheckCompiledObjects(7, make.CompiledObjectNames, _emptyStringList);

            /*******************************************
            *
            *  SQL Server Object Tests
            *
            *  These tests delete the objects on the server to see if the
            *  make algorithm correctly re-creates them.
            *
            *******************************************/

            /*****************************************************************************************
             * 8. Restore the initial file environment, then drop all of the
             *    corresponding server objects.  Essentially the same as test #1. */

            CreateIndependentSPFile(FileVersion.One);
            CreateDependentSPFile(FileVersion.One);
            CreateUdttFile(FileVersion.One);

            DropUdtt(); /* Also drops the dependent SP. */
            DropIndependentSP();

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "8. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "8. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "8. No udtt V1.");
            CheckCompiledObjects(8, make.CompiledObjectNames, new[] { _independentSpObjectName, _dependentSpObjectName, _udttObjectName });

            /*****************************************************************************************
             * 9. Drop the independent stored procedure server object. */

            DropIndependentSP();

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "9. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "9. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "9. No udtt V1.");
            CheckCompiledObjects(9, make.CompiledObjectNames, new[] { _independentSpObjectName });

            /*****************************************************************************************
             * 10. Drop the dependent stored procedure server object. */

            DropDependentSP();

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "10. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "10. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "10. No udtt V1.");
            CheckCompiledObjects(10, make.CompiledObjectNames, new[] { _dependentSpObjectName });

            /*****************************************************************************************
             * 11. Drop the udtt (which also drops the dependent stored procedure server object). */

            DropUdtt(); /* Also drops the dependent SP. */

            make.Run();

            Assert.IsTrue(DoesIndependentSPServerObjectExist(FileVersion.One), "11. No independent V1.");
            Assert.IsTrue(DoesDependentSPServerObjectExist(FileVersion.One), "11. No dependent V1.");
            Assert.IsTrue(DoesUdttServerObjectExist(FileVersion.One), "11. No udtt V1.");
            CheckCompiledObjects(11, make.CompiledObjectNames, new[] { _dependentSpObjectName, _udttObjectName });
        }