-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwhen_using_as_xunit_class_fixture.cs
More file actions
82 lines (66 loc) · 2.44 KB
/
when_using_as_xunit_class_fixture.cs
File metadata and controls
82 lines (66 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using Xunit;
namespace Postgres2Go.Samples
{
public class PgFixture : IDisposable
{
private readonly PostgresRunner _pgRunner;
public PgFixture()
{
_pgRunner = PostgresRunner.Start();
}
public void Dispose() => _pgRunner?.Dispose();
public string ConnectionString => _pgRunner.GetConnectionString();
}
public class when_using_as_xunit_class_fixture : IClassFixture<PgFixture>
{
private readonly PgFixture _fixture;
public when_using_as_xunit_class_fixture(PgFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task should_create_new_database_and_allow_exec_query_1()
{
// PREPARE
var dbName = "test_xunit_db_1";
var cmdBuilder = new StringBuilder();
cmdBuilder.AppendLine($"CREATE DATABASE {dbName}");
cmdBuilder.AppendLine("CONNECTION LIMIT = -1");
// RUN
using (var conn = new Npgsql.NpgsqlConnection(_fixture.ConnectionString))
{
await conn.OpenAsync();
var cmd = new NpgsqlCommand(cmdBuilder.ToString(), conn);
cmd.ExecuteNonQuery();
// ASSERT
var dbExists = await new NpgsqlCommand($"SELECT datname FROM pg_catalog.pg_database WHERE datname = '{dbName}'",conn)
.ExecuteScalarAsync();
Assert.NotNull(dbExists);
}
}
[Fact]
public async Task should_create_new_database_and_allow_exec_query_2()
{
// PREPARE
var dbName = "test_xunit_db_2";
var cmdBuilder = new StringBuilder();
cmdBuilder.AppendLine($"CREATE DATABASE {dbName}");
cmdBuilder.AppendLine("CONNECTION LIMIT = -1");
// RUN
using (var conn = new Npgsql.NpgsqlConnection(_fixture.ConnectionString))
{
await conn.OpenAsync();
var cmd = new NpgsqlCommand(cmdBuilder.ToString(), conn);
cmd.ExecuteNonQuery();
// ASSERT
var dbExists = await new NpgsqlCommand($"SELECT datname FROM pg_catalog.pg_database WHERE datname = '{dbName}'",conn)
.ExecuteScalarAsync();
Assert.NotNull(dbExists);
}
}
}
}