Giter Club home page Giter Club logo

serpro69 / kotlin-faker Goto Github PK

View Code? Open in Web Editor NEW
430.0 3.0 41.0 9.89 MB

Port of a popular ruby faker gem written in kotlin. Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.

Home Page: https://serpro69.github.io/kotlin-faker/

License: MIT License

Kotlin 97.90% Groovy 0.15% Java 0.53% Shell 0.82% Makefile 0.60%
kotlin kotlin-library testing faker data test-automation testing-tools kotlin-faker faker-generator faker-gem

kotlin-faker's Introduction

Stand With Ukraine

Generates realistically-looking fake data
Just like this fake-logo, but not quite so.
fake-logo

Build Status Maven Central Sonatype Nexus (Snapshots) Issues GitHub Top Lang Awesome Kotlin Licence

ToC

About

Port of a popular ruby faker gem written in kotlin. Generates realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data-anonymization purposes.

Prerequisites

  • Faker libraries depend on java 8 and kotlin 1.9.x for runtime
  • Building faker from source requires 3 jdk distributions: jdk 8 (temurin is recommended, but any vendor should work), jdk 11 (for docs module) and jdk 17 (graalvm-ce distribution, for cli-bot module). See CONTRIBUTING for more details.

Installation

Latest releases are always available on maven central.

Install with Gradle

dependencies {
    implementation 'io.github.serpro69:kotlin-faker:$version'
}

Install with Maven

<dependencies>
    <dependency>
        <groupId>io.github.serpro69</groupId>
        <artifactId>kotlin-faker</artifactId>
        <version>${version}</version>
    </dependency>
</dependencies>

Snapshots are also available using the following repository: https://oss.sonatype.org/content/repositories/snapshots/

With gradle

repositories {
    maven {
        url = 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

With maven

<repositories>
    <repository>
        <id>sonatype-snapshot</id>
        <name>Sonatype Snapshot</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
</repositories>

BOM

Kotlin-faker provides a Bill-of-Materials that simplifies dependency management.

See bom/README.md for more details.

Additional Fakers

Extra fakers covering a wide range of domains are available as separate dependencies. See faker submodules in this repo for more details about each faker.

Third-Party Extensions

Faker provides extensions for some third-party testing libraries. See extension submodules in this repo for more details about each extension.

Usage

Full usage documentation for kotlin-faker is available at serpro69.github.io/kotlin-faker/.

NB! The documentation website is currently not versioned and always reflects master rather than the last stable version.

Warning

kotlin-faker 2.0 is coming, which, apart from new features, also means breaking changes.

More details on currently added breaking changes and migration can be found in #220

Generating data

val faker = faker { }

faker.name.firstName() // => Ana
faker.address.city() // => New York

CLI

Command line application can be used for a quick lookup of faker functions. See faker-bot README for installation and usage details.

Test

To run unit tests: ./gradlew clean test

To run integration tests: ./gradlew clean integrationTest

Build and Deploy

To deploy to OSS Sonatype repo:

  • set the following properties in ~/.gradle/gradle.properties
    • signing.keyId=<key_id>
    • signing.password=<key_passphrase>
    • signing.secretKeyRingFile=/home/user/.gnupg/secring.gpg
    • sonatypeUsername=<oss_user_token>
    • sonatypePassword=<oss_password_token>
    • stagingProfileId=<oss_staging_profile_id>
  • running publishFakerCorePublicationToSonatypeRepository will publish the artifacts to either staging release repo or to snapshots repo, depending on the current version

Bumping versions

Versions need to be bumped manually through a tag with the next release version that has to follow the semver rules, and the tag has to be pushed to origin.

Creating the tag can be either done manually with git tag or by using gradlew tag task.

Pre-releases

To create a new pre-release version (new release candidate) the following can be used: ./gradlew clean tag -Prelease -PnewPreRelease -PbumpComponent={comp}, where comp can be one of the following values: major, minor, or patch.

To bump an existing pre-release to the next version (next release candidate for the same release version) the following can be used: ./gradlew clean tag -Prelease -PpreRelease.

Releases

To promote a pre-release to a release version the following can be used: ./gradlew clean tag -Prelease -PpromoteToRelease,

To create a new release version the following can be used: ./gradlew clean tag -Prelease -PbumpComponent={comp}, where comp can be one of the following values: major , minor, or patch.

Make targets

Alternatively to the above targets from Makefile can be used for the same purposes.

Contributing

The CONTRIBUTING guidelines should help in getting you started on how to contribute to this project.

Thanks

Many thanks to these awesome tools that help us in creating open-source software:
Intellij IDEA YourKit Java profiler

License

This code is free to use under the terms of the MIT license. See LICENSE.

kotlin-faker's People

Contributors

andrew-kirkham avatar ansman avatar arsennikum avatar cosmin-marginean avatar cvk77 avatar efenderbosch-atg avatar el-qq avatar grisu118 avatar humblehacker avatar jgiovaresco avatar joaosoumoreira avatar johanvergeer avatar mboisnard avatar rafal-spryszynski-allegro avatar scorsi avatar serpro69 avatar tzanke avatar urosjarc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kotlin-faker's Issues

Config.random should be reset when re-initializing Faker instance?

Currently the following code will fail:

Faker.Config.random = Random(42)
Faker.init()
val rand1 = Faker.Config.random

Faker.init()
val rand2 = Faker.Config.random

rand2 shouldNotBe rand1

Need to decide if to re-set the Config.random when re-initializing Faker or letting the user decide

Unable to resolve partially localized providers

For example Faker(FakerConfig.builder().create { locale = "de" }).pokemon.locations() will throw:

Exception in thread "main" java.util.NoSuchElementException: Secondary key 'locations' not found.
	at io.github.serpro69.kfaker.FakerService.getRawValue(FakerService.kt:179)
	at io.github.serpro69.kfaker.FakerService.resolve(FakerService.kt:245)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$returnOrResolveUnique$result$2.invoke(AbstractFakeDataProvider.kt:114)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$returnOrResolveUnique$result$2.invoke(AbstractFakeDataProvider.kt:14)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$resolve$1.invoke(AbstractFakeDataProvider.kt:54)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$resolve$1.invoke(AbstractFakeDataProvider.kt:14)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.returnOrResolveUnique(AbstractFakeDataProvider.kt:114)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.returnOrResolveUnique$default(AbstractFakeDataProvider.kt:109)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.resolve(AbstractFakeDataProvider.kt:80)
	at io.github.serpro69.kfaker.provider.Pokemon.locations(Pokemon.kt:16)
	at io.github.serpro69.kfaker.FakerKt.main(Faker.kt:8)
	at io.github.serpro69.kfaker.FakerKt.main(Faker.kt)

This happens because pokemon is partially localized in de.yml and locations key is missing.
In cases like this should fallback to default locale.

CLI application

(Thinking out loud) Would it be useful to have a CLI app for the functionality provided by the lib?

Unique values exclusions with wildcards

Example usage:

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

val excludePattern = Regex("^A")
faker.unique.exclude<Address>("country", excludePattern)

// in addition to generating unique values this will not generate any countries who's names begin with "A"
faker.address.country() 

Add support for deterministic random generation

Would be good to have the ability to control the seed of the pseudo-random generator to provide deterministic output of repeated function calls.

Pseudo code:

Faker.Config.random = Random(68)
Faker.name.firstName() // "Erika"

Faker.Config.random = Random(68)
Faker.name.firstName() // "Erika"

Use a different kotlintest spec for tests

Should consider using a different spec other than FreeSpec for unit tests.

Right now with the FreeSpec the tests are organized as:

"GIVEN xxx" - {
    // some preconditions

    "WHEN yyy" - {
        // some action

        "THEN zzz" { 
            // verification of results
        }
    }
}

However there is very little way to enforce this structure.
It would be better to use some specs where you need to use a pre-defined functions (i.e. FeatureSpec, BehaviorSpec or DescribeSpec) instead of strings.

Feature: add unique generation of values

Would be useful to generate only unique values.

Something along the lines of:

Faker().unique.name.firstName() // will return a unique firstName every time it's called

Revisit automated builds for patches

Currently each push to master will build a patch version automatically (unless the commit has a tag).
This might not be desirable in certain conditions, for example when merging pull-requests associated with the next major/minor version

Support deterministic constructor selection for randomClassInstance

Currently the constructor with the least number of arguments is used when creating a random instance of a class through RandomProvider#randomClassInstance.

This can be refactored, where the above is default behavior, but can be configured.

Not sure of a good syntax yet, I would prefer to stay away from doing something along the lines of:

val baz: Baz = faker.randomProvider.randomClassInstance {
    constructorArgSize = 3 // I would like to avoid doing this, but is there a better way?
    failIfNotFound = true // this should be added if above is added, and true should be default behavior?
}

Additionally, this can be completely randomized, so that a random constructor can be picked up and used by the generator.

Parameter 'city_name' not found in 'address' for 'lv' locale

Sample code:

FakerConfig fakerConfig = FakerConfigBuilder.create(FakerConfig.builder(), fromConsumer(builder -> {
    builder.setLocale("lv");
    builder.setRandom(new Random(42));
}));
Faker faker = new Faker(fakerConfig);
System.out.println(faker.getAddress().fullAddress());

This, most likely applies to other locales as well.

Thought unit tests covered this already

Update to Kotlin 1.4.31 and Jackson 2.12.1

I recently update the dependencies in a project of mine to Kotlin 1.4.31 and Jackson >= 2.12.

This in fact leads to a NoClassDefFound Exception at the Mapper class of Faker. To be precise at this line:

    init {
        mapper.registerModule(KotlinModule())
    }

So I would highly appreciate an updated version with updated dependencies! Thanks a lot!

Paragraph ? Sentence ?

Isn't there an easy way to create paragraph or sentences ?
Like

Faker().paragraph(5)//paragraphs with 5 sentence
Faker().sentence(3)//sentence with 3 words

Problems with Fasterxml 2.10.0 and Springboot 2.2.1

I cannot use Faker with my latest Springboot project (2.2.1.RELEASE)

java.lang.NoSuchMethodError: com.fasterxml.jackson.module.kotlin.KotlinModule.<init>(IZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V

	at io.github.serpro69.kfaker.Mapper.<clinit>(Mapper.kt:12)
	at io.github.serpro69.kfaker.FakerService.readCategory(FakerService.kt:121)
	at io.github.serpro69.kfaker.FakerService.load(FakerService.kt:65)
	at io.github.serpro69.kfaker.FakerService.<init>(FakerService.kt:30)
	at io.github.serpro69.kfaker.Faker.<init>(Faker.kt:13)
	at io.github.serpro69.kfaker.Faker.<init>(Faker.kt:12)

I fixed it by upgrading fasterxml to 2.10.1.

Update dictionary files

Some dictionary files have been updated and also looks like a few new ones added to the faker gem.

Consider support for JavaScript

Hi,

I would appreciate to use your project for webfrontends; therefore it would be great to enhance your project as multiplatform build.

I know that the need for reflection is a no go at the moment, but #23 could solve this I suppose.

[core] Feature: add generation of missing functions with annotation processing

Example:

Add some new parameter to a dictionary's category:

    name #this exists in name.yml for example
      strange_name: ["Some gibberish", "More gibberish"]

Annotation processor should generate the following code:

#NameExt.kt

val Name.strangeName = resolve { fakerService.resolve(it, "strange_name") }

After compiling should be possible to call:

Faker().name.strangeName() // => "Some gibberish"

This would particularly be useful if we want to add some extra category parameters without modifying the codebase

Android Support

Hi @serpro69

Can I use this library in an android project? Currently, I added this dependency and I get the following error:

faker.internet.email()

java.util.NoSuchElementException: Category with name 'NAME' not found

	at io.github.serpro69.kfaker.FakerService.fetchCategory(FakerService.kt:130)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$resolve$1.invoke(AbstractFakeDataProvider.kt:54)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider$resolve$1.invoke(AbstractFakeDataProvider.kt:14)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.returnOrResolveUnique(AbstractFakeDataProvider.kt:116)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.returnOrResolveUnique$default(AbstractFakeDataProvider.kt:109)
	at io.github.serpro69.kfaker.provider.AbstractFakeDataProvider.resolve(AbstractFakeDataProvider.kt:67)
	at io.github.serpro69.kfaker.provider.Name.name(Name.kt:19)
	at io.github.serpro69.kfaker.provider.Internet.email(Internet.kt:20)
	at io.github.serpro69.kfaker.provider.Internet.email$default(Internet.kt:18)

Publish release candidates to bintray

Adding rc releases that would go in-between actual releases would probably benefit some users, i.e. bringing new features faster.
This should be published to bintray, and users who are interested in latest features would have to add the extra repo for the dependency.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.