~donaldw/dbversion/verify-feature

« back to all changes in this revision

Viewing changes to src/DatabaseVersion.Tests/Sql/ScriptTaskTests.cs

  • Committer: Adam Connelly
  • Date: 2012-01-24 22:01:24 UTC
  • Revision ID: adam.rpconnelly@gmail.com-20120124220124-zi1dcqbt7bo8ie77
Updating ScriptTask so that it uses the current connection from the NHibernate session to create a command, and then uses that command to execute the script. Previously it was using ISession.CreateSQLQuery. The problem with CreateSQLQuery is that it parses the SQL text for parameters which can cause executing the script to fail.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    public class ScriptTaskTests
17
17
    {
18
18
        private readonly Mock<ISession> session = new Mock<ISession>() { DefaultValue = DefaultValue.Mock };
 
19
        private readonly Mock<IDbCommand> command = new Mock<IDbCommand>();
19
20
        private readonly Mock<IMessageService> messageService = new Mock<IMessageService> { DefaultValue = DefaultValue.Mock };
20
21
 
 
22
        public ScriptTaskTests()
 
23
        {
 
24
            this.session.Setup(s => s.Connection.CreateCommand()).Returns(command.Object);
 
25
        }
 
26
 
21
27
        [Fact]
22
28
        public void ShouldUseDatabaseArchiveToGetScript()
23
29
        {
49
55
            task.Execute(session.Object, 1, 1);
50
56
 
51
57
            // Assert
52
 
            session.Verify(s => s.CreateSQLQuery("ABCDE").ExecuteUpdate());
 
58
            command.VerifySet(c => c.CommandText = "ABCDE");
 
59
            command.Verify(c => c.ExecuteNonQuery());
53
60
        }
54
61
 
55
62
        [Fact]
66
73
            task.Execute(session.Object, 1, 1);
67
74
 
68
75
            // Assert
69
 
            session.Verify(s => s.CreateSQLQuery("ABCDE").ExecuteUpdate());
70
 
            session.Verify(s => s.CreateSQLQuery("FGHIJ").ExecuteUpdate());
 
76
            command.VerifySet(c => c.CommandText = "ABCDE");
 
77
            command.VerifySet(c => c.CommandText = "FGHIJ");
71
78
        }
72
79
 
73
80
        private static readonly string ScriptWithDifferentCasedSeparators =
95
102
            task.Execute(session.Object, 1, 1);
96
103
 
97
104
            // Assert
98
 
            session.Verify(s => s.CreateSQLQuery("ABCDE").ExecuteUpdate());
99
 
            session.Verify(s => s.CreateSQLQuery("FGHIJ").ExecuteUpdate());
100
 
            session.Verify(s => s.CreateSQLQuery("DEFJAB").ExecuteUpdate());
101
 
            session.Verify(s => s.CreateSQLQuery("LDKSIE").ExecuteUpdate());
102
 
            session.Verify(s => s.CreateSQLQuery("dkjsaks").ExecuteUpdate());
 
105
            command.VerifySet(c => c.CommandText = "ABCDE");
 
106
            command.VerifySet(c => c.CommandText = "FGHIJ");
 
107
            command.VerifySet(c => c.CommandText = "DEFJAB");
 
108
            command.VerifySet(c => c.CommandText = "LDKSIE");
 
109
            command.VerifySet(c => c.CommandText = "dkjsaks");
103
110
        }
104
111
 
105
112
        private static readonly string ScriptWithSeparatorsWithinLines =
123
130
            task.Execute(session.Object, 1, 1);
124
131
 
125
132
            // Assert
126
 
            session.Verify(s => s.CreateSQLQuery("insert into books (name) values ('Great Book');").ExecuteUpdate());
127
 
            session.Verify(s => s.CreateSQLQuery("update books set name = 'Good to go' where name = 'Great Book';").ExecuteUpdate());
128
 
            session.Verify(s => s.CreateSQLQuery("delete from books;").ExecuteUpdate());
 
133
            command.VerifySet(c => c.CommandText = "insert into books (name) values ('Great Book');");
 
134
            command.VerifySet(c => c.CommandText = "update books set name = 'Good to go' where name = 'Great Book';");
 
135
            command.VerifySet(c => c.CommandText = "delete from books;");
129
136
        }
130
137
 
131
138
        private static readonly string ScriptWithSeparatorAtStartOfScript =
146
153
            task.Execute(session.Object, 1, 1);
147
154
 
148
155
            // Assert
149
 
            session.Verify(s => s.CreateSQLQuery("update books set name = 'Good to go' where name = 'Great Book';").ExecuteUpdate());
 
156
            command.VerifySet(c => c.CommandText = "update books set name = 'Good to go' where name = 'Great Book';");
150
157
        }
151
158
 
152
159
        private static readonly string ScriptWithSeparatorAtEndOfScript =
167
174
            task.Execute(session.Object, 1, 1);
168
175
 
169
176
            // Assert
170
 
            session.Verify(s => s.CreateSQLQuery("update books set name = 'Good to go' where name = 'Great Book';").ExecuteUpdate());
 
177
            command.VerifySet(c => c.CommandText = "update books set name = 'Good to go' where name = 'Great Book';");
171
178
        }
172
179
 
173
180
        [Fact]
181
188
                GetStream(ScriptWithSeparatorAtEndOfScript));
182
189
            ScriptTask task = new ScriptTask("scripts\\schema.sql", 0, version.Object, messageService.Object);
183
190
            Exception exception = new Exception();
184
 
            session.Setup(s => s.CreateSQLQuery(It.IsAny<string>()).ExecuteUpdate()).Throws(exception);
 
191
            command.Setup(c=>c.ExecuteNonQuery()).Throws(exception);
185
192
 
186
193
            // Act
187
194
            Exception thrownException = Record.Exception(() => task.Execute(session.Object, 1, 1));