-
Notifications
You must be signed in to change notification settings - Fork 2
/
IntegrationTests.fs
204 lines (173 loc) · 5.61 KB
/
IntegrationTests.fs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module Symbolica.Extensions.Configuration.FSharp.IntegrationTests
open Microsoft.Extensions.Configuration
open Swensen.Unquote
open Xunit
[<RequireQualifiedAccess>]
type LogLevel =
| Info
| Debug
| Warning
| Error
module LogLevel =
let bind =
Binder(
fun (s: string) -> s.ToLowerInvariant()
>> (function
| "info" -> Success LogLevel.Info
| "debug" -> Success LogLevel.Debug
| "warning" -> Success LogLevel.Warning
| "error" -> Success LogLevel.Error
| _ -> Failure ValueError.invalidType<LogLevel>)
)
type ILogSink =
abstract Level: LogLevel option
type ConsoleSink =
{ Level: LogLevel option }
interface ILogSink with
member x.Level = x.Level
module ConsoleSink =
let bind =
bind {
let! level = Bind.optValueAt "Level" LogLevel.bind
return { Level = level }
}
module Bytes =
let bind<[<Measure>] 'u> (units: string) =
Binder (fun (s: string) ->
if s.EndsWith(units) then
s.Substring(0, s.Length - 1) |> Success
else
Failure(ValueError.Message $"Expected bytes value to end with '{units}'."))
|> Binder.extend Bind.int
|> Binder.map LanguagePrimitives.Int32WithMeasure<'u>
[<Measure>]
type B
module B =
let bind = Bytes.bind<B> "B"
[<Measure>]
type KB
module KB =
let bind = Bytes.bind<KB> "KB"
let toBytes (x: int<KB>) = x * 1000<B/KB>
[<Measure>]
type MB
module MB =
let bind = Bytes.bind<MB> "MB"
let toKiloBytes (x: int<MB>) = x * 1000<KB/MB>
let toBytes = toKiloBytes >> KB.toBytes
type FileSink =
{ Level: LogLevel option
MaxFileSize: int<B> }
interface ILogSink with
member x.Level = x.Level
module FileSink =
let bind =
bind {
let! level = Bind.optValueAt "Level" LogLevel.bind
and! maxFileSize =
Bind.valueAt
"MaxFileSize"
(Bind.oneValueOf (
MB.bind |> Binder.map MB.toBytes
<|> (KB.bind |> Binder.map KB.toBytes)
<|> B.bind
))
return
{ Level = level
MaxFileSize = maxFileSize }
}
type AppInsightsSink =
{ Level: LogLevel option
InstrumentationKey: string }
interface ILogSink with
member x.Level = x.Level
module AppInsightsSink =
let bind =
bind {
let! level = Bind.optValueAt "Level" LogLevel.bind
and! instrumentationKey = Bind.valueAt "InstrumentationKey" Bind.string
return
{ Level = level
InstrumentationKey = instrumentationKey }
}
type LoggingOptions =
{ DefaultLevel: LogLevel
Sinks: ILogSink list }
module LoggingOptions =
let toILogSinkBinder b =
b |> Binder.map (fun s -> s :> ILogSink)
let bind =
bind {
let! defaultLevel = Bind.valueAt "DefaultLevel" LogLevel.bind
and! sinks =
Bind.optSection
"Sinks"
([ Bind.optSection "Console" (ConsoleSink.bind |> toILogSinkBinder)
Bind.optSection "File" (FileSink.bind |> toILogSinkBinder)
Bind.optSection "AppInsights" (AppInsightsSink.bind |> toILogSinkBinder) ]
|> Bind.allOf
|> Binder.map (List.choose id))
return
{ DefaultLevel = defaultLevel
Sinks = sinks |> Option.defaultValue [] }
}
let mkConfig config =
Bind.section "Logging" LoggingOptions.bind
|> Binder.eval (
ConfigurationBuilder()
.AddInMemoryCollection(config |> Map.ofList)
.Build()
)
|> BindResult.mapFailure (fun e -> e.ToString())
[<Fact>]
let ``should bind successfully without any sinks`` () =
test
<@ [ "Logging:DefaultLevel", "Warning" ] |> mkConfig = Success(
{ DefaultLevel = LogLevel.Warning
Sinks = [] }
) @>
[<Fact>]
let ``should bind successfully with some sinks`` () =
test
<@ [ "Logging:DefaultLevel", "Warning"
"Logging:Sinks:File:MaxFileSize", "1024B"
"Logging:Sinks:AppInsights:Level", "Error"
"Logging:Sinks:AppInsights:InstrumentationKey", "super-secret-key" ]
|> mkConfig = Success(
{ DefaultLevel = LogLevel.Warning
Sinks =
[ { Level = None; MaxFileSize = 1024<B> }
{ Level = Some LogLevel.Error
InstrumentationKey = "super-secret-key" } ] }
) @>
[<Fact>]
let ``should fail with pretty message if config empty`` () =
test
<@ [] |> mkConfig = Failure(
"""@'Logging':
The key was not found."""
) @>
[<Fact>]
let ``should fail with pretty message if level and a sink is invalid`` () =
test
<@ [ "Logging:DefaultLevel", "NotALevel"
"Logging:Sinks:File:MaxFileSize", "NotBytes" ]
|> mkConfig = Failure(
"""@'Logging':
all of these:
@'DefaultLevel':
Value: 'NotALevel'
Error:
Could not parse value as type 'LogLevel'.
@'Sinks':
all of these:
@'File':
all of these:
@'MaxFileSize':
Value: 'NotBytes'
Error:
one of these:
Expected bytes value to end with 'MB'.
Expected bytes value to end with 'KB'.
Expected bytes value to end with 'B'."""
) @>