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

testInsert: specific support for domains #112

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion site-in/other-features/testing-with-random-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ The idea is that you:

In summary, this is a fantastic way of setting up complex test scenarios in the database!

### Domains
If you use [postgres domains](../type-safety/domains.md) you typically want affect the generation of data yourself.
For that reason there is a trait you need to implement and pass in. This only affect you if you use domains.

```scala mdoc
import adventureworks.public.*

import scala.util.Random

// apply domain-specific rules here
object DomainInsert extends adventureworks.TestDomainInsert {
override def publicAccountNumber(random: Random): AccountNumber = AccountNumber(random.nextString(10))
override def publicFlag(random: Random): Flag = Flag(random.nextBoolean())
override def publicMydomain(random: Random): Mydomain = Mydomain(random.nextString(10))
override def publicName(random: Random): Name = Name(random.nextString(10))
override def publicNameStyle(random: Random): NameStyle = NameStyle(random.nextBoolean())
override def publicPhone(random: Random): Phone = Phone(random.nextString(10))
override def publicShortText(random: Random): ShortText = ShortText(random.nextString(10))
}
```

### Usage example

Expand All @@ -34,7 +54,7 @@ import adventureworks.TestInsert

import scala.util.Random

val testInsert = new TestInsert(new Random(0))
val testInsert = new TestInsert(new Random(0), DomainInsert)

val unitmeasure = testInsert.productionUnitmeasure(UnitmeasureId("kgg"))
val productCategory = testInsert.productionProductcategory()
Expand Down
17 changes: 15 additions & 2 deletions site-in/patterns/multi-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,25 @@ Here is example usage:

Note that we can easily create a deep dependency graph with random data due to [testInsert](../other-features/testing-with-random-values.md).
```scala mdoc:silent
import adventureworks.{TestInsert, withConnection}
import adventureworks.{TestInsert, TestDomainInsert, withConnection}
import adventureworks.userdefined.FirstName
import scala.util.Random
import adventureworks.public.{AccountNumber, Flag, Mydomain, Name, NameStyle, Phone, ShortText}

import scala.util.Random

object DomainInsert extends TestDomainInsert {
override def publicAccountNumber(random: Random): AccountNumber = AccountNumber(random.nextString(10))
override def publicFlag(random: Random): Flag = Flag(random.nextBoolean())
override def publicMydomain(random: Random): Mydomain = Mydomain(random.nextString(10))
override def publicName(random: Random): Name = Name(random.nextString(10))
override def publicNameStyle(random: Random): NameStyle = NameStyle(random.nextBoolean())
override def publicPhone(random: Random): Phone = Phone(random.nextString(10))
override def publicShortText(random: Random): ShortText = ShortText(random.nextString(10))
}

// set a fixed seed to get consistent values
val testInsert = new TestInsert(new Random(1))
val testInsert = new TestInsert(new Random(1), DomainInsert)

val businessentityRow = testInsert.personBusinessentity()
val personRow = testInsert.personPerson(businessentityRow.businessentityid, persontype = "SC", FirstName("name"))
Expand Down
2 changes: 1 addition & 1 deletion site-in/what-is/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Neither aggregation nor projection is allowed — you always work on whole rows.
On the other hand, you get predicates, joins, ordering, pagination.

It's designed to cover the "I just need to fetch/update this data" scenario, without requiring you to break flow by creating an sql file.
Whenever you need more flexibility, you should reach for [SQL files](what-is/sql-is-king.md)
Whenever you need more flexibility, you should reach for [SQL files](sql-is-king.md)

**Type Safety**:
With the Typo DSL, you benefit from Scala's robust type system, ensuring that your queries are
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* File has been automatically generated by `typo`.
*
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN.
*/
package adventureworks

import adventureworks.public.AccountNumber
import adventureworks.public.Flag
import adventureworks.public.Mydomain
import adventureworks.public.Name
import adventureworks.public.NameStyle
import adventureworks.public.Phone
import adventureworks.public.ShortText
import scala.util.Random

trait TestDomainInsert {
/** Domain `public.AccountNumber`
* No constraint
*/
def publicAccountNumber(random: Random): AccountNumber
/** Domain `public.Flag`
* No constraint
*/
def publicFlag(random: Random): Flag
/** Domain `public.mydomain`
* No constraint
*/
def publicMydomain(random: Random): Mydomain
/** Domain `public.Name`
* No constraint
*/
def publicName(random: Random): Name
/** Domain `public.NameStyle`
* No constraint
*/
def publicNameStyle(random: Random): NameStyle
/** Domain `public.Phone`
* No constraint
*/
def publicPhone(random: Random): Phone
/** Domain `public.short_text`
* Constraint: CHECK ((length(VALUE) <= 55))
*/
def publicShortText(random: Random): ShortText
}
Loading
Loading