Skip to content

Commit

Permalink
Add 'commerce' provider module
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Feb 16, 2024
1 parent 5d07f10 commit 244a90b
Show file tree
Hide file tree
Showing 28 changed files with 154 additions and 84 deletions.
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
// integration dependencies for classes in docs package, e.g. Homepage.kt
// NB! only add providers that are needed
val integrationImplementation by configurations
integrationImplementation(project(":provider:commerce"))
integrationImplementation(project(":provider:movies"))
integrationImplementation(project(":provider:tvshows"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,8 @@ class UniqueDataProviderIT : DescribeSpec({
"Block", "Bode", "Boehm", "Bogan", "Bogisich", "Borer", "Bosco", "Botsford", "Boyer", "Boyle",
"Bradtke", "Brakus", "Braun", "Breitenberg", "Brekke", "Brown", "Bruen", "Buckridge"
)
val excludedBicCodes = listOf(
"AACCGB21", "AACNGB21", "AAFMGB21", "AAHOGB21", "AAHVGB21", "AANLGB21",
"AANLGB2L", "AAOGGB21", "AAPEGB21", "AAPUGB21", "AAQIGB21", "ABBYGB2L",
"BCYPGB2LCBB", "BCYPGB2LHGB", "BCYPGB2LHHB", "BCYPGB2LPGB", "BCYPGB2LSSB", "BCYPGB2LMBB"
)
val excludeAll = listOf(excludedCountries, excludedNames, excludedBicCodes).flatten()
val excludedDomains = listOf("com", "biz", "info")
val excludeAll = listOf(excludedCountries, excludedNames, excludedDomains).flatten()

faker.unique.configuration {
enable(faker::address)
Expand All @@ -125,16 +121,16 @@ class UniqueDataProviderIT : DescribeSpec({
context("collection of unique values is generated run#$it") {
val countries = (0..30).map { faker.address.country() }
val names = (0..30).map { faker.name.lastName() }
// Unique generation not enabled for Bank
val bicCodes = (0..30).map { faker.bank.swiftBic() }
// Unique generation not enabled for Internet
val domainSuffixes = (0..30).map { faker.internet.domainSuffix() }

it("should not contain excluded values") {
assertSoftly {
countries shouldNotContainAnyOf excludeAll
names shouldNotContainAnyOf excludeAll
// Unique generation not enabled for Bank
bicCodes shouldNot beUnique()
bicCodes shouldContainAnyOf excludedBicCodes
// Unique generation not enabled for Internet
domainSuffixes shouldNot beUnique()
domainSuffixes shouldContainAnyOf excludedDomains
}
}
}
Expand All @@ -150,23 +146,23 @@ class UniqueDataProviderIT : DescribeSpec({
enable(faker::address)
enable(faker::name)
// Exclude all values starting with "A"
exclude { listOf(Regex("^A")) }
exclude { listOf(Regex("^[Cc]")) }
}

it("should not contain values matching pattern run#$it") {
val countries = (0..30).map { faker.address.country() }
val names = (0..30).map { faker.name.lastName() }
// Unique generation not enabled for Bank
val bicCodes = (0..30).map { faker.bank.swiftBic() }
val domainSuffixes = (0..30).map { faker.internet.domainSuffix() }

assertSoftly {
countries.none { s -> s.startsWith("A") } shouldBe true
countries.none { s -> s.startsWith("C") } shouldBe true
countries should beUnique()
names.none { s -> s.startsWith("A") } shouldBe true
names.none { s -> s.startsWith("C") } shouldBe true
names should beUnique()
// Unique generation not enabled for Bank
bicCodes.any { s -> s.startsWith("A") } shouldBe true
bicCodes shouldNot beUnique()
// Unique generation not enabled for Internet
domainSuffixes.any { s -> s.startsWith("c") } shouldBe true
domainSuffixes shouldNot beUnique()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.serpro69.kfaker.docs

import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.commerce.CommerceFaker
import io.github.serpro69.kfaker.fakerConfig
import io.github.serpro69.kfaker.movies.MoviesFaker
import io.github.serpro69.kfaker.tv.TvShowsFaker
Expand Down Expand Up @@ -44,7 +45,7 @@ class Homepage : DescribeSpec({
}
it("should print a SWIFT BIC code") {
// START data_provider_four
faker.bank.swiftBic() // => AACCGB21
CommerceFaker().bank.swiftBic() // => AACCGB21
// END data_provider_four
}
it("should print a safe email address") {
Expand Down
36 changes: 1 addition & 35 deletions core/src/main/kotlin/io/github/serpro69/kfaker/Faker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@ import io.github.serpro69.kfaker.provider.Ancient
import io.github.serpro69.kfaker.provider.Animal
import io.github.serpro69.kfaker.provider.Artist
import io.github.serpro69.kfaker.provider.Australia
import io.github.serpro69.kfaker.provider.Bank
import io.github.serpro69.kfaker.provider.Barcode
import io.github.serpro69.kfaker.provider.Beer
import io.github.serpro69.kfaker.provider.Bible
import io.github.serpro69.kfaker.provider.Bird
import io.github.serpro69.kfaker.provider.Blood
import io.github.serpro69.kfaker.provider.BossaNova
import io.github.serpro69.kfaker.provider.Business
import io.github.serpro69.kfaker.provider.Cannabis
import io.github.serpro69.kfaker.provider.Cat
import io.github.serpro69.kfaker.provider.Chiquito
import io.github.serpro69.kfaker.provider.ChuckNorris
import io.github.serpro69.kfaker.provider.Code
import io.github.serpro69.kfaker.provider.Coffee
import io.github.serpro69.kfaker.provider.Coin
import io.github.serpro69.kfaker.provider.Color
import io.github.serpro69.kfaker.provider.Commerce
import io.github.serpro69.kfaker.provider.Company
import io.github.serpro69.kfaker.provider.Construction
import io.github.serpro69.kfaker.provider.Cosmere
import io.github.serpro69.kfaker.provider.Currency
import io.github.serpro69.kfaker.provider.CurrencySymbol
Expand All @@ -39,8 +30,6 @@ import io.github.serpro69.kfaker.provider.DrivingLicense
import io.github.serpro69.kfaker.provider.Educator
import io.github.serpro69.kfaker.provider.Emotion
import io.github.serpro69.kfaker.provider.File
import io.github.serpro69.kfaker.provider.Finance
import io.github.serpro69.kfaker.provider.Food
import io.github.serpro69.kfaker.provider.FunnyName
import io.github.serpro69.kfaker.provider.Gender
import io.github.serpro69.kfaker.provider.GreekPhilosophers
Expand All @@ -50,14 +39,12 @@ import io.github.serpro69.kfaker.provider.Hobby
import io.github.serpro69.kfaker.provider.Horse
import io.github.serpro69.kfaker.provider.House
import io.github.serpro69.kfaker.provider.IdNumber
import io.github.serpro69.kfaker.provider.IndustrySegments
import io.github.serpro69.kfaker.provider.Internet
import io.github.serpro69.kfaker.provider.JackHandey
import io.github.serpro69.kfaker.provider.Job
import io.github.serpro69.kfaker.provider.KamenRider
import io.github.serpro69.kfaker.provider.Lorem
import io.github.serpro69.kfaker.provider.Markdown
import io.github.serpro69.kfaker.provider.Marketing
import io.github.serpro69.kfaker.provider.Measurement
import io.github.serpro69.kfaker.provider.Military
import io.github.serpro69.kfaker.provider.MitchHedberg
Expand All @@ -71,7 +58,6 @@ import io.github.serpro69.kfaker.provider.PhoneNumber
import io.github.serpro69.kfaker.provider.Quote
import io.github.serpro69.kfaker.provider.Rajnikanth
import io.github.serpro69.kfaker.provider.Relationship
import io.github.serpro69.kfaker.provider.Restaurant
import io.github.serpro69.kfaker.provider.Room
import io.github.serpro69.kfaker.provider.Science
import io.github.serpro69.kfaker.provider.Separator
Expand All @@ -80,10 +66,8 @@ import io.github.serpro69.kfaker.provider.Show
import io.github.serpro69.kfaker.provider.SlackEmoji
import io.github.serpro69.kfaker.provider.Sport
import io.github.serpro69.kfaker.provider.Stripe
import io.github.serpro69.kfaker.provider.Subscription
import io.github.serpro69.kfaker.provider.Superhero
import io.github.serpro69.kfaker.provider.Tarkov
import io.github.serpro69.kfaker.provider.Tea
import io.github.serpro69.kfaker.provider.Team
import io.github.serpro69.kfaker.provider.TheThickOfIt
import io.github.serpro69.kfaker.provider.Tolkien
Expand Down Expand Up @@ -128,27 +112,18 @@ class Faker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }): Ab
val animal: Animal by lazy { Animal(fakerService) }
val artist: Artist by lazy { Artist(fakerService) }
val australia: Australia by lazy { Australia(fakerService) }
val bank: Bank by lazy { Bank(fakerService) }
val barcode: Barcode by lazy { Barcode(fakerService) }
val beer: Beer by lazy { Beer(fakerService) }
val bible: Bible by lazy { Bible(fakerService) }
val bird: Bird by lazy { Bird(fakerService) }
val blood: Blood by lazy { Blood(fakerService) }
val bossaNova: BossaNova by lazy { BossaNova(fakerService) }
val business: Business by lazy { Business(fakerService) }
val cannabis: Cannabis by lazy { Cannabis(fakerService) }
val cat: Cat by lazy { Cat(fakerService) }
val chiquito: Chiquito by lazy { Chiquito(fakerService) }
val chuckNorris: ChuckNorris by lazy { ChuckNorris(fakerService) }
val code: Code by lazy { Code(fakerService) }
val coffee: Coffee by lazy { Coffee(fakerService) }
val coin: Coin by lazy { Coin(fakerService) }
val color: Color by lazy { Color(fakerService) }
val commerce: Commerce by lazy { Commerce(fakerService) }
val company: Company by lazy { Company(fakerService) }

// val compass: Compass by lazy {Compass(fakerService) }
val construction: Construction by lazy { Construction(fakerService) }
val cosmere: Cosmere by lazy { Cosmere(fakerService) }
val currency: Currency by lazy { Currency(fakerService) }
val dcComics: DcComics by lazy { DcComics(fakerService) }
Expand All @@ -159,8 +134,6 @@ class Faker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }): Ab
val educator: Educator by lazy { Educator(fakerService) }
val emotion: Emotion by lazy { Emotion(fakerService) }
val file: File by lazy { File(fakerService) }
val finance: Finance by lazy { Finance(fakerService) }
val food: Food by lazy { Food(fakerService) }
val funnyName: FunnyName by lazy { FunnyName(fakerService) }
val gender: Gender by lazy { Gender(fakerService) }
val greekPhilosophers: GreekPhilosophers by lazy { GreekPhilosophers(fakerService) }
Expand All @@ -170,16 +143,12 @@ class Faker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }): Ab
val horse: Horse by lazy { Horse(fakerService) }
val house: House by lazy { House(fakerService) }
val idNumber: IdNumber by lazy { IdNumber(fakerService) }
val industrySegments: IndustrySegments by lazy { IndustrySegments(fakerService) }
val internet: Internet by lazy { Internet(fakerService, company, name) }

// val invoice: Invoice by lazy {Invoice(fakerService }
val internet: Internet by lazy { Internet(fakerService, name) }
val jackHandey: JackHandey by lazy { JackHandey(fakerService) }
val job: Job by lazy { Job(fakerService) }
val kamenRider: KamenRider by lazy { KamenRider(fakerService) }
val lorem: Lorem by lazy { Lorem(fakerService) }
val markdown: Markdown by lazy { Markdown(fakerService) }
val marketing: Marketing by lazy { Marketing(fakerService) }
val measurement: Measurement by lazy { Measurement(fakerService) }
val military: Military by lazy { Military(fakerService) }
val mitchHedberg: MitchHedberg by lazy { MitchHedberg(fakerService) }
Expand All @@ -193,7 +162,6 @@ class Faker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }): Ab
val quote: Quote by lazy { Quote(fakerService) }
val rajnikanth: Rajnikanth by lazy { Rajnikanth(fakerService) }
val relationship: Relationship by lazy { Relationship(fakerService) }
val restaurant: Restaurant by lazy { Restaurant(fakerService) }
val room: Room by lazy { Room(fakerService) }
val science: Science by lazy { Science(fakerService) }
val shakespeare: Shakespeare by lazy { Shakespeare(fakerService) }
Expand All @@ -203,10 +171,8 @@ class Faker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }): Ab
// val source: Source by lazy {Source(fakerService }
val sport: Sport by lazy { Sport(fakerService) }
val stripe: Stripe by lazy { Stripe(fakerService) }
val subscription: Subscription by lazy { Subscription(fakerService) }
val superhero: Superhero by lazy { Superhero(fakerService) }
val tarkov: Tarkov by lazy { Tarkov(fakerService) }
val tea: Tea by lazy { Tea(fakerService) }
val team: Team by lazy { Team(fakerService) }
val theThickOfIt: TheThickOfIt by lazy { TheThickOfIt(fakerService) }
val tolkien: Tolkien by lazy { Tolkien(fakerService) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.FakerService
import io.github.serpro69.kfaker.dictionary.YamlCategory
import io.github.serpro69.kfaker.faker
import io.github.serpro69.kfaker.helper.isReservedNet
import io.github.serpro69.kfaker.helper.prepare
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
Expand All @@ -14,7 +15,6 @@ import java.lang.String.format
@Suppress("unused")
class Internet internal constructor(
fakerService: FakerService,
private val companyProvider: Company,
private val nameProvider: Name,
) : YamlFakeDataProvider<Internet>(fakerService) {
override val yamlCategory = YamlCategory.INTERNET
Expand All @@ -26,7 +26,7 @@ class Internet internal constructor(
}

fun domain(subdomain: Boolean = false, domain: String? = null): String {
val name: () -> String = { prepare(companyProvider.name().split(" ").first(), fakerService.faker.config) }
val name: () -> String = { prepare(nameProvider.lastName().split(" ").first(), fakerService.faker.config) }
return domain?.let {
domain.split(".")
.map { domainPart -> prepare(domainPart, fakerService.faker.config) }
Expand Down Expand Up @@ -135,3 +135,10 @@ private val privateIpv4Ranges = listOf(
// 198.18.0.0/15 - Used for benchmark testing of inter-network communications between subnets
listOf(198..198, 18..19, 0..255, 1..255)
)

fun main() {
val f = faker { }
repeat(100) {
println(f.internet.domain())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import sun.net.util.IPAddressUtil
class InternetTest: DescribeSpec({
describe("Internet provider") {
val s = FakerService(Faker())
val internet = Internet(s, Company(s), Name(s))
val internet = Internet(s, Name(s))

context("IPv4 address generation") {
repeat(100) {
Expand Down
4 changes: 4 additions & 0 deletions provider/commerce/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins {
`faker-lib-conventions`
`faker-provider-conventions`
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.github.serpro69.kfaker.provider
package io.github.serpro69.kfaker.commerce.provider

import io.github.serpro69.kfaker.faker
import io.github.serpro69.kfaker.commerce.faker
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldMatch

class CompanyIT : DescribeSpec({
describe("Company Provider") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.github.serpro69.kfaker.provider
package io.github.serpro69.kfaker.commerce.provider

import io.github.serpro69.kfaker.faker
import io.github.serpro69.kfaker.commerce.faker
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldNotHave
import io.kotest.matchers.string.shouldNotContain

class FinanceIT : DescribeSpec({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.github.serpro69.kfaker.commerce

import io.github.serpro69.kfaker.AbstractFaker
import io.github.serpro69.kfaker.FakerConfig
import io.github.serpro69.kfaker.FakerDsl
import io.github.serpro69.kfaker.commerce.provider.Bank
import io.github.serpro69.kfaker.commerce.provider.Barcode
import io.github.serpro69.kfaker.commerce.provider.Beer
import io.github.serpro69.kfaker.commerce.provider.Business
import io.github.serpro69.kfaker.commerce.provider.Cannabis
import io.github.serpro69.kfaker.commerce.provider.Coffee
import io.github.serpro69.kfaker.commerce.provider.Commerce
import io.github.serpro69.kfaker.commerce.provider.Company
import io.github.serpro69.kfaker.commerce.provider.Construction
import io.github.serpro69.kfaker.commerce.provider.Finance
import io.github.serpro69.kfaker.commerce.provider.Food
import io.github.serpro69.kfaker.commerce.provider.IndustrySegments
import io.github.serpro69.kfaker.commerce.provider.Marketing
import io.github.serpro69.kfaker.provider.Money
import io.github.serpro69.kfaker.commerce.provider.Restaurant
import io.github.serpro69.kfaker.commerce.provider.Subscription
import io.github.serpro69.kfaker.commerce.provider.Tea
import io.github.serpro69.kfaker.fakerConfig

/**
* Typealias for the [CommerceFaker]
*/
typealias Faker = CommerceFaker

/**
* Provides access to fake data generators withing the Books domain.
*
* Each category (generator) from this [CommerceFaker] is represented by a property that has the same name as the `.yml` file.
*
* @property unique global provider for generation of unique values.
*/
@Suppress("unused")
class CommerceFaker @JvmOverloads constructor(config: FakerConfig = fakerConfig { }) : AbstractFaker(config) {

val bank: Bank by lazy { Bank(fakerService) }
val barcode: Barcode by lazy { Barcode(fakerService) }
val beer: Beer by lazy { Beer(fakerService) }
val business: Business by lazy { Business(fakerService) }
val cannabis: Cannabis by lazy { Cannabis(fakerService) }
val coffee: Coffee by lazy { Coffee(fakerService) }
val commerce: Commerce by lazy { Commerce(fakerService) }
val company: Company by lazy { Company(fakerService) }
val construction: Construction by lazy { Construction(fakerService) }
val finance: Finance by lazy { Finance(fakerService, randomService) }
val food: Food by lazy { Food(fakerService) }
val industrySegments: IndustrySegments by lazy { IndustrySegments(fakerService) }
// TODO not implemented
// val invoice: Invoice by lazy { Invoice(fakerService }
val marketing: Marketing by lazy { Marketing(fakerService) }
val restaurant: Restaurant by lazy { Restaurant(fakerService) }
val subscription: Subscription by lazy { Subscription(fakerService) }
val tea: Tea by lazy { Tea(fakerService) }

@FakerDsl
/**
* DSL builder for creating instances of [Faker]
*/
class Builder internal constructor() : AbstractFaker.Builder<Faker>() {

/**
* Builds an instance of [Faker] with this [config].
*/
override fun build(): Faker = Faker(config)
}
}

/**
* Applies the [block] function to [CommerceFaker.Builder]
* and returns as an instance of [CommerceFaker] from that builder.
*/
fun faker(block: CommerceFaker.Builder.() -> Unit): CommerceFaker = CommerceFaker.Builder().apply(block).build()
Loading

0 comments on commit 244a90b

Please sign in to comment.