Skip to content

Commit 4cde10e

Browse files
committed
Merge pull request #120 from rhubner/notempty-bug
[bufgix] Fix notEmpty bug plus some other related bugs.
2 parents 1678e1f + d8e5ece commit 4cde10e

File tree

3 files changed

+222
-10
lines changed

3 files changed

+222
-10
lines changed

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/v1_0/SchemaParser.scala

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ trait SchemaParser extends BaseSchemaParser {
168168
/**
169169
* [24] OptionalDirective ::= DirectivePrefix "optional"
170170
*/
171-
lazy val optionalDirective = "OptionalDirective" ::= directivePrefix ~> "optional" ^^^ Optional()
171+
lazy val optionalDirective = "OptionalDirective" ::= directivePrefix ~> "optional" ^^ {
172+
case _ => Optional()
173+
}
172174

173175
/**
174176
* [25] MatchIsFalseDirective ::= DirectivePrefix "matchIsFalse"
@@ -352,12 +354,16 @@ trait SchemaParser extends BaseSchemaParser {
352354
/**
353355
* [46] EmptyExpr ::= "empty"
354356
*/
355-
lazy val emptyExpr = "EmptyExpr" ::= "empty" ^^^ EmptyRule()
357+
lazy val emptyExpr = "EmptyExpr" ::= "empty" ^^ {
358+
case _ => EmptyRule()
359+
}
356360

357361
/**
358362
* [47] NotEmptyExpr ::= "notEmpty"
359363
*/
360-
lazy val notEmptyExpr = "NotEmptyExpr" ::= "notEmpty" ^^^ NotEmptyRule()
364+
lazy val notEmptyExpr = "NotEmptyExpr" ::= "notEmpty" ^^ {
365+
case _ => NotEmptyRule()
366+
}
361367

362368

363369

@@ -374,7 +380,9 @@ trait SchemaParser extends BaseSchemaParser {
374380
/**
375381
* [49] UriExpr ::= "uri"
376382
*/
377-
lazy val uriExpr = "UriExpr" ::= "uri" ^^^ UriRule()
383+
lazy val uriExpr = "UriExpr" ::= "uri" ^^ {
384+
case _ => UriRule()
385+
}
378386

379387
/**
380388
* [50] XsdDateTimeExpr ::= "xDateTime" ("(" XsdDateTimeLiteral "," XsdDateTimeLiteral ")")?
@@ -425,7 +433,9 @@ trait SchemaParser extends BaseSchemaParser {
425433
/**
426434
* [55] PartialUkDateExpr ::= "partUkDate"
427435
*/
428-
lazy val partialUkDateExpr: PackratParser[PartUkDateRule] = "PartialUkDateExpr" ::= "partUkDate" ^^^ PartUkDateRule()
436+
lazy val partialUkDateExpr: PackratParser[PartUkDateRule] = "PartialUkDateExpr" ::= "partUkDate" ^^ {
437+
case _ => PartUkDateRule()
438+
}
429439

430440
/**
431441
* [56] PartialDateExpr ::= "partDate(" StringProvider "," StringProvider "," StringProvider ")"
@@ -435,12 +445,16 @@ trait SchemaParser extends BaseSchemaParser {
435445
/**
436446
* [57] Uuid4Expr ::= "uuid4"
437447
*/
438-
lazy val uuid4Expr: PackratParser[Uuid4Rule] = "Uuid4Expr" ::= "uuid4" ^^^ Uuid4Rule()
448+
lazy val uuid4Expr: PackratParser[Uuid4Rule] = "Uuid4Expr" ::= "uuid4" ^^ {
449+
case _ => Uuid4Rule()
450+
}
439451

440452
/**
441453
* [58] PositiveIntegerExpr ::= "positiveInteger"
442454
*/
443-
lazy val positiveIntegerExpr: PackratParser[PositiveIntegerRule] = "PositiveIntegerExpr" ::= "positiveInteger" ^^^ PositiveIntegerRule()
455+
lazy val positiveIntegerExpr: PackratParser[PositiveIntegerRule] = "PositiveIntegerExpr" ::= "positiveInteger" ^^ {
456+
case _ => PositiveIntegerRule()
457+
}
444458

445459

446460

csv-validator-core/src/main/scala/uk/gov/nationalarchives/csv/validator/schema/v1_1/SchemaParser.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ trait SchemaParser extends SchemaParser1_0 {
8484
}
8585

8686

87-
lazy val identicalExpr: PackratParser[IdenticalRule] = "IdenticalExpr" ::= "identical" ^^^ IdenticalRule()
87+
lazy val identicalExpr: PackratParser[IdenticalRule] = "IdenticalExpr" ::= "identical" ^^ {
88+
case _ => IdenticalRule()
89+
}
8890

89-
lazy val upperCaseExpr: PackratParser[UpperCaseRule] = "UpperCaseExpr" ::= "upperCase" ^^^ UpperCaseRule()
91+
lazy val upperCaseExpr: PackratParser[UpperCaseRule] = "UpperCaseExpr" ::= "upperCase" ^^ {
92+
case _ => UpperCaseRule()
93+
}
9094

91-
lazy val lowerCaseExpr: PackratParser[LowerCaseRule] = "LowerCaseExpr" ::= "lowerCase" ^^^ LowerCaseRule()
95+
lazy val lowerCaseExpr: PackratParser[LowerCaseRule] = "LowerCaseExpr" ::= "lowerCase" ^^ {
96+
case _ => LowerCaseRule()
97+
}
9298

9399
lazy val xsdDateTimeTzExpr = "XsdDateTimeTzExpr" ::= "xDateTimeTz" ~> opt((("(" ~> xsdDateTimeTzLiteral) <~ ",") ~ (xsdDateTimeTzLiteral <~ ")")) ^^ {
94100
case None =>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* Copyright (c) 2013, The National Archives <[email protected]>
3+
* http://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
10+
11+
import java.io.{Reader, StringReader}
12+
13+
import org.junit.runner.RunWith
14+
import org.specs2.mutable.Specification
15+
import org.specs2.runner.JUnitRunner
16+
import uk.gov.nationalarchives.csv.validator.schema.{Schema, _}
17+
18+
import scala.language.reflectiveCalls
19+
import scalaz.{Failure, IList, Success}
20+
21+
@RunWith(classOf[JUnitRunner])
22+
class NotEmptyBugTest extends Specification with TestResources {
23+
24+
implicit def stringToStringReader(s: String): StringReader = new StringReader(s.replaceAll("\n\\s+", "\n"))
25+
26+
implicit def stringToSchema(s: String): Schema = {
27+
val schemaParser = new SchemaParser() {
28+
val pathSubstitutions = List[(String,String)]()
29+
val enforceCaseSensitivePathChecks = false
30+
val trace = false
31+
32+
override def parse(reader: Reader): ParseResult[Schema] = {
33+
super.parse(reader) match {
34+
case s@Success(schema: Schema, _) => s
35+
case NoSuccess(message, next) => throw new RuntimeException(message)
36+
}
37+
}
38+
}
39+
40+
schemaParser.parse(s).get
41+
}
42+
43+
object TestMetaDataValidator extends AllErrorsMetaDataValidator {
44+
val pathSubstitutions = List[(String,String)]();
45+
val trace = false
46+
}
47+
48+
import TestMetaDataValidator._
49+
50+
51+
"Validation" should {
52+
53+
"succeed for more than 1 notEmpty rule with different columns" in {
54+
val schema =
55+
"""version 1.1
56+
@totalColumns 2
57+
file_name: notEmpty if($curated_file_name/notEmpty, is(noext($curated_file_name) ))
58+
curated_file_name: @optional
59+
"""
60+
61+
val metaData =
62+
"""file_name,curated_file_name
63+
content,
64+
"""
65+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
66+
67+
}
68+
69+
"succeed for more than 1 empty rule with different columns" in {
70+
val schema =
71+
"""version 1.1
72+
@totalColumns 2
73+
file_name: empty if($curated_file_name/empty, is(noext($curated_file_name)))
74+
curated_file_name: @optional
75+
"""
76+
77+
val metaData =
78+
"""file_name,curated_file_name
79+
,content
80+
"""
81+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
82+
}
83+
84+
"succeed for more than 1 uuid4 rule with different columns" in {
85+
val schema =
86+
"""version 1.1
87+
@totalColumns 2
88+
file_name: uuid4 if($curated_file_name/uuid4, is(noext($curated_file_name)))
89+
curated_file_name: @optional
90+
"""
91+
92+
val metaData =
93+
"""file_name,curated_file_name
94+
8f60aab0-f66d-48d8-9382-f692b26b34dc,not-uuidv4-value
95+
"""
96+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
97+
}
98+
99+
"succeed for more than 1 positiveInteger rule with different columns" in {
100+
val schema =
101+
"""version 1.1
102+
@totalColumns 2
103+
file_name: positiveInteger if($curated_file_name/positiveInteger, is(noext($curated_file_name)))
104+
curated_file_name: @optional
105+
"""
106+
107+
val metaData =
108+
"""file_name,curated_file_name
109+
111,-1224544
110+
"""
111+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
112+
}
113+
114+
"succeed for more than 1 uri rule with different columns" in {
115+
val schema =
116+
"""version 1.1
117+
@totalColumns 2
118+
file_name: uri if($curated_file_name/uri, is(noext($curated_file_name)))
119+
curated_file_name: @optional
120+
"""
121+
122+
val metaData =
123+
"""file_name,curated_file_name
124+
http://www.root.cz/,not a URI
125+
"""
126+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
127+
}
128+
129+
"succeed for more than 1 partUkDate rule with different columns" in {
130+
val schema =
131+
"""version 1.1
132+
@totalColumns 2
133+
file_name: partUkDate if($curated_file_name/partUkDate, is(noext($curated_file_name)))
134+
curated_file_name: @optional
135+
"""
136+
137+
val metaData =
138+
"""file_name,curated_file_name
139+
04/February/1981,not a date
140+
"""
141+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
142+
}
143+
144+
"succeed for more than 1 lowerCase rule with different columns" in {
145+
val schema =
146+
"""version 1.1
147+
@totalColumns 2
148+
file_name: lowerCase if($curated_file_name/lowerCase, is(noext($curated_file_name)))
149+
curated_file_name: @optional
150+
"""
151+
152+
val metaData =
153+
"""file_name,curated_file_name
154+
lowercase text,CamelCaseText
155+
"""
156+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
157+
}
158+
159+
"succeed for more than 1 upperCase rule with different columns" in {
160+
val schema =
161+
"""version 1.1
162+
@totalColumns 2
163+
file_name: upperCase if($curated_file_name/upperCase, is(noext($curated_file_name)))
164+
curated_file_name: @optional
165+
"""
166+
167+
val metaData =
168+
"""file_name,curated_file_name
169+
UPPERCASE,CamelCaseText
170+
"""
171+
validate(metaData, schema, None) must beLike { case Success(_) => ok }
172+
}
173+
174+
"succeed for more than 1 identical rule with different columns" in {
175+
val schema =
176+
"""version 1.1
177+
@totalColumns 2
178+
file_name: identical or lowerCase
179+
curated_file_name: identical
180+
"""
181+
182+
val metaData =
183+
"""file_name,curated_file_name
184+
ble,sameValue
185+
elb,sameValue
186+
"""
187+
val result = validate(metaData, schema, None)
188+
189+
result must beLike { case Success(_) => ok }
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)