-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix calling gRPC stub if proto-scheme contains nested enum
Calling of gRPC stub fails if the stub contains proto-scheme with nested enum. Regardless of whether message with the nested enum is used or not.
- Loading branch information
1 parent
769b949
commit 96f3a7d
Showing
7 changed files
with
147 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 56 additions & 67 deletions
123
backend/mockingbird/src/test/scala/ru/tinkoff/tcb/protobuf/MappersSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,79 @@ | ||
package ru.tinkoff.tcb.protobuf | ||
|
||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import scala.language.postfixOps | ||
import scala.reflect.io.Directory | ||
import scala.sys.process.* | ||
|
||
import com.github.os72.protobuf.dynamic.DynamicSchema | ||
import zio.managed.* | ||
import zio.test.* | ||
|
||
import ru.tinkoff.tcb.mockingbird.grpc.GrpcExractor.FromDynamicSchema | ||
import ru.tinkoff.tcb.mockingbird.grpc.GrpcExractor.FromGrpcProtoDefinition | ||
import ru.tinkoff.tcb.mockingbird.model.GrpcEnumSchema | ||
import ru.tinkoff.tcb.mockingbird.model.GrpcMessageSchema | ||
import ru.tinkoff.tcb.mockingbird.model.GrpcProtoDefinition | ||
import ru.tinkoff.tcb.mockingbird.model.GrpcRootMessage | ||
|
||
object MappersSpec extends ZIOSpecDefault { | ||
val allTypesInRequests = Set( | ||
".BrandAndModelRequest", | ||
".CarGenRequest", | ||
".CarSearchRequest", | ||
".CarConfigurationsRequest", | ||
".Condition", | ||
".CarPriceRequest", | ||
".PreciseCarPriceRequest", | ||
".PriceRequest", | ||
".MemoRequest", | ||
".MemoRequest.ReqEntry", // <-- It's type of the "req" field | ||
) | ||
|
||
val allTypesInNested = Set( | ||
".GetStocksRequest", | ||
".GetStocksResponse", | ||
".GetStocksResponse.StockKinds", | ||
".GetStocksResponse.Stock", | ||
".GetStocksResponse.Stocks", | ||
) | ||
|
||
override def spec: Spec[TestEnvironment & Scope, Any] = | ||
suite("Mappers suite")( | ||
test("Mappers from DynamicSchema to GrpcProtoDefinition") { | ||
for { | ||
content <- Utils.getProtoDescriptionFromResource("requests.proto") | ||
schema = DynamicSchema.parseFrom(content) | ||
protoDefinition = schema.toGrpcProtoDefinition | ||
} yield assertTrue(getAllTypes(protoDefinition) == allTypesInRequests) | ||
}, | ||
test("Mappers from DynamicSchema to GrpcProtoDefinition and back are consistent") { | ||
val managed = ZManaged.acquireReleaseWith(ZIO.attemptBlockingIO(Files.createTempDirectory("temp"))) { path => | ||
ZIO.attemptBlockingIO { | ||
val dir = new Directory(path.toFile) | ||
dir.deleteRecursively() | ||
}.orDie | ||
} | ||
(for { | ||
path <- managed | ||
readStream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO( | ||
Files.newInputStream( | ||
Paths.get("./mockingbird/src/test/resources/requests.proto") | ||
) | ||
) | ||
} | ||
writeStream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO { | ||
Files.newOutputStream(Paths.get(s"${path.toString}/requests.proto")) | ||
} | ||
} | ||
_ <- ZIO.attemptBlockingIO(writeStream.write(readStream.readAllBytes())).toManaged | ||
_ <- ZManaged.succeed { | ||
s"protoc --descriptor_set_out=${path.toString}/descriptor.desc --proto_path=${path.toString} requests.proto" ! | ||
} | ||
stream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO(new FileInputStream(new File(s"${path.toString}/descriptor.desc"))) | ||
} | ||
content <- ZIO.attemptBlockingIO(stream.readAllBytes()).toManaged | ||
for { | ||
content <- Utils.getProtoDescriptionFromResource("requests.proto") | ||
schema = DynamicSchema.parseFrom(content) | ||
protoDefinition = schema.toGrpcProtoDefinition | ||
protoDefinitionAgain = protoDefinition.toDynamicSchema.toGrpcProtoDefinition | ||
} yield assertTrue(protoDefinition == protoDefinitionAgain)).useNow | ||
} yield assertTrue(protoDefinition == protoDefinitionAgain) | ||
}, | ||
test("Mappers from nested DynamicSchema to GrpcProtoDefinition and back are consistent") { | ||
val managed = ZManaged.acquireReleaseWith(ZIO.attemptBlockingIO(Files.createTempDirectory("temp"))) { path => | ||
ZIO.attemptBlockingIO { | ||
val dir = new Directory(path.toFile) | ||
dir.deleteRecursively() | ||
}.orDie | ||
} | ||
(for { | ||
path <- managed | ||
readStream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO( | ||
Files.newInputStream( | ||
Paths.get("./mockingbird/src/test/resources/nested.proto") | ||
) | ||
) | ||
} | ||
writeStream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO { | ||
Files.newOutputStream(Paths.get(s"${path.toString}/nested.proto")) | ||
} | ||
} | ||
_ <- ZIO.attemptBlockingIO(writeStream.write(readStream.readAllBytes())).toManaged | ||
_ <- ZManaged.succeed { | ||
s"protoc --descriptor_set_out=${path.toString}/nested_descriptor.desc --proto_path=${path.toString} nested.proto" ! | ||
} | ||
stream <- ZManaged.fromAutoCloseable { | ||
ZIO.attemptBlockingIO(new FileInputStream(new File(s"${path.toString}/nested_descriptor.desc"))) | ||
} | ||
content <- ZIO.attemptBlockingIO(stream.readAllBytes()).toManaged | ||
for { | ||
content <- Utils.getProtoDescriptionFromResource("nested.proto") | ||
schema = DynamicSchema.parseFrom(content) | ||
protoDefinition = schema.toGrpcProtoDefinition | ||
} yield assertTrue(getAllTypes(protoDefinition) == allTypesInNested) | ||
}, | ||
test("Mappers from nested DynamicSchema to GrpcProtoDefinition and back are consistent") { | ||
for { | ||
content <- Utils.getProtoDescriptionFromResource("nested.proto") | ||
schema = DynamicSchema.parseFrom(content) | ||
protoDefinition = schema.toGrpcProtoDefinition | ||
protoDefinitionAgain = protoDefinition.toDynamicSchema.toGrpcProtoDefinition | ||
} yield assertTrue(protoDefinition == protoDefinitionAgain)).useNow | ||
} yield assertTrue(protoDefinition == protoDefinitionAgain) | ||
} | ||
) | ||
|
||
def getAllTypes(pd: GrpcProtoDefinition): Set[String] = | ||
pd.schemas.flatMap(getAllTypes("")).toSet | ||
|
||
def getAllTypes(packageName: String)(m: GrpcRootMessage): List[String] = | ||
m match { | ||
case GrpcEnumSchema(name, values) => s"$packageName.$name" :: Nil | ||
case GrpcMessageSchema(name, fields, oneofs, nested, nestedEnums) => | ||
val npn = s"$packageName.$name" | ||
npn :: nested.getOrElse(Nil).flatMap(getAllTypes(npn)) ::: nestedEnums.getOrElse(Nil).flatMap(getAllTypes(npn)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.