added flux testing package

pull/51/head
Scott Anderson 2019-02-07 19:33:41 -07:00
parent d5e50b1dcf
commit 54bcfcfa60
4 changed files with 146 additions and 16 deletions

View File

@ -0,0 +1,19 @@
---
title: Flux Testing functions
description: >
Flux testing functions test piped-forward data in specific ways and return errors if the tests fail.
menu:
v2_0_ref:
name: Testing
parent: Flux functions
weight: 204
---
Flux testing functions test piped-forward data in specific ways and return errors if the tests fail.
To use them, import the `testing` package:
```js
import "testing"
```
{{< children type="functions" show="pages" >}}

View File

@ -0,0 +1,41 @@
---
title: testing.assertEmpty() function
description: The testing.assertEmpty() function tests if an input stream is empty.
menu:
v2_0_ref:
name: testing.assertEmpty
parent: Testing
weight: 301
---
The `testing.assertEmpty()` function tests if an input stream is empty.
If not empty, the function outputs an error.
_**Function type:** Test_
```js
import "testing"
testing.assertEmpty()
```
_The `testing.assertEmpty()` function can be used to perform in-line tests in a query._
## Examples
#### Check if there is a difference between streams
This example uses the [`diff()` function](/flux/v0.x/functions/tests/diff)
which outputs the diff for the two streams.
The `.testing.assertEmpty()` function checks to see if the diff is empty.
```js
import "testing"
got = from(bucket: "telegraf/autogen")
|> range(start: -15m)
want = from(bucket: "backup_telegraf/autogen")
|> range(start: -15m)
got
|> diff(want: want)
|> testing.assertEmpty()
```

View File

@ -1,45 +1,45 @@
--- ---
title: assertEquals() function title: testing.assertEquals() function
description: The assertEquals() function tests whether two streams have identical data. description: The testing.assertEquals() function tests whether two streams have identical data.
aliases: aliases:
- /v2.0/reference/flux/functions/tests/assertequals - /v2.0/reference/flux/functions/tests/assertequals
menu: menu:
v2_0_ref: v2_0_ref:
name: assertEquals name: testing.assertEquals
parent: built-in-tests parent: Testing
weight: 401 weight: 301
--- ---
The `assertEquals()` function tests whether two streams have identical data. The `testing.assertEquals()` function tests whether two streams have identical data.
If equal, the function outputs the tested data stream unchanged. If equal, the function outputs the tested data stream unchanged.
If unequal, the function outputs an error. If unequal, the function outputs an error.
_**Function type:** Test_
```js ```js
assertEquals( import "testing"
testing.assertEquals(
name: "streamEquality", name: "streamEquality",
got: got, got: got,
want: want want: want
) )
``` ```
_The `assertEquals()` function can be used to perform in-line tests in a query._ _The `testing.assertEquals()` function can be used to perform in-line tests in a query._
## Parameters ## Parameters
## name ### name
Unique name given to the assertion. Unique name given to the assertion.
_**Data type:** String_ _**Data type:** String_
## got ### got
The stream containing data to test. The stream containing data to test.
Defaults to data piped-forward from another function (`<-`). Defaults to piped-forward data (`<-`).
_**Data type:** Object_ _**Data type:** Object_
## want ### want
The stream that contains the expected data to test against. The stream that contains the expected data to test against.
_**Data type:** Object_ _**Data type:** Object_
@ -49,21 +49,25 @@ _**Data type:** Object_
##### Assert of separate streams ##### Assert of separate streams
```js ```js
import "testing"
want = from(bucket: "backup-telegraf/autogen") want = from(bucket: "backup-telegraf/autogen")
|> range(start: -5m) |> range(start: -5m)
got = from(bucket: "telegraf/autogen") got = from(bucket: "telegraf/autogen")
|> range(start: -5m) |> range(start: -5m)
assertEquals(got: got, want: want) testing.assertEquals(got: got, want: want)
``` ```
##### Inline assertion ##### Inline assertion
```js ```js
import "testing"
want = from(bucket: "backup-telegraf/autogen") want = from(bucket: "backup-telegraf/autogen")
|> range(start: -5m) |> range(start: -5m)
from(bucket: "telegraf/autogen") from(bucket: "telegraf/autogen")
|> range(start: -5m) |> range(start: -5m)
|> assertEquals(want: want) |> testing.assertEquals(want: want)
``` ```

View File

@ -0,0 +1,66 @@
---
title: testing.diff() function
description: The testing.diff() function produces a diff between two streams.
menu:
v2_0_ref:
name: testing.diff
parent: Testing
weight: 301
---
The `testing.diff()` function produces a diff between two streams.
_**Function type:** Test_
```js
import "testing"
testing.diff(got: stream2, want: stream1)
```
It matches tables from each stream with the same group keys.
For each matched table, it produces a diff.
Any added or removed rows are added to the table as a row.
An additional string column with the name `diff` is created and contains a `-` if the
row was present in the `got` table and not in the `want` table or `+` if the opposite is true.
The diff function is guaranteed to emit at least one row if the tables are
different and no rows if the tables are the same. _The exact diff produced may change._
_The `testing.diff()` function can be used to perform in-line diffs in a query._
## Parameters
### got
The stream containing data to test.
_Defaults to piped-forward data (`<-`)._
_**Data type:** Object_
### want
The stream that contains the expected data to test against.
_**Data type:** Object_
## Examples
##### Diff separate streams
```js
import "testing"
want = from(bucket: "backup-telegraf/autogen")
|> range(start: -5m)
got = from(bucket: "telegraf/autogen")
|> range(start: -5m)
testing.diff(got: got, want: want)
```
##### Inline diff
```js
import "testing"
want = from(bucket: "backup-telegraf/autogen") |> range(start: -5m)
from(bucket: "telegraf/autogen")
|> range(start: -5m)
|> testing.diff(want: want)
```