This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[WIP] - New Versioning #284
Draft
Yawolf
wants to merge
2
commits into
master
Choose a base branch
from
new-versioning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+682
−152
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
version = "2.5.2" | ||
style = defaultWithAlign | ||
maxColumn = 100 | ||
maxColumn = 120 | ||
|
||
continuationIndent.callSite = 2 | ||
|
||
|
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
2 changes: 1 addition & 1 deletion
2
src/main/resources/db/migration/data/V1__add_protocols_table.sql
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,6 +1,6 @@ | ||
CREATE TABLE protocols ( | ||
id CHARACTER VARYING(255) NOT NULL, | ||
version INTEGER NOT NULL, | ||
version VARCHAR(20) NOT NULL CHECK (version ~ '^(\d+\.)?(\d+\.)?(\*|\d+)$'), | ||
protocol BYTEA, | ||
PRIMARY KEY (id, version) | ||
); |
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 |
---|---|---|
|
@@ -3,5 +3,5 @@ CREATE TYPE idl AS ENUM ('avro', 'protobuf', 'mu', 'openapi', 'scala'); | |
CREATE TABLE metaprotocols ( | ||
id CHARACTER VARYING(255) PRIMARY KEY, | ||
idl_name idl NOT NULL, | ||
version INTEGER NOT NULL | ||
version VARCHAR(20) NOT NULL CHECK (version ~ '^(\d+\.)?(\d+\.)?(\*|\d+)$') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,8 +24,8 @@ import eu.timepit.refined.boolean.{And, AnyOf} | |
import eu.timepit.refined.char.LetterOrDigit | ||
import eu.timepit.refined.collection.{Forall, MaxSize} | ||
import eu.timepit.refined.generic.Equal | ||
import eu.timepit.refined.numeric.Positive | ||
import higherkindness.compendium.models.{ProtocolIdError, ProtocolVersionError} | ||
import eu.timepit.refined.string.MatchesRegex | ||
import higherkindness.compendium.models._ | ||
import shapeless.{::, HNil} | ||
|
||
object refinements { | ||
|
@@ -37,19 +37,20 @@ object refinements { | |
type ProtocolIdConstraints = And[MaxProtocolIdSize, ValidProtocolIdChars] | ||
|
||
type ProtocolId = String Refined ProtocolIdConstraints | ||
|
||
object ProtocolId extends RefinedTypeOps[ProtocolId, String] { | ||
def parseOrRaise[F[_]: Sync](id: String): F[ProtocolId] = | ||
F.fromEither(ProtocolId.from(id).leftMap(ProtocolIdError)) | ||
} | ||
|
||
type ProtocolVersion = Int Refined Positive | ||
/** An String that matches with format xx.yy.zz, xx.yy, xx */ | ||
type ProtocolVersionRefined = | ||
String Refined MatchesRegex[W.`"""^(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$"""`.T] | ||
object ProtocolVersionRefined extends RefinedTypeOps[ProtocolVersionRefined, String] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we don't need this anymore... I have to give it a deeper look |
||
|
||
object ProtocolVersion extends RefinedTypeOps[ProtocolVersion, Int] { | ||
def parseOrRaise[F[_]: Sync](version: String): F[ProtocolVersion] = | ||
for { | ||
number <- F.delay(version.toInt) | ||
protoVersion <- F.fromEither(ProtocolVersion.from(number).leftMap(ProtocolVersionError)) | ||
} yield protoVersion | ||
versionRefined <- F.fromEither(ProtocolVersionRefined.from(version).leftMap(ProtocolVersionError)) | ||
protocolVersion <- F.fromEither(ProtocolVersion.fromString(versionRefined.value)) | ||
} yield protocolVersion | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -17,10 +17,10 @@ | |
package higherkindness.compendium.metadata.pg | ||
|
||
import doobie.implicits._ | ||
import doobie.{Query0, Update0} | ||
import doobie._ | ||
import higherkindness.compendium.core.doobie.implicits._ | ||
import higherkindness.compendium.core.refinements.ProtocolId | ||
import higherkindness.compendium.models.ProtocolMetadata | ||
import higherkindness.compendium.models._ | ||
|
||
object Queries { | ||
|
||
|
@@ -29,11 +29,11 @@ object Queries { | |
SELECT exists (SELECT true FROM metaprotocols WHERE id=$id) | ||
""".query[Boolean] | ||
|
||
def store(id: ProtocolId, idl_name: String): Update0 = | ||
def store(id: ProtocolId, protocol_version: ProtocolVersion, idl_name: String): Update0 = | ||
sql""" | ||
INSERT INTO metaprotocols (id, idl_name, version) | ||
VALUES ($id, $idl_name::idl, 1) | ||
ON CONFLICT (id) DO UPDATE SET version = metaprotocols.version + 1 | ||
VALUES ($id, $idl_name::idl, $protocol_version) | ||
ON CONFLICT (id) DO UPDATE SET version = ${protocol_version.incRevision} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Revision is updated by default, but this can be changed |
||
RETURNING version | ||
""".update | ||
|
||
|
@@ -44,4 +44,9 @@ object Queries { | |
|
||
def checkConn: Query0[Boolean] = | ||
sql"SELECT exists (SELECT 1)".query[Boolean] | ||
|
||
val checkVersion: Query[ProtocolId, ProtocolVersion] = | ||
Query( | ||
"SELECT version from metaprotocols WHERE id = ?" | ||
) | ||
} |
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
195 changes: 195 additions & 0 deletions
195
src/main/scala/higherkindness/compendium/models/ProtocolVersion.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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2018-2020 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package higherkindness.compendium.models | ||
|
||
import cats.{Eq, Show} | ||
import cats.implicits._ | ||
import cats.kernel.Monoid | ||
|
||
/** | ||
* A case class that contains the version of a Protocol. | ||
* Used this as guideline: https://snowplowanalytics.com/blog/2014/05/13/introducing-schemaver-for-semantic-versioning-of-schemas/ | ||
* | ||
* This versioning is quite similar to SemVer, but applied to Schemas. | ||
* | ||
* @param model equivalent to Major. Changes that will break interaction with historical data. | ||
* @param revision equivalent to Minor. Changes that may, or may not, break interaction with historical data. | ||
* @param addition equivalent to Patch. Changes that do not break the interaction with all historical data. | ||
*/ | ||
|
||
final case class ProtocolVersion( | ||
model: ModelVersion, | ||
revision: RevisionVersion, | ||
addition: AdditionVersion | ||
) | ||
|
||
object ProtocolVersion { | ||
|
||
/** | ||
* This function pretends to set a new patch version. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(0)) | ||
* val newPatch = AdditionVersion(100) | ||
* ProtocolVersion.setPatch(ver, newPatch) == ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(100)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a nre ProtocolVersion | ||
*/ | ||
def setAddition(pv: ProtocolVersion, newAddition: AdditionVersion): ProtocolVersion = | ||
pv.copy(addition = newAddition) | ||
|
||
/** | ||
* This function pretends to set a new minor version. | ||
* When a new Revision is set, the Addition is reset to 0. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(10)) | ||
* val newMinor = RevisionVersion(100) | ||
* ProtocolVersion.setMinor(ver, newMinor) == ProtocolVersion(ModelVersion(0), RevisionVersion(100), AdditionVersion(0)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a nre ProtocolVersion | ||
*/ | ||
def setRevision(pv: ProtocolVersion, newRevision: RevisionVersion): ProtocolVersion = | ||
setAddition(pv.copy(revision = newRevision), Monoid[AdditionVersion].empty) | ||
|
||
/** | ||
* This function pretends to set a new minor version. | ||
* When a new model is set, the Revision and the Addition are set to 0. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(1), AdditionVersion(2)) | ||
* val newMajor = ModelVersion(100) | ||
* ProtocolVersion.setMinor(ver, newMajor) == ProtocolVersion(ModelVersion(100), RevisionVersion(0), AdditionVersion(0)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a new ProtocolVersion | ||
*/ | ||
def setModel(pv: ProtocolVersion, newModel: ModelVersion): ProtocolVersion = | ||
setRevision(pv.copy(model = newModel), Monoid[RevisionVersion].empty) | ||
|
||
/** | ||
* This function pretends to increment the addition version by one. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(0)) | ||
* ProtocolVersion.incPatch(ver) == ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(1)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a new ProtocolVersion | ||
*/ | ||
def incAddition(pv: ProtocolVersion): ProtocolVersion = | ||
setAddition(pv, pv.addition |+| AdditionVersion(1)) | ||
|
||
/** | ||
* This function pretends to increment the minor version by one. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(1), AdditionVersion(0)) | ||
* ProtocolVersion.incMinor(ver) == ProtocolVersion(ModelVersion(0), RevisionVersion(1), AdditionVersion(0)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a new ProtocolVersion | ||
*/ | ||
def incRevision(pv: ProtocolVersion): ProtocolVersion = | ||
setRevision(pv, pv.revision |+| RevisionVersion(1)) | ||
|
||
/** | ||
* This function pretends to increment the minor version by one. | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(0), RevisionVersion(0), AdditionVersion(0)) | ||
* ProtocolVersion.incMajor(ver) == ProtocolVersion(ModelVersion(1), RevisionVersion(0), AdditionVersion(0)) | ||
* | ||
* @param pv Protocol Version | ||
* @return a new ProtocolVersion | ||
*/ | ||
def incModel(pv: ProtocolVersion): ProtocolVersion = setModel(pv, pv.model |+| ModelVersion(1)) | ||
|
||
/** | ||
* A simple function for creating a ProtocolVersion from a String | ||
* E.g.: | ||
* val version = "10.1.9" | ||
* ProtocolVersion.fromString(version) == Right( | ||
* ProtocolVersion(ModelVersion(10), RevisionVersion(1), AdditionVersion(9)) | ||
* ) | ||
* | ||
* @param s the string to be parsed | ||
* @return a ProtocolVersion | ||
*/ | ||
def fromString(s: String): Either[ProtocolVersionError, ProtocolVersion] = { | ||
val matcher = "([0-9]+)".r | ||
|
||
def protocolRight( | ||
mV: ModelVersion, | ||
rV: RevisionVersion = Monoid[RevisionVersion].empty, | ||
aV: AdditionVersion = Monoid[AdditionVersion].empty | ||
) = ProtocolVersion(mV, rV, aV).asRight[ProtocolVersionError] | ||
|
||
matcher.findAllIn(s).toList.map(_.toInt) match { | ||
case List(model, revision, addition) => | ||
protocolRight(ModelVersion(model), RevisionVersion(revision), AdditionVersion(addition)) | ||
case List(mode, revision) => | ||
protocolRight(ModelVersion(mode), RevisionVersion(revision)) | ||
case List(mode) => | ||
protocolRight(ModelVersion(mode)) | ||
case _ => ProtocolVersionError(s"$s is not a valid version string.").asLeft | ||
} | ||
} | ||
|
||
/** A simple val fpr defining de initial version: 1.0.0 */ | ||
val initial: ProtocolVersion = | ||
ProtocolVersion(ModelVersion(1), Monoid[RevisionVersion].empty, Monoid[AdditionVersion].empty) | ||
|
||
/** | ||
* E.g.: | ||
* | ||
* val ver = ProtocolVersion(ModelVersion(10), RevisionVersion(3), AdditionVersion(54)) | ||
* Show[ProtocolVersion].show(ProtocolVersion) == "10.3.54" | ||
* | ||
*/ | ||
implicit val protocolVersionShow: Show[ProtocolVersion] = | ||
Show.show(pv => s"${pv.model}.${pv.revision}.${pv.addition}") | ||
|
||
/** | ||
* E.g.: | ||
* | ||
* val ver1 = ProtocolVersion(ModelVersion(10), RevisionVersion(3), AdditionVersion(54)) | ||
* val ver2 = ProtocolVersion(ModelVersion(10), RevisionVersion(3), AdditionVersion(54)) | ||
* Eq[ProtocolVersion].eqv(ver1, ver2) == true | ||
* | ||
*/ | ||
implicit val protocolVersionEq: Eq[ProtocolVersion] = new Eq[ProtocolVersion] { | ||
def eqv(x: ProtocolVersion, y: ProtocolVersion): Boolean = | ||
x.model === y.model && x.revision === x.revision && x.addition === y.addition | ||
} | ||
|
||
/** | ||
* Syntax object for ProtocolVersion | ||
* | ||
* @param pv | ||
*/ | ||
implicit class ProtocolVersionOps(val pv: ProtocolVersion) extends AnyVal { | ||
def incAddition = ProtocolVersion.incAddition(pv) | ||
def incRevision = ProtocolVersion.incRevision(pv) | ||
def incModel = ProtocolVersion.incModel(pv) | ||
def setAddition(newAddition: AdditionVersion) = ProtocolVersion.setAddition(pv, newAddition) | ||
def setRevision(newRevision: RevisionVersion) = ProtocolVersion.setRevision(pv, newRevision) | ||
def setModel(newModel: ModelVersion) = ProtocolVersion.setModel(pv, newModel) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/scala/higherkindness/compendium/models/Tagged.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2018-2020 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package higherkindness.compendium.models | ||
|
||
import cats.{Eq, Show} | ||
import cats.implicits._ | ||
import io.circe.{Decoder, Encoder} | ||
import io.circe._ | ||
import shapeless.tag | ||
import shapeless.tag.@@ | ||
import org.http4s.QueryParamDecoder | ||
|
||
trait Tagged[T] { | ||
sealed trait Tag | ||
type Type = T @@ Tag | ||
def apply(t: T): T @@ Tag = tag[Tag][T](t) | ||
} | ||
|
||
object Tagged { | ||
|
||
abstract class DeriveCodecEqShow[T: Decoder: Encoder: Eq: Show] extends Tagged[T] { | ||
implicit val decoderTagged: Decoder[Type] = | ||
Decoder[T].map(apply) | ||
|
||
implicit val encoderTagged: Encoder[Type] = | ||
Encoder[T].narrow | ||
|
||
implicit val eqTagged: Eq[Type] = | ||
Eq[T].narrow | ||
|
||
implicit val showTagged: Show[Type] = | ||
Show[T].narrow | ||
} | ||
|
||
class Str extends DeriveCodecEqShow[String] { | ||
override implicit val showTagged: Show[Type] = Show[String].narrow | ||
} | ||
|
||
class Number extends DeriveCodecEqShow[Int] { | ||
override implicit val showTagged: Show[Type] = Show[Int].narrow | ||
} | ||
|
||
trait TaggedQueryParamDecoder[T] { self: Tagged[T] => | ||
implicit def queryParamDecoder(implicit base: QueryParamDecoder[T]): QueryParamDecoder[Type] = | ||
base.map(apply) | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/main/scala/higherkindness/compendium/models/package.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2018-2020 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package higherkindness.compendium | ||
|
||
import cats.kernel.Monoid | ||
|
||
package object models { | ||
|
||
type ModelVersion = ModelVersion.Type | ||
object ModelVersion extends Tagged.Number { | ||
implicit val majorVersionMonoid: Monoid[ModelVersion] = new Monoid[ModelVersion] { | ||
def combine(x: ModelVersion, y: ModelVersion): ModelVersion = ModelVersion(x + y) | ||
def empty: ModelVersion = ModelVersion(0) | ||
} | ||
} | ||
|
||
type RevisionVersion = RevisionVersion.Type | ||
object RevisionVersion extends Tagged.Number { | ||
implicit val minorVersionMonoid: Monoid[RevisionVersion] = new Monoid[RevisionVersion] { | ||
def combine(x: RevisionVersion, y: RevisionVersion): RevisionVersion = RevisionVersion(x + y) | ||
def empty: RevisionVersion = RevisionVersion(0) | ||
} | ||
} | ||
|
||
type AdditionVersion = AdditionVersion.Type | ||
object AdditionVersion extends Tagged.Number { | ||
implicit val patchVersionMonoid: Monoid[AdditionVersion] = new Monoid[AdditionVersion] { | ||
def combine(x: AdditionVersion, y: AdditionVersion): AdditionVersion = AdditionVersion(x + y) | ||
def empty: AdditionVersion = AdditionVersion(0) | ||
} | ||
} | ||
|
||
} |
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
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
66 changes: 66 additions & 0 deletions
66
src/test/scala/higherkindness/compendium/core/refinementsSpec.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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2018-2020 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package higherkindness.compendium.core | ||
|
||
import cats.effect.IO | ||
import cats.implicits._ | ||
import org.specs2.mutable.Specification | ||
import org.specs2.matcher.IOMatchers | ||
import org.specs2.ScalaCheck | ||
import org.scalacheck.Prop | ||
import higherkindness.compendium.CompendiumArbitrary._ | ||
import higherkindness.compendium.core.refinements.ProtocolVersionRefined | ||
import higherkindness.compendium.models.ProtocolVersionError | ||
|
||
object refinementsSpec extends Specification with ScalaCheck with IOMatchers { | ||
|
||
private val shortVersion: String => String = _.dropWhile(_ != '.').tail | ||
|
||
"ProtocolVersionRefined" >> { | ||
"parseOrRaise should parse a correct version of type xx.yy.zz" >> Prop.forAll( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
ProtocolVersionRefined.parseOrRaise[IO](rawVersion).map(_.show) must returnValue(rawVersion) | ||
} | ||
|
||
"parseOrRaise should parse a correct version of type xx.yy" >> Prop.forAll( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
val fixedVersion = shortVersion(rawVersion) | ||
ProtocolVersionRefined.parseOrRaise[IO](fixedVersion).map(_.show) must returnValue( | ||
fixedVersion |+| ".0" | ||
) | ||
} | ||
|
||
"parseOrRaise should parse a correct version of type xx" >> Prop.forAll( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
val fixedVersion = shortVersion(shortVersion(rawVersion)) | ||
ProtocolVersionRefined.parseOrRaise[IO](fixedVersion).map(_.show) must returnValue( | ||
fixedVersion |+| ".0.0" | ||
) | ||
} | ||
|
||
"parseOrRaise shoudl raise a ProtocolVersionError on error" >> Prop.forAll( | ||
invalidProtocolVersion.arbitrary | ||
) { rawVersion => | ||
ProtocolVersionRefined | ||
.parseOrRaise[IO](rawVersion) | ||
.unsafeRunSync() must throwA[ProtocolVersionError] | ||
} | ||
} | ||
} |
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
112 changes: 112 additions & 0 deletions
112
src/test/scala/higherkindness/compendium/models/ProtocolVersionSpec.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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2018-2020 47 Degrees, LLC. <http://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package higherkindness.compendium.models | ||
|
||
import cats.implicits._ | ||
import cats.kernel.Monoid | ||
import higherkindness.compendium.CompendiumArbitrary._ | ||
import higherkindness.compendium.models._ | ||
import higherkindness.compendium.models.ProtocolVersion._ | ||
import org.specs2.mutable.Specification | ||
import org.specs2.ScalaCheck | ||
import org.scalacheck.Prop | ||
|
||
object ProtocolVersionSpec extends Specification with ScalaCheck { | ||
|
||
"Set" >> { | ||
"setAddition should set the correct addition value" >> prop { (pv: ProtocolVersion, addVer: AdditionVersion) => | ||
pv.setAddition(addVer).addition === addVer | ||
} | ||
|
||
"setRevision should set the correct revision value and reset addition" >> prop { | ||
(pv: ProtocolVersion, revVer: RevisionVersion) => | ||
val version = pv.setRevision(revVer) | ||
version.revision === revVer && version.addition === Monoid[AdditionVersion].empty | ||
} | ||
|
||
"setModel should set the correct model value and reset revision and addition" >> prop { | ||
(pv: ProtocolVersion, modelVer: ModelVersion) => | ||
val version = pv.setModel(modelVer) | ||
version.model === modelVer && version.revision === Monoid[ | ||
RevisionVersion | ||
].empty && version.addition === Monoid[AdditionVersion].empty | ||
} | ||
} | ||
|
||
"Increment" >> { | ||
"incAddition should increment the addition" >> prop { protocolVersion: ProtocolVersion => | ||
protocolVersion.incAddition === protocolVersion.setAddition( | ||
protocolVersion.addition |+| AdditionVersion(1) | ||
) | ||
} | ||
|
||
"incRevision should increment the revision" >> prop { protocolVersion: ProtocolVersion => | ||
protocolVersion.incRevision === protocolVersion.setRevision( | ||
protocolVersion.revision |+| RevisionVersion(1) | ||
) | ||
} | ||
|
||
"incModel should increment the model" >> prop { protocolVersion: ProtocolVersion => | ||
protocolVersion.incModel === protocolVersion.setModel( | ||
protocolVersion.model |+| ModelVersion(1) | ||
) | ||
} | ||
} | ||
|
||
"Show" >> { | ||
"Show should create the correct string" >> prop { protocolVersion: ProtocolVersion => | ||
protocolVersion.show === show"${protocolVersion.model}.${protocolVersion.revision}.${protocolVersion.addition}" | ||
} | ||
} | ||
|
||
"Eq" >> { | ||
"Eq should work as expected" >> prop { protocolVersion: ProtocolVersion => | ||
protocolVersion === protocolVersion && protocolVersion.incAddition =!= protocolVersion | ||
} | ||
} | ||
|
||
"Parsing" >> { | ||
val shortVersion: String => String = _.dropWhile(_ != '.').tail | ||
|
||
"fromString should parse a correct version of type xx.yy.zz" >> Prop.forAllNoShrink( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
ProtocolVersion.fromString(rawVersion).map(_.show) must_== Right(rawVersion) | ||
} | ||
|
||
"fromString should parse a correct version of type xx.yy" >> Prop.forAllNoShrink( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
val fixedVersion = shortVersion(rawVersion) | ||
ProtocolVersion.fromString(fixedVersion).map(_.show) must_== Right(fixedVersion |+| ".0") | ||
} | ||
|
||
"fromString should parse a correct version of type xx" >> Prop.forAllNoShrink( | ||
rawProtocolVersionArb.arbitrary | ||
) { rawVersion => | ||
val fixedVersion = shortVersion(shortVersion(rawVersion)) | ||
ProtocolVersion.fromString(fixedVersion).map(_.show) must_== Right(fixedVersion |+| ".0.0") | ||
} | ||
|
||
"fromString should return a Left(ProtocolVersionError) on error" >> Prop.forAllNoShrink( | ||
invalidProtocolVersion.arbitrary | ||
) { rawVersion => | ||
ProtocolVersion.fromString(rawVersion) must beLeft[ProtocolVersionError] | ||
} | ||
} | ||
|
||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this here because of the tests, but I guess the correct way would be to create a migration?