<commit_before>module Tests

open System
open Expecto
open Davenport.Fsharp.Wrapper

type MyUnion =
    | Case1 of string
    | Case2 of int
    | Case3 of int64

type MyTestClass = {
    MyId: string
    MyRev: string
    Foo: string
    Bar: bool
    Baz: int
    Bat: int64
    Opt: string option
    Union: MyUnion
    Date: DateTime
    Dec: Decimal
}

let defaultRecord = {
    MyId = ""
    MyRev = ""
    Foo = "test value"
    Bar = true
    Baz = 11
    Bat = 0L
    Opt = None
    Union = Case1 "Hello world"
    Date = DateTime.UtcNow
    Dec = 10.5m
}

let viewName = "only-bazs-greater-than-10"

let designDocName = "list"

let defaultDesignDocs =
    mapFunction "function (doc) { if (doc.Baz > 10) { emit(doc._id, doc); } }"
    |> reduceFunction "_count"
    |> view viewName
    |> toSeq
    |> designDoc designDocName
    |> toSeq

let notNullOrEmpty (s: string) = String.IsNullOrEmpty s |> Expect.isFalse

let asyncMap (fn: 'a -> 'b) (task: Async<'a>) = async {
    let! result = task

    return fn result
}

[<Tests>]
let tests =
    let t = System.Environment.CommandLine
    let debug = false
    let fiddler = false
    let client =
        if fiddler then "localhost.fiddler:5984" else "localhost:5984"
        |> database "davenport_net_fsharp"
        |> idField "MyId"
        |> revField "MyRev"

    // Set debug to `true` below to start debugging.
    // 1. Start the test suite (dotnet run)
    // 2. Go to VS Code's Debug tab.
    // 3. Choose ".NET Core Attach"
    // 4. Choose one of the running processes. It's probably the one that says 'dotnet exec yadayada path to app'. Several processes may start and stop while the project is building.
    if debug then
        printfn "Waiting to attach debugger. Run .NET Core Attach under VS Code's debug menu."
        while not(System.Diagnostics.Debugger.IsAttached) do
          System.Threading.Thread.Sleep(100)
        System.Diagnostics.Debugger.Break()

    printfn "Configuring database."

    // Configure the database before running tests
    configureDatabase [] [] client
    |> Async.RunSynchronously

    printfn "Database configured."

    testList "Davenport.Fsharp.Wrapper" [
        testCaseAsync "Creates docs" <| async {
            let! doc = create<MyTestClass> defaultRecord client

            notNullOrEmpty doc.Id "Id was empty"
            notNullOrEmpty doc.Rev "Rev was empty"
            Expect.isTrue doc.Ok ""
        }

        testCaseAsync "Creates docs with specific ids" <| async {
            let myId = sprintf "specific_id_%s" (Guid.NewGuid().ToString())
            let! doc = createWithId myId defaultRecord client

            Expect.equal doc.Id myId "Created id and supplied id did not match"
            notNullOrEmpty doc.Rev "Rev was empty"
            Expect.isTrue doc.Ok ""
        }

        testCaseAsync "Gets docs" <| async {
            let! created = create<MyTestClass> defaultRecord client
            let! docResult = get<MyTestClass> created.Id None client

            Expect.isSome docResult "DocResult is None"

            let doc = Option.get docResult

            Expect.equal created.Id doc.MyId ""
            Expect.equal created.Rev doc.MyRev ""
            Expect.equal defaultRecord.Foo doc.Foo ""
            Expect.equal defaultRecord.Bar doc.Bar ""
            Expect.equal defaultRecord.Baz doc.Baz ""
        }

        testCaseAsync "Gets docs with revision" <| async {
            let! created = create<MyTestClass> defaultRecord client
            let! docResult = get<MyTestClass> created.Id (Some created.Rev) client

            Expect.isSome docResult "DocResult is None"

            let doc = Option.get docResult

            Expect.equal created.Id doc.MyId ""
            Expect.equal created.Rev doc.MyRev ""
            Expect.equal defaultRecord.Foo doc.Foo ""
            Expect.equal defaultRecord.Bar doc.Bar ""
            Expect.equal defaultRecord.Baz doc.Baz ""
        }

        ftestCaseAsync "Returns None for dogs that don't exist" <| async {
            let! docResult = get<MyTestClass> (Guid.NewGuid().ToString()) None client

            Expect.isNone docResult "DocResult should be None"
        }

        testCaseAsync "Counts docs" <| async {
            do! create<MyTestClass> defaultRecord client |> Async.Ignore

            let! count = count client

            Expect.isGreaterThan count 0 "Count was 0."
        }
<commit_msg>Unfocus test<commit_after>module Tests

open System
open Expecto
open Davenport.Fsharp.Wrapper

type MyUnion =
    | Case1 of string
    | Case2 of int
    | Case3 of int64

type MyTestClass = {
    MyId: string
    MyRev: string
    Foo: string
    Bar: bool
    Baz: int
    Bat: int64
    Opt: string option
    Union: MyUnion
    Date: DateTime
    Dec: Decimal
}

let defaultRecord = {
    MyId = ""
    MyRev = ""
    Foo = "test value"
    Bar = true
    Baz = 11
    Bat = 0L
    Opt = None
    Union = Case1 "Hello world"
    Date = DateTime.UtcNow
    Dec = 10.5m
}

let viewName = "only-bazs-greater-than-10"

let designDocName = "list"

let defaultDesignDocs =
    mapFunction "function (doc) { if (doc.Baz > 10) { emit(doc._id, doc); } }"
    |> reduceFunction "_count"
    |> view viewName
    |> toSeq
    |> designDoc designDocName
    |> toSeq

let notNullOrEmpty (s: string) = String.IsNullOrEmpty s |> Expect.isFalse

let asyncMap (fn: 'a -> 'b) (task: Async<'a>) = async {
    let! result = task

    return fn result
}

[<Tests>]
let tests =
    let t = System.Environment.CommandLine
    let debug = false
    let fiddler = false
    let client =
        if fiddler then "localhost.fiddler:5984" else "localhost:5984"
        |> database "davenport_net_fsharp"
        |> idField "MyId"
        |> revField "MyRev"

    // Set debug to `true` below to start debugging.
    // 1. Start the test suite (dotnet run)
    // 2. Go to VS Code's Debug tab.
    // 3. Choose ".NET Core Attach"
    // 4. Choose one of the running processes. It's probably the one that says 'dotnet exec yadayada path to app'. Several processes may start and stop while the project is building.
    if debug then
        printfn "Waiting to attach debugger. Run .NET Core Attach under VS Code's debug menu."
        while not(System.Diagnostics.Debugger.IsAttached) do
          System.Threading.Thread.Sleep(100)
        System.Diagnostics.Debugger.Break()

    printfn "Configuring database."

    // Configure the database before running tests
    configureDatabase [] [] client
    |> Async.RunSynchronously

    printfn "Database configured."

    testList "Davenport.Fsharp.Wrapper" [
        testCaseAsync "Creates docs" <| async {
            let! doc = create<MyTestClass> defaultRecord client

            notNullOrEmpty doc.Id "Id was empty"
            notNullOrEmpty doc.Rev "Rev was empty"
            Expect.isTrue doc.Ok ""
        }

        testCaseAsync "Creates docs with specific ids" <| async {
            let myId = sprintf "specific_id_%s" (Guid.NewGuid().ToString())
            let! doc = createWithId myId defaultRecord client

            Expect.equal doc.Id myId "Created id and supplied id did not match"
            notNullOrEmpty doc.Rev "Rev was empty"
            Expect.isTrue doc.Ok ""
        }

        testCaseAsync "Gets docs" <| async {
            let! created = create<MyTestClass> defaultRecord client
            let! docResult = get<MyTestClass> created.Id None client

            Expect.isSome docResult "DocResult is None"

            let doc = Option.get docResult

            Expect.equal created.Id doc.MyId ""
            Expect.equal created.Rev doc.MyRev ""
            Expect.equal defaultRecord.Foo doc.Foo ""
            Expect.equal defaultRecord.Bar doc.Bar ""
            Expect.equal defaultRecord.Baz doc.Baz ""
        }

        testCaseAsync "Gets docs with revision" <| async {
            let! created = create<MyTestClass> defaultRecord client
            let! docResult = get<MyTestClass> created.Id (Some created.Rev) client

            Expect.isSome docResult "DocResult is None"

            let doc = Option.get docResult

            Expect.equal created.Id doc.MyId ""
            Expect.equal created.Rev doc.MyRev ""
            Expect.equal defaultRecord.Foo doc.Foo ""
            Expect.equal defaultRecord.Bar doc.Bar ""
            Expect.equal defaultRecord.Baz doc.Baz ""
        }

        testCaseAsync "Returns None for dogs that don't exist" <| async {
            let! docResult = get<MyTestClass> (Guid.NewGuid().ToString()) None client

            Expect.isNone docResult "DocResult should be None"
        }

        testCaseAsync "Counts docs" <| async {
            do! create<MyTestClass> defaultRecord client |> Async.Ignore

            let! count = count client

            Expect.isGreaterThan count 0 "Count was 0."
        }

{'fraction_non_alphanumeric': 0.08939051918735892, 'fraction_numerical': 0.007900677200902935, 'mean_word_length': 2.19200288184438, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487999', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2537, 'n_words': 821}