Skip to content

Commit

Permalink
Merge pull request #103 from jbogard/fix-naming
Browse files Browse the repository at this point in the history
Fix naming problems
  • Loading branch information
jbogard authored Sep 30, 2022
2 parents 5aae60b + 308b527 commit c3f5d4a
Show file tree
Hide file tree
Showing 16 changed files with 474 additions and 438 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

Respawn is a small utility to help in resetting test databases to a clean state. Instead of deleting data at the end of a test or rolling back a transaction, Respawn [resets the database back to a clean checkpoint](http://lostechies.com/jimmybogard/2013/06/18/strategies-for-isolating-the-database-in-tests/) by intelligently deleting data from tables.

To use, create a `Checkpoint` and initialize with tables you want to skip, or schemas you want to keep/ignore:
To use, create a `Respawner` and initialize with tables you want to skip, or schemas you want to keep/ignore:

```csharp
private static Checkpoint checkpoint = new Checkpoint
var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
TablesToIgnore = new Table[]
{
Expand All @@ -23,31 +23,31 @@ private static Checkpoint checkpoint = new Checkpoint
{
"RoundhousE"
}
};
});
```
Or if you want to use a different database:
```csharp
private static Checkpoint checkpoint = new Checkpoint
var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
SchemasToInclude = new []
{
"public"
},
DbAdapter = DbAdapter.Postgres
};
});
```

In your tests, in the fixture setup, reset your checkpoint:
```csharp
await checkpoint.Reset("MyConnectionStringName");
await respawner.ResetAsync("MyConnectionStringName");
```
or if you're using a database besides SQL Server, pass an open `DbConnection`:
```csharp
using (var conn = new NpgsqlConnection("ConnectionString"))
{
await conn.OpenAsync();

await checkpoint.Reset(conn);
await respawner.ResetAsync(conn);
}
```

Expand Down
48 changes: 24 additions & 24 deletions Respawn.DatabaseTests/InformixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ public async Task ShouldDeleteData()
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" }
};
await checkPoint.Reset(_connection);
});
await checkPoint.ResetAsync(_connection);

command.ExecuteScalar().ShouldBe(0);
}
Expand All @@ -97,13 +97,13 @@ public async Task ShouldIgnoreTables()
command.ExecuteNonQuery();
command.Parameters.Clear();
}
var checkPoint = new Checkpoint()
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions()
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" },
TablesToIgnore = new Table[] { "foo" }
};
await checkPoint.Reset(_connection);
});
await checkPoint.ResetAsync(_connection);

command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);
Expand Down Expand Up @@ -138,14 +138,14 @@ FOREIGN KEY (FooValue) REFERENCES Foo(Value)
command.CommandText = "SELECT COUNT(1) FROM Bar";
command.ExecuteScalar().ShouldBe(100);

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -201,14 +201,14 @@ ParentId INT NULL
command.CommandText = "SELECT COUNT(1) FROM Child";
command.ExecuteScalar().ShouldBe(100);

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -249,14 +249,14 @@ ParentId INT NULL
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -329,14 +329,14 @@ public async Task ShouldHandleComplexCycles()
command.CommandText = "SELECT COUNT(1) FROM F";
command.ExecuteScalar().ShouldBe(1);

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { "informix" }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -381,14 +381,14 @@ public async Task ShouldExcludeSchemas()
command.Parameters.Clear();
}

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToExclude = new[] { user_1 }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -425,14 +425,14 @@ public async Task ShouldIncludeSchemas()
command.Parameters.Clear();
}

var checkPoint = new Checkpoint
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.Informix,
SchemasToInclude = new[] { user_2 }
};
});
try
{
await checkPoint.Reset(_connection);
await checkPoint.ResetAsync(_connection);
}
catch
{
Expand Down
60 changes: 30 additions & 30 deletions Respawn.DatabaseTests/MySqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public async Task ShouldDeleteData()

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(100);

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(0);
}
Expand Down Expand Up @@ -114,12 +114,12 @@ PRIMARY KEY (`BarValue`),
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bob").ShouldBe(100);

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(0);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(0);
Expand All @@ -140,14 +140,14 @@ public async Task ShouldHandleSelfRelationships()

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM foo").ShouldBe(100);

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "MySqlTests" }
};
});
try
{
await checkpoint.Reset(_connection);
await checkpoint.ResetAsync(_connection);
}
catch
{
Expand Down Expand Up @@ -179,12 +179,12 @@ public async Task ShouldHandleCircularRelationships()
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM parent").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM child").ShouldBe(100);

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM parent").ShouldBe(0);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM child").ShouldBe(0);
Expand Down Expand Up @@ -224,14 +224,14 @@ public async Task ShouldHandleComplexCycles()
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM e").ShouldBe(1);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM f").ShouldBe(1);

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "MySqlTests" }
};
});
try
{
await checkpoint.Reset(_connection);
await checkpoint.ResetAsync(_connection);
}
catch
{
Expand All @@ -258,13 +258,13 @@ public async Task ShouldIgnoreTables()
_database.InsertBulk(Enumerable.Range(0, 100).Select(i => new Foo { Value = i }));
_database.InsertBulk(Enumerable.Range(0, 100).Select(i => new Bar { Value = i }));

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
TablesToIgnore = new Table[] { "Foo" },
SchemasToInclude = new[] { "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(0);
Expand All @@ -281,13 +281,13 @@ public async Task ShouldIncludeTables()
_database.InsertBulk(Enumerable.Range(0, 100).Select(i => new Foo { Value = i }));
_database.InsertBulk(Enumerable.Range(0, 100).Select(i => new Bar { Value = i }));

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
TablesToInclude = new Table[] { "Foo" },
SchemasToInclude = new[] { "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(0);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(100);
Expand All @@ -311,12 +311,12 @@ public async Task ShouldExcludeSchemas()
_database.Execute("INSERT `B`.`Bar` VALUES (" + i + ")");
}

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToExclude = new[] { "A", "MySqlTests" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM A.Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM B.Bar").ShouldBe(0);
Expand All @@ -340,12 +340,12 @@ public async Task ShouldIncludeSchemas()
_database.Execute("INSERT B.Bar VALUES (" + i + ")");
}

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
SchemasToInclude = new[] { "B" }
};
await checkpoint.Reset(_connection);
});
await checkpoint.ResetAsync(_connection);

_database.ExecuteScalar<int>("SELECT COUNT(1) FROM A.Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM B.Bar").ShouldBe(0);
Expand All @@ -359,13 +359,13 @@ public async Task ShouldResetSequencesAndIdentities()
_database.Execute("INSERT INTO a(id) VALUES (0)");
_database.Execute("INSERT INTO a(id) VALUES (0)");

var checkpoint = new Checkpoint
var checkpoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
DbAdapter = DbAdapter.MySql,
WithReseed = true
};
});

await checkpoint.Reset(_connection);
await checkpoint.ResetAsync(_connection);
_database.ExecuteScalar<int>("SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'MySqlTests' AND TABLE_NAME = 'a';").ShouldBe(1);
}

Expand Down
Loading

0 comments on commit c3f5d4a

Please sign in to comment.