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

Unique generation config #52

Merged
merged 16 commits into from
Dec 13, 2020
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and the project versioning adheres to [Semantic Versioning](https://semver.org/s
### Added
- [#44](https://github.com/serpro69/kotlin-faker/pull/44) [core] Add support for random instance configuration.
- [#47](https://github.com/serpro69/kotlin-faker/issues/47) [core] Publish release candidates to bintray
- [#49](https://github.com/serpro69/kotlin-faker/issues/49) [core] Unique values exclusions with wildcards

### Fixed
- [#48](https://github.com/serpro69/kotlin-faker/issues/48) [core] streetFighter#moves: class java.util.LinkedHashMap cannot be cast to class java.lang.String
- [#50](https://github.com/serpro69/kotlin-faker/issues/50) [core] Horseman spelt wrong

### Changed
- [core] Configuration for generation of unique values. Old functionality is deprecated and will be removed in future releases. This relates to changes in [#49](https://github.com/serpro69/kotlin-faker/issues/49)

## [v1.5.0] - 2020-08-30
### Added
Expand Down
2 changes: 1 addition & 1 deletion LICENCE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Sergii Prodanov
Copyright (c) 2020 Sergii Prodanov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
104 changes: 73 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
### <a href="https://github.com/serpro69/kotlin-faker"> <img src=./logo/name.png alt="kotlin-faker"/> </a>

> **Generates realistically-looking fake data**
> Just like this fake-logo, but not quite so.
> <img src=./logo/kotlin_faker.png height="144" alt="fake-logo"/>

[![Build Status](https://travis-ci.org/serpro69/kotlin-faker.svg?branch=master)](https://travis-ci.org/serpro69/kotlin-faker)
[![Version Badge](https://api.bintray.com/packages/serpro69/maven/kotlin-faker/images/download.svg) ](https://bintray.com/serpro69/maven/kotlin-faker/_latestVersion)
[![RC Version Badge](https://api.bintray.com/packages/serpro69/maven-release-candidates/kotlin-faker/images/download.svg) ](https://bintray.com/serpro69/maven-release-candidates/kotlin-faker/_latestVersion)
Expand All @@ -7,12 +12,8 @@
[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin)
[![Licence Badge](https://img.shields.io/github/license/serpro69/kotlin-faker.svg)](LICENCE.md)

> **Generates realistically-looking fake data**
>
> <img src=./logo/kotlin_faker.png height="144" alt="fake-logo"/>


## ToC

- [About](#about)
- [Comparison with other JVM-based faker libraries](#comparison-with-other-jvm-based-faker-libs)
- [Usage](#usage)
Expand All @@ -36,14 +37,14 @@
- [Thanks](#thanks)
- [Licence](#licence)


## About

Port of a popular ruby [faker](https://github.com/stympy/faker) gem written completely in kotlin.
Generates realistically looking fake data such as names, addresses, banking details, and many more,
that can be used for testing purposes during development and testing.


## Comparison with other jvm-based faker libs

While there are several other libraries out there with similar functionalities,
I had several reasons for creating this one:
- most of the ones I've found are written in java and I wanted to use kotlin
Expand Down Expand Up @@ -72,9 +73,10 @@ benchmarks for `moove-it/fakeit` could not be done due to android dependencies i
| **[cli-bot app](cli-bot)** | &#9989; | &#10062; | &#10062; | &#10062; | &#10062; |
| **benchmarks** | 5482ms | 17529.9ms | 15036.5ms | NA | NA |


## Usage

### Downloading

Latest releases are always available on [jcenter](https://bintray.com/bintray/jcenter).

From `v1.4.1` onward releases are also published on maven central.
Expand Down Expand Up @@ -130,6 +132,7 @@ The jar and pom files can also be found at this [link](https://dl.bintray.com/se


### Generating data

```kotlin
val faker = Faker()

Expand All @@ -138,13 +141,16 @@ faker.address.city() // => New York
```

### Configuring Faker

#### Default configuration

If no `FakerConfig` instance is passed to `Faker` constructor then default configuration will be used:
- `locale` is set to `en`
- `random` is seeded with a pseudo-randomly generated number.
- `uniqueGeneratorRetryLimit` is set to `100`

#### Deterministic Random

Faker supports seeding of it's PRNG (pseudo-random number generator) through configuration to
provide deterministic output of repeated function invocations.

Expand All @@ -166,14 +172,18 @@ name1 == name2 // => true
```

#### Generating unique values
Faker supports generation of unique values. There are two ways to generate unique values:

Faker supports generation of unique values. There are basically two ways to generate unique values:

**Unique values for entire provider**
```kotlin
val faker = Faker()
faker.unique.enable(faker::address) // enable generation of unique values for address provider
faker.unique.configure {
enable(faker::address) // enable generation of unique values for address provider
}

repeat(10) { faker.address.country() } // will generate unique country each time it's called
// will generate unique country each time it's called
repeat(10) { faker.address.country() }
```

To clear the record of unique values that were already generated:
Expand Down Expand Up @@ -221,33 +231,65 @@ val faker = Faker(config)
```

**Excluding values from generation**
It is possible to exclude values from being generated with unique generator:
It is possible to exclude values from being generated with unique generator.
This is configured on the `faker` level for each (or in some cases - all) of the providers.

```kotlin
val faker = Faker()
faker.unique.enable(faker::address)

val excludedCountries = listOf(
"Afghanistan",
"Albania",
"Algeria",
"American Samoa",
"Andorra",
"Angola"
)

faker.unique.exclude<Address>("country", excludedCountries)

// in addition to generating unique values
// this will not generate any of the excluded countries as well
faker.address.country()

faker.unique.configuration {
// Enable generation of unique values for Address provider
// Any unique generation configuration will only affect "enabled" providers
enable(faker::address)

// Exclude listOfValues from being generated
// in all providers that are enabled for unique generation
exclude(listOfValues)

// Exclude values starting with "A" from being generated
// in all providers that are enabled for unique generation
exclude { listOf(Regex("^A")) }

// Additional configuration for particular provider
// First enable generation of unique values for Name provider
enable(faker::name) {
// Exclude listOfNames from being generated by any Name provider function
excludeFromProvider<Name>(listOfNames)

// Exclude listOfLastNames from being generated by Name#lastName function
excludeFromFunction(Name::lastName, listOfLastNames)

// Exclude values starting with "B" from being generated by any Name provider function
excludeFromProvider<Name> { listOf(Regex("^B")) }

// Exclude values starting with "C" from being generated by Name#country function
excludeFromFunction(Name::lastName) { listOf(Regex("^C")) }
}
}

// Based on the above config the following will be true in addition to generating unique values:
val city = faker.address.city()
assertTrue(listOfValues.contains(city) == false)
assertTrue(city.startsWith("A") == false)

val firstName = faker.name.firstName()
val lastName = faker.name.lastName()
assertTrue(listOfValues.contains(firstName) == false)
assertTrue(listOfValues.contains(lastName) == false)
assertTrue(listOfNames.contains(firstName) == false)
assertTrue(listOfNames.contains(lastName) == false)
assertTrue(listOfLastNames.contains(lastName) == false)
assertTrue(firstName.startsWith("A") == false)
assertTrue(lastName.startsWith("A") == false)
assertTrue(firstName.startsWith("B") == false)
assertTrue(lastName.startsWith("B") == false)
assertTrue(lastName.startsWith("C") == false)
```

This is only applicable when the whole category, i.e. `Address` is enabled for unique generation
of values.
This is only applicable when the whole category, i.e. `Address` or `Name` is enabled for unique generation of values.

```kotlin
faker.address.unique.country() // will still generate unique values, but won't consider exclusions if any
faker.address.unique.country() // will still generate unique values, but won't consider exclusions, if any
```

#### Localized dictionary
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/io/github/serpro69/kfaker/Faker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package io.github.serpro69.kfaker

import io.github.serpro69.kfaker.provider.*
import io.github.serpro69.kfaker.provider.unique.GlobalUniqueDataDataProvider

/**
* Object provides functionality to generate fake data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.exception.*
import io.github.serpro69.kfaker.Faker
import io.github.serpro69.kfaker.FakerService
import io.github.serpro69.kfaker.dictionary.Category
import io.github.serpro69.kfaker.dictionary.CategoryName
import io.github.serpro69.kfaker.exception.RetryLimitException
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import org.slf4j.LoggerFactory

/**
* Abstract class for all concrete [FakeDataProvider]'s.
Expand All @@ -14,6 +18,7 @@ import io.github.serpro69.kfaker.exception.*
abstract class AbstractFakeDataProvider<T : FakeDataProvider> internal constructor(
internal val fakerService: FakerService
) : FakeDataProvider {
private val log = LoggerFactory.getLogger(this::class.java)

/**
* Name of the category for `this` fake data provider class.
Expand Down Expand Up @@ -141,28 +146,85 @@ abstract class AbstractFakeDataProvider<T : FakeDataProvider> internal construct
)
}
}
} else if (!globalUniqueProvider.markedUnique.contains(this::class)) {
} else if (!globalUniqueProvider.config.markedUnique.contains(this::class)) {
// if global unique provider is not enabled for this category -> return result
result
} else {
val usedValuesMap = requireNotNull(globalUniqueProvider.usedValues[this::class])
when (val set = usedValuesMap[key]) {
null -> {
usedValuesMap[key] = mutableSetOf(result)
result
val exclusionValues = globalUniqueProvider.config.excludedValues
val exclusionPatterns = globalUniqueProvider.config.excludedPatterns
val usedProviderValues = requireNotNull(globalUniqueProvider.config.usedProviderValues[this::class])
val providerExclusionPatterns = requireNotNull(globalUniqueProvider.config.providerExclusionPatterns[this::class])
val providerFunctionExclusionPatternsMap = requireNotNull(globalUniqueProvider.config.providerFunctionExclusionPatterns[this::class])
val usedProviderFunctionsValuesMap = requireNotNull(globalUniqueProvider.config.usedProviderFunctionValues[this::class])

when {
// Globally excluded values
exclusionValues.isNotEmpty() && exclusionValues.contains(result) -> {
returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
else -> {
if (counter >= fakerConfig.uniqueGeneratorRetryLimit) {
throw RetryLimitException("Retry limit of $counter exceeded")
} else if (!set.contains(result)) result.also {
usedValuesMap[key] = mutableSetOf(result).also { it.addAll(set) }
} else returnOrResolveUnique(
// Global exclusion patterns
exclusionPatterns.isNotEmpty() && exclusionPatterns.any { r -> r.containsMatchIn(result) } -> {
returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
// Provider-based excluded values for all functions
usedProviderValues.isNotEmpty() && usedProviderValues.contains(result) -> {
returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
// Provider-based exclusion patterns for all functions
providerExclusionPatterns.isNotEmpty() && providerExclusionPatterns.any { it.containsMatchIn(result) } -> {
returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
else -> {
val patterns = providerFunctionExclusionPatternsMap[key]
val usedValues = usedProviderFunctionsValuesMap[key]

when {
patterns != null && patterns.isNotEmpty() && patterns.any { r -> r.containsMatchIn(result) } -> {
returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
usedValues == null -> {
usedProviderFunctionsValuesMap[key] = mutableSetOf(result)
result
}
else -> {
if (counter >= fakerConfig.uniqueGeneratorRetryLimit) {
throw RetryLimitException("Retry limit of $counter exceeded")
} else if (!usedValues.contains(result)) result.also {
usedProviderFunctionsValuesMap[key] = mutableSetOf(result).also { it.addAll(usedValues) }
} else returnOrResolveUnique(
primaryKey = primaryKey,
secondaryKey = secondaryKey,
thirdKey = thirdKey,
counter = counter + 1
)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.ADDRESS] category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.ANCIENT] category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.CREATURE] category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.APP] category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.APPLIANCE] category.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.serpro69.kfaker.provider

import io.github.serpro69.kfaker.*
import io.github.serpro69.kfaker.dictionary.*
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate

/**
* [FakeDataProvider] implementation for [CategoryName.AQUA_TEEN_HUNGER_FORCE] category.
Expand Down
Loading