|
| 1 | +using DbUp.Cli.Tests.TestInfrastructure; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using FluentAssertions; |
| 7 | +using System.Data.SqlClient; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Npgsql; |
| 11 | +using System.Data.Common; |
| 12 | + |
| 13 | +namespace DbUp.Cli.IntegrationTests |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class CocroachDbTests : DockerBasedTest |
| 17 | + { |
| 18 | + readonly CaptureLogsLogger Logger; |
| 19 | + readonly IEnvironment Env; |
| 20 | + |
| 21 | + public CocroachDbTests() |
| 22 | + { |
| 23 | + Env = new CliEnvironment(); |
| 24 | + Logger = new CaptureLogsLogger(); |
| 25 | + |
| 26 | + Environment.SetEnvironmentVariable("CONNSTR", "Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=dbup;Username=root"); |
| 27 | + } |
| 28 | + |
| 29 | + string GetBasePath(string subPath = "EmptyScript") => |
| 30 | + Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\CockroachDb\{subPath}"); |
| 31 | + |
| 32 | + string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript") => new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName; |
| 33 | + |
| 34 | + Func<DbConnection> CreateConnection = () => new NpgsqlConnection("Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=defaultdb;Username=root"); |
| 35 | + |
| 36 | + [TestInitialize] |
| 37 | + public Task TestInitialize() |
| 38 | + { |
| 39 | + /* |
| 40 | + * Before the first run, download the image: |
| 41 | + * docker pull cockroachdb/cockroach:v22.1.1 |
| 42 | + * */ |
| 43 | + return DockerInitialize( |
| 44 | + "cockroachdb/cockroach:v22.1.1", |
| 45 | + new List<string>() { }, |
| 46 | + new List<string>() { "start-single-node", "--insecure" }, |
| 47 | + "26257", |
| 48 | + CreateConnection |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + [TestCleanup] |
| 53 | + public Task TestCleanup() |
| 54 | + { |
| 55 | + return DockerCleanup(CreateConnection, con => new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", con as NpgsqlConnection)); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Ensure_CreateANewDb() |
| 60 | + { |
| 61 | + var engine = new ToolEngine(Env, Logger); |
| 62 | + |
| 63 | + var result = engine.Run("upgrade", "--ensure", GetConfigPath()); |
| 64 | + result.Should().Be(0); |
| 65 | + |
| 66 | + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) |
| 67 | + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) |
| 68 | + { |
| 69 | + connection.Open(); |
| 70 | + var count = command.ExecuteScalar(); |
| 71 | + |
| 72 | + count.Should().Be(1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + // Drop database does not supported for PostgreSQL by DbUp |
| 78 | + [TestMethod] |
| 79 | + public void Drop_DropADb() |
| 80 | + { |
| 81 | + var engine = new ToolEngine(Env, Logger); |
| 82 | +
|
| 83 | + engine.Run("upgrade", "--ensure", GetConfigPath()); |
| 84 | + var result = engine.Run("drop", GetConfigPath()); |
| 85 | + result.Should().Be(0); |
| 86 | + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) |
| 87 | + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) |
| 88 | + { |
| 89 | + Action a = () => connection.Open(); |
| 90 | + a.Should().Throw<SqlException>("Database DbUp should not exist"); |
| 91 | + } |
| 92 | + } |
| 93 | + */ |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void DatabaseShouldNotExistBeforeTestRun() |
| 97 | + { |
| 98 | + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) |
| 99 | + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) |
| 100 | + { |
| 101 | + Action a = () => { connection.Open(); command.ExecuteScalar(); }; |
| 102 | + a.Should().Throw<Exception>("Database DbUp should not exist"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + [TestMethod] |
| 107 | + public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries() |
| 108 | + { |
| 109 | + var engine = new ToolEngine(Env, Logger); |
| 110 | + |
| 111 | + var r = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "Timeout")); |
| 112 | + r.Should().Be(1); |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void UpgradeCommand_ShouldUseASpecifiedJournal() |
| 117 | + { |
| 118 | + var engine = new ToolEngine(Env, Logger); |
| 119 | + |
| 120 | + var result = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "JournalTableScript")); |
| 121 | + result.Should().Be(0); |
| 122 | + |
| 123 | + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) |
| 124 | + using (var command = new NpgsqlCommand("select count(*) from public.journal where scriptname = '001.sql'", connection)) |
| 125 | + { |
| 126 | + connection.Open(); |
| 127 | + var count = command.ExecuteScalar(); |
| 128 | + |
| 129 | + count.Should().Be(1); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | +} |
0 commit comments