Add RethinkDB tests
- Add env variables for all data tests - Standardize Page.All behavior - Add Page.Count* tests
This commit is contained in:
@@ -49,7 +49,20 @@ let ``Add succeeds`` (data: IData) = task {
|
||||
|
||||
let ``All succeeds`` (data: IData) = task {
|
||||
let! pages = data.Page.All rootId
|
||||
Expect.hasLength pages 2 "There should have been 4 pages retrieved"
|
||||
Expect.isEmpty pages[0].Revisions "Page 0 should have had no revisions"
|
||||
Expect.isEmpty pages[1].Revisions "Page 1 should have had no revisions"
|
||||
Expect.hasLength pages 2 "There should have been 2 pages retrieved"
|
||||
pages |> List.iteri (fun idx pg ->
|
||||
Expect.equal pg.Text "" $"Page {idx} should have had no text"
|
||||
Expect.isEmpty pg.Metadata $"Page {idx} should have had no metadata"
|
||||
Expect.isEmpty pg.Revisions $"Page {idx} should have had no revisions"
|
||||
Expect.isEmpty pg.PriorPermalinks $"Page {idx} should have had no prior permalinks")
|
||||
}
|
||||
|
||||
let ``CountAll succeeds`` (data: IData) = task {
|
||||
let! pages = data.Page.CountAll rootId
|
||||
Expect.equal pages 2 "There should have been 2 pages counted"
|
||||
}
|
||||
|
||||
let ``CountListed succeeds`` (data: IData) = task {
|
||||
let! pages = data.Page.CountListed rootId
|
||||
Expect.equal pages 1 "There should have been 1 page in the page list"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
module PostgresDataTests
|
||||
|
||||
open System
|
||||
open BitBadger.Documents
|
||||
open Expecto
|
||||
open Microsoft.Extensions.Logging.Abstractions
|
||||
@@ -20,10 +21,26 @@ let mutable db: ThrowawayDatabase option = None
|
||||
let mkData () =
|
||||
PostgresData(NullLogger<PostgresData>(), ser) :> IData
|
||||
|
||||
/// The host for the PostgreSQL test database (defaults to localhost)
|
||||
let testHost =
|
||||
RethinkDbTests.env "PG_HOST" "localhost"
|
||||
|
||||
/// The database name for the PostgreSQL test database (defaults to postgres)
|
||||
let testDb =
|
||||
RethinkDbTests.env "PG_DB" "postgres"
|
||||
|
||||
/// The user ID for the PostgreSQL test database (defaults to postgres)
|
||||
let testUser =
|
||||
RethinkDbTests.env "PG_USER" "postgres"
|
||||
|
||||
/// The password for the PostgreSQL test database (defaults to postgres)
|
||||
let testPw =
|
||||
RethinkDbTests.env "PG_PW" "postgres"
|
||||
|
||||
/// Create a fresh environment from the root backup
|
||||
let freshEnvironment () = task {
|
||||
if Option.isSome db then db.Value.Dispose()
|
||||
db <- Some (ThrowawayDatabase.Create "Host=localhost;Database=postgres;User ID=postgres;Password=postgres")
|
||||
db <- Some (ThrowawayDatabase.Create $"Host={testHost};Database={testDb};User ID={testUser};Password={testPw}")
|
||||
let source = NpgsqlDataSourceBuilder db.Value.ConnectionString
|
||||
let _ = source.UseNodaTime()
|
||||
Postgres.Configuration.useDataSource (source.Build())
|
||||
@@ -108,6 +125,12 @@ let pageTests = testList "Page" [
|
||||
testTask "All succeeds" {
|
||||
do! PageDataTests.``All succeeds`` (mkData ())
|
||||
}
|
||||
testTask "CountAll succeeds" {
|
||||
do! PageDataTests.``CountAll succeeds`` (mkData ())
|
||||
}
|
||||
testTask "CountListed succeeds" {
|
||||
do! PageDataTests.``CountListed succeeds`` (mkData ())
|
||||
}
|
||||
]
|
||||
|
||||
/// Drop the throwaway PostgreSQL database
|
||||
|
||||
147
src/MyWebLog.Tests/Data/RethinkDbTests.fs
Normal file
147
src/MyWebLog.Tests/Data/RethinkDbTests.fs
Normal file
@@ -0,0 +1,147 @@
|
||||
module RethinkDbTests
|
||||
|
||||
open System
|
||||
open Expecto
|
||||
open Microsoft.Extensions.Logging.Abstractions
|
||||
open MyWebLog
|
||||
open MyWebLog.Converters
|
||||
open MyWebLog.Data
|
||||
open RethinkDb.Driver.FSharp
|
||||
open RethinkDb.Driver.Net
|
||||
|
||||
/// Get an environment variable, using the given value as the default if it is not set
|
||||
let env name value =
|
||||
match Environment.GetEnvironmentVariable $"MWL_TEST_{name}" with
|
||||
| null -> value
|
||||
| it when it.Trim() = "" -> value
|
||||
| it -> it
|
||||
|
||||
|
||||
/// The data configuration for the test database
|
||||
let dataCfg =
|
||||
DataConfig.FromUri (env "RETHINK_URI" "rethinkdb://172.17.0.2/mwl_test")
|
||||
|
||||
/// The active data instance to use for testing
|
||||
let mutable data: IData option = None
|
||||
|
||||
/// Dispose the existing data
|
||||
let disposeData () = task {
|
||||
if data.IsSome then
|
||||
let conn = (data.Value :?> RethinkDbData).Conn
|
||||
do! rethink { dbDrop dataCfg.Database; write; withRetryOnce; ignoreResult conn }
|
||||
conn.Dispose()
|
||||
data <- None
|
||||
}
|
||||
|
||||
/// Create a new data implementation instance
|
||||
let newData () =
|
||||
let log = NullLogger<RethinkDbData>()
|
||||
let conn = dataCfg.CreateConnection log
|
||||
RethinkDbData(conn, dataCfg, log)
|
||||
|
||||
/// Create a fresh environment from the root backup
|
||||
let freshEnvironment () = task {
|
||||
do! disposeData ()
|
||||
data <- Some (newData ())
|
||||
do! data.Value.StartUp()
|
||||
// This exercises Restore for all implementations; all tests are dependent on it working as expected
|
||||
do! Maintenance.Backup.restoreBackup "root-weblog.json" None false false data.Value
|
||||
}
|
||||
|
||||
/// Set up the environment for the RethinkDB tests
|
||||
let environmentSetUp = testTask "creating database" {
|
||||
let _ = Json.configure Converter.Serializer
|
||||
do! freshEnvironment ()
|
||||
}
|
||||
|
||||
/// Integration tests for the Category implementation in RethinkDB
|
||||
let categoryTests = testList "Category" [
|
||||
testTask "Add succeeds" {
|
||||
do! CategoryDataTests.``Add succeeds`` data.Value
|
||||
}
|
||||
testList "CountAll" [
|
||||
testTask "succeeds when categories exist" {
|
||||
do! CategoryDataTests.``CountAll succeeds when categories exist`` data.Value
|
||||
}
|
||||
testTask "succeeds when categories do not exist" {
|
||||
do! CategoryDataTests.``CountAll succeeds when categories do not exist`` data.Value
|
||||
}
|
||||
]
|
||||
testList "CountTopLevel" [
|
||||
testTask "succeeds when top-level categories exist" {
|
||||
do! CategoryDataTests.``CountTopLevel succeeds when top-level categories exist`` data.Value
|
||||
}
|
||||
testTask "succeeds when no top-level categories exist" {
|
||||
do! CategoryDataTests.``CountTopLevel succeeds when no top-level categories exist`` data.Value
|
||||
}
|
||||
]
|
||||
testTask "FindAllForView succeeds" {
|
||||
do! CategoryDataTests.``FindAllForView succeeds`` data.Value
|
||||
}
|
||||
testList "FindById" [
|
||||
testTask "succeeds when a category is found" {
|
||||
do! CategoryDataTests.``FindById succeeds when a category is found`` data.Value
|
||||
}
|
||||
testTask "succeeds when a category is not found" {
|
||||
do! CategoryDataTests.``FindById succeeds when a category is not found`` data.Value
|
||||
}
|
||||
]
|
||||
testList "FindByWebLog" [
|
||||
testTask "succeeds when categories exist" {
|
||||
do! CategoryDataTests.``FindByWebLog succeeds when categories exist`` data.Value
|
||||
}
|
||||
testTask "succeeds when no categories exist" {
|
||||
do! CategoryDataTests.``FindByWebLog succeeds when no categories exist`` data.Value
|
||||
}
|
||||
]
|
||||
testTask "Update succeeds" {
|
||||
do! CategoryDataTests.``Update succeeds`` data.Value
|
||||
}
|
||||
testList "Delete" [
|
||||
testTask "succeeds when the category is deleted (no posts)" {
|
||||
do! CategoryDataTests.``Delete succeeds when the category is deleted (no posts)`` data.Value
|
||||
}
|
||||
testTask "succeeds when the category does not exist" {
|
||||
do! CategoryDataTests.``Delete succeeds when the category does not exist`` data.Value
|
||||
}
|
||||
testTask "succeeds when reassigning parent category to None" {
|
||||
do! CategoryDataTests.``Delete succeeds when reassigning parent category to None`` data.Value
|
||||
}
|
||||
testTask "succeeds when reassigning parent category to Some" {
|
||||
do! CategoryDataTests.``Delete succeeds when reassigning parent category to Some`` data.Value
|
||||
}
|
||||
testTask "succeeds and removes category from posts" {
|
||||
do! CategoryDataTests.``Delete succeeds and removes category from posts`` data.Value
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Integration tests for the Page implementation in RethinkDB
|
||||
let pageTests = testList "Page" [
|
||||
testTask "Add succeeds" {
|
||||
do! PageDataTests.``Add succeeds`` data.Value
|
||||
}
|
||||
testTask "All succeeds" {
|
||||
do! PageDataTests.``All succeeds`` data.Value
|
||||
}
|
||||
testTask "CountAll succeeds" {
|
||||
do! PageDataTests.``CountAll succeeds`` data.Value
|
||||
}
|
||||
testTask "CountListed succeeds" {
|
||||
do! PageDataTests.``CountListed succeeds`` data.Value
|
||||
}
|
||||
]
|
||||
|
||||
/// Drop the throwaway RethinkDB database
|
||||
let environmentCleanUp = testTask "Clean Up" {
|
||||
do! disposeData ()
|
||||
}
|
||||
|
||||
/// All RethinkDB data tests
|
||||
let all =
|
||||
testList "RethinkDbData"
|
||||
[ environmentSetUp
|
||||
categoryTests
|
||||
pageTests
|
||||
environmentCleanUp ]
|
||||
|> testSequenced
|
||||
@@ -13,7 +13,8 @@ open Newtonsoft.Json
|
||||
let ser = Json.configure (JsonSerializer.CreateDefault())
|
||||
|
||||
/// The test database name
|
||||
let dbName = "test-db.db"
|
||||
let dbName =
|
||||
RethinkDbTests.env "SQLITE_DB" "test-db.db"
|
||||
|
||||
/// Create a SQLiteData instance for testing
|
||||
let mkData () =
|
||||
@@ -153,6 +154,16 @@ let pageTests = testList "Page" [
|
||||
try do! PageDataTests.``All succeeds`` data
|
||||
finally dispose data
|
||||
}
|
||||
testTask "CountAll succeeds" {
|
||||
let data = mkData ()
|
||||
try do! PageDataTests.``CountAll succeeds`` data
|
||||
finally dispose data
|
||||
}
|
||||
testTask "CountListed succeeds" {
|
||||
let data = mkData ()
|
||||
try do! PageDataTests.``CountListed succeeds`` data
|
||||
finally dispose data
|
||||
}
|
||||
]
|
||||
|
||||
/// Delete the SQLite database
|
||||
|
||||
Reference in New Issue
Block a user