Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding in multiple functions #102

Merged
merged 2 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ratatool-common/src/main/avro/TestRecord.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{"name": "double_field", "type": ["null", "double"], "default": null},
{"name": "boolean_field", "type": ["null", "boolean"], "default": null},
{"name": "string_field", "type": ["null", "string"], "default": null},
{"name": "upper_string_field", "type": ["null", "string"], "default": null},
{"name": "fixed_field", "type": ["null", {"type": "fixed", "size": 16, "name": "MD5"}], "default": null},
{"name": "map_field", "type": ["null", {"type": "map", "values": "int"}], "default": null},
{"name": "bytes_field", "type": ["null", "bytes"], "default": null }
Expand Down
1 change: 1 addition & 0 deletions ratatool-common/src/main/protobuf/schemas.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ message OptionalNestedRecord {
optional bool bool_field = 13;
optional string string_field = 14;
optional bytes bytes_field = 15;
optional string upper_string_field = 16;
}

message RequiredNestedRecord {
Expand Down
1 change: 1 addition & 0 deletions ratatool-common/src/test/resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{"mode": "NULLABLE", "name": "float_field", "type": "FLOAT"},
{"mode": "NULLABLE", "name": "boolean_field", "type": "BOOLEAN"},
{"mode": "NULLABLE", "name": "string_field", "type": "STRING"},
{"mode": "NULLABLE", "name": "upper_string_field", "type": "STRING"},
{"mode": "NULLABLE", "name": "timestamp_field", "type": "TIMESTAMP"},
{"mode": "NULLABLE", "name": "date_field", "type": "DATE"},
{"mode": "NULLABLE", "name": "time_field", "type": "TIME"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,10 @@ object ExampleAvroGen {
* Set dependent fields based on Schema criteria. This is done in a single amend with
* a single gen to ensure values are consistent per record
*/
.amend(intGen)(m => i => {
m.setIndependentIntField(i)
m.setDependentIntField(dependentIntFunc(i))
})
.amend(stringGen)(m => s => {
m.setIndependentStringField(s)
m.setDependentEnumField(dependentEnumFunc(s))
})
.amend(intGen)(_.setIndependentIntField,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a nitpick but can we add the first function on a newline here, makes it more clear when reading the examples that it's doing two things

m => i => m.setDependentIntField(dependentIntFunc(i)))
.amend(stringGen)(_.setIndependentStringField,
m => s => m.setDependentEnumField(dependentEnumFunc(s)))

private val recordIdGen: Gen[String] = Gen.alphaUpperStr

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ package object scalacheck extends AvroGeneratorOps
}

implicit class RichAvroGen[T <: SpecificRecord](gen: Gen[T]) {
def amend[U](g: Gen[U])(f: T => (U => Unit)): Gen[T] = {
def amend[U](g: Gen[U])(fns: (T => (U => Unit))*): Gen[T] = {
for (r <- gen; v <- g) yield {
f(r)(v)
fns.foreach(f => f(r)(v))
r
}
}
Expand Down Expand Up @@ -73,9 +73,9 @@ package object scalacheck extends AvroGeneratorOps
private type Record = java.util.Map[String, AnyRef]

implicit class RichTableRowGen(gen: Gen[TableRow]) {
def amend[U](g: Gen[U])(f: TableRow => (AnyRef => Record)): Gen[TableRow] = {
def amend[U](g: Gen[U])(fns: (TableRow => (AnyRef => Record))*): Gen[TableRow] = {
for (r <- gen; v <- g) yield {
f(r)(v.asInstanceOf[AnyRef])
fns.foreach(f => f(r)(v.asInstanceOf[AnyRef]))
r
}
}
Expand All @@ -99,9 +99,9 @@ package object scalacheck extends AvroGeneratorOps
}

implicit class RichProtobufBuilder[T <: Message.Builder](gen: Gen[T]) {
def amend[U](g: Gen[U])(f: T => (U => T)): Gen[T] = {
def amend[U](g: Gen[U])(fns: (T => (U => T))*): Gen[T] = {
for (r <- gen; v <- g) yield {
f(r)(v)
fns.foldLeft(r)((r, f) => f(r)(v))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ object AvroGeneratorTest extends Properties("AvroGenerator") {
.amend(Gen.choose(10.0f, 20.0f))(_.getNullableFields.setFloatField)
.amend(Gen.choose(10.0, 20.0))(_.getNullableFields.setDoubleField)
.amend(Gen.const(true))(_.getNullableFields.setBooleanField)
.amend(Gen.const("hello"))(_.getNullableFields.setStringField)
.amend(Gen.const("hello"))(_.getNullableFields.setStringField,
m => s => m.getNullableFields.setUpperStringField(s.toUpperCase))

val richTupGen = (specificRecordOf[TestRecord], specificRecordOf[TestRecord]).tupled
.amend2(specificRecordOf[RequiredNestedRecord])(_.setRequiredFields,
Expand All @@ -55,7 +56,8 @@ object AvroGeneratorTest extends Properties("AvroGenerator") {
"Double" |:
r.getNullableFields.getDoubleField >= 10.0 && r.getNullableFields.getDoubleField <= 20.0,
"Boolean" |: r.getNullableFields.getBooleanField == true,
"String" |: r.getNullableFields.getStringField == "hello"
"String" |: r.getNullableFields.getStringField == "hello",
"String" |: r.getNullableFields.getUpperStringField == "HELLO"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object ProtoBufGeneratorTest extends Properties("ProtoBufGenerator") {
.amend(Gen.choose(10.0f, 20.0f))(_.setFloatField)
.amend(Gen.choose(10.0, 20.0))(_.setDoubleField)
.amend(Gen.const(true))(_.setBoolField)
.amend(Gen.const("hello"))(_.setStringField)
.amend(Gen.const("hello"))(_.setStringField, m => s => m.setUpperStringField(s.toUpperCase))
.map(_.build())


Expand All @@ -59,7 +59,8 @@ object ProtoBufGeneratorTest extends Properties("ProtoBufGenerator") {
"Double" |:
r.getOptionalFields.getDoubleField >= 10.0 && r.getOptionalFields.getDoubleField <= 20.0,
"Boolean" |: r.getOptionalFields.getBoolField,
"String" |: r.getOptionalFields.getStringField == "hello"
"String" |: r.getOptionalFields.getStringField == "hello",
"String" |: r.getOptionalFields.getUpperStringField == "HELLO"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ object TableRowGeneratorTest extends Properties("TableRowGenerator") {
.amend(Gen.choose(10L, 20L))(_.getRecord(n).set("int_field"))
.amend(Gen.choose(10.0, 20.0))(_.getRecord(n).set("float_field"))
.amend(Gen.const(true))(_.getRecord(n).set("boolean_field"))
.amend(Gen.const("hello"))(_.getRecord(n).set("string_field"))
.amend(Gen.const("hello"))(_.getRecord(n).set("string_field"),
m => s => m.getRecord(n).set("upper_string_field")(s.asInstanceOf[String].toUpperCase))

val richTupGen = (tableRowOf(Schemas.tableSchema), tableRowOf(Schemas.tableSchema)).tupled
.amend2(Gen.choose(10L, 20L))(_.getRecord(r).set("int_field"),
Expand All @@ -49,11 +50,13 @@ object TableRowGeneratorTest extends Properties("TableRowGenerator") {
val f = fields.get("float_field").asInstanceOf[Double]
val b = fields.get("boolean_field").asInstanceOf[Boolean]
val s = fields.get("string_field").asInstanceOf[String]
val upper = fields.get("upper_string_field").asInstanceOf[String]
all(
"Int" |: i >= 10L && i <= 20L,
"Float" |: f >= 10.0 && f <= 20.0,
"Boolean" |: b == true,
"String" |: s == "hello"
"String" |: s == "hello",
"String" |: upper == "HELLO"
)
}

Expand Down