Skip to content

Commit 324f700

Browse files
committed
Add missing 'Rule' tests
1 parent 736040d commit 324f700

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* https://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_0
10+
11+
import cats.data.Validated
12+
import org.junit.runner.RunWith
13+
import org.specs2.mutable.Specification
14+
import org.specs2.runner.JUnitRunner
15+
import uk.gov.nationalarchives.csv.validator.metadata.{Cell, Row}
16+
import uk.gov.nationalarchives.csv.validator.schema._
17+
18+
@RunWith(classOf[JUnitRunner])
19+
class EndsRuleSpec extends Specification {
20+
21+
"EndsRule with a string literal behaviour" should {
22+
val globalDirsOne = List(TotalColumns(1))
23+
24+
"succeed if cell ends with endsRule value" in {
25+
val endsRule = EndsRule(Literal(Some("world")))
26+
27+
endsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
28+
}
29+
30+
"fail if cell does not end with endsRule value" in {
31+
val endsRule = EndsRule(Literal(Some("hello world")))
32+
endsRule.evaluate(0, Row(List(Cell("hello")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
33+
case Validated.Invalid(messages) => messages.head mustEqual """ends("hello world") fails for line: 1, column: column1, value: "hello""""
34+
}
35+
}
36+
37+
"succeed if endsRule is the same as value" in {
38+
val endsRule = EndsRule(Literal(Some("hello world")))
39+
endsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
40+
}
41+
42+
"succeed if endsRule's column reference does exist" in {
43+
val endsRule = EndsRule(ColumnReference(NamedColumnIdentifier("column1")))
44+
45+
endsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
46+
}
47+
48+
"fail if endsRule's column reference doesn't exist" in {
49+
val endsRule = EndsRule(ColumnReference(NamedColumnIdentifier("nonExistentColumn")))
50+
51+
endsRule.evaluate(0, Row(List(Cell("hello world today")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
52+
case Validated.Invalid(messages) => messages.head mustEqual """ends($nonExistentColumn) fails for line: 1, column: column1, value: "hello world today""""
53+
}
54+
}
55+
56+
"succeed with @ignoreCase" in {
57+
val endsRule = EndsRule(Literal(Some("hello world")))
58+
endsRule.evaluate(0, Row(List(Cell("hello WORLD")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"), Nil, List(IgnoreCase()))))) mustEqual Validated.Valid(true)
59+
}
60+
}
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* https://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_0
10+
11+
import cats.data.Validated
12+
import org.junit.runner.RunWith
13+
import org.specs2.mutable.Specification
14+
import org.specs2.runner.JUnitRunner
15+
import uk.gov.nationalarchives.csv.validator.metadata.{Cell, Row}
16+
import uk.gov.nationalarchives.csv.validator.schema._
17+
18+
@RunWith(classOf[JUnitRunner])
19+
class IsRuleSpec extends Specification {
20+
21+
"IsRule with a string literal behaviour" should {
22+
val globalDirsOne = List(TotalColumns(1))
23+
24+
"succeed if isRule is the same as value" in {
25+
val isRule = IsRule(Literal(Some("hello world")))
26+
isRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
27+
}
28+
29+
"fail if isRule is not the same as value" in {
30+
val isRule = IsRule(Literal(Some("completely different value")))
31+
isRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
32+
case Validated.Invalid(messages) => messages.head mustEqual """is("completely different value") fails for line: 1, column: column1, value: "hello world""""
33+
}
34+
}
35+
36+
"fail if isRule is embedded in value" in {
37+
val isRule = IsRule(Literal(Some("hello world today")))
38+
isRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
39+
case Validated.Invalid(messages) => messages.head mustEqual """is("hello world today") fails for line: 1, column: column1, value: "hello world""""
40+
}
41+
}
42+
43+
"fail if isRule is not in value" in {
44+
val isRule = IsRule(Literal(Some("hello world")))
45+
isRule.evaluate(0, Row(List(Cell("hello world today")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
46+
case Validated.Invalid(messages) => messages.head mustEqual """is("hello world") fails for line: 1, column: column1, value: "hello world today""""
47+
}
48+
}
49+
50+
"succeed with @ignoreCase" in {
51+
val isRule = IsRule(Literal(Some("hello world")))
52+
isRule.evaluate(0, Row(List(Cell("hello WORLD")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"), Nil, List(IgnoreCase()))))) mustEqual Validated.Valid(true)
53+
}
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* https://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_0
10+
11+
import cats.data.Validated
12+
import org.junit.runner.RunWith
13+
import org.specs2.mutable.Specification
14+
import org.specs2.runner.JUnitRunner
15+
import uk.gov.nationalarchives.csv.validator.metadata.{Cell, Row}
16+
import uk.gov.nationalarchives.csv.validator.schema._
17+
18+
@RunWith(classOf[JUnitRunner])
19+
class NotRuleSpec extends Specification {
20+
21+
"NotRule with a string literal behaviour" should {
22+
val globalDirsOne = List(TotalColumns(1))
23+
24+
"succeed if notRule is not the same as value" in {
25+
val notRule = NotRule(Literal(Some("completely different value")))
26+
notRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
27+
}
28+
29+
"succeed if notRule is the similar to value" in {
30+
val notRule = NotRule(Literal(Some("hello world ")))
31+
notRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
32+
}
33+
34+
"fail if notRule is the same as value" in {
35+
val notRule = NotRule(Literal(Some("hello world")))
36+
notRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
37+
case Validated.Invalid(messages) => messages.head mustEqual """not("hello world") fails for line: 1, column: column1, value: "hello world""""
38+
}
39+
}
40+
41+
"fail with @ignoreCase" in {
42+
val notRule = NotRule(Literal(Some("hello world")))
43+
notRule.evaluate(0, Row(List(Cell("hello WORLD")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"), Nil, List(IgnoreCase()))))) must beLike {
44+
case Validated.Invalid(messages) => messages.head mustEqual """not("hello world") fails for line: 1, column: column1, value: "hello WORLD""""
45+
}
46+
}
47+
}
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* https://www.nationalarchives.gov.uk
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this
7+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*/
9+
package uk.gov.nationalarchives.csv.validator.schema.v1_0
10+
11+
import cats.data.Validated
12+
import org.junit.runner.RunWith
13+
import org.specs2.mutable.Specification
14+
import org.specs2.runner.JUnitRunner
15+
import uk.gov.nationalarchives.csv.validator.metadata.{Cell, Row}
16+
import uk.gov.nationalarchives.csv.validator.schema._
17+
18+
@RunWith(classOf[JUnitRunner])
19+
class StartsRuleSpec extends Specification {
20+
21+
"StartsRule with a string literal behaviour" should {
22+
val globalDirsOne = List(TotalColumns(1))
23+
24+
"succeed if cell starts with startsRule value" in {
25+
val startsRule = StartsRule(Literal(Some("hello world")))
26+
27+
startsRule.evaluate(0, Row(List(Cell("hello world today")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
28+
}
29+
30+
"fail if cell does not start with startsRule value" in {
31+
val startsRule = StartsRule(Literal(Some("hello world today")))
32+
startsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
33+
case Validated.Invalid(messages) => messages.head mustEqual """starts("hello world today") fails for line: 1, column: column1, value: "hello world""""
34+
}
35+
}
36+
37+
"succeed if startsRule is the same as value" in {
38+
val startsRule = StartsRule(Literal(Some("hello world")))
39+
startsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
40+
}
41+
42+
"succeed if startsRule's column reference does exist" in {
43+
val startsRule = StartsRule(ColumnReference(NamedColumnIdentifier("column1")))
44+
45+
startsRule.evaluate(0, Row(List(Cell("hello world")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) mustEqual Validated.Valid(true)
46+
}
47+
48+
"fail if startsRule's column reference doesn't exist" in {
49+
val startsRule = StartsRule(ColumnReference(NamedColumnIdentifier("nonExistentColumn")))
50+
51+
startsRule.evaluate(0, Row(List(Cell("hello world today")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"))))) must beLike {
52+
case Validated.Invalid(messages) => messages.head mustEqual """starts($nonExistentColumn) fails for line: 1, column: column1, value: "hello world today""""
53+
}
54+
}
55+
56+
"succeed with @ignoreCase" in {
57+
val startsRule = StartsRule(Literal(Some("hello world")))
58+
startsRule.evaluate(0, Row(List(Cell("hello WORLD")), 1), Schema(globalDirsOne, List(ColumnDefinition(NamedColumnIdentifier("column1"), Nil, List(IgnoreCase()))))) mustEqual Validated.Valid(true)
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)