evolution_mocker/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// MIT License
//
// Copyright (c) 2024 Firelink Data
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// File created: 2024-05-25
// Last updated: 2024-10-11
//

use evolution_common::datatype::DataType;
use evolution_schema::column::FixedColumn;
use faker_rand::en_us::names::FirstName;
use rand::rngs::ThreadRng;
use rand::Rng;

pub static MOCKED_F16_MAX: f32 = 256.0;
pub static MOCKED_F32_MAX: f32 = 1_000_000.0;
pub static MOCKED_F64_MAX: f64 = 1_000_000_000.0;
pub static MOCKED_I16_MAX: i16 = 10_000;
pub static MOCKED_I32_MAX: i32 = 1_000_000;
pub static MOCKED_I64_MAX: i64 = 1_000_000_000;

/// Create a string with mocked data based on the [`FixedColumn`] datatype.
pub fn mock_column(column: &FixedColumn, rng: &mut ThreadRng) -> String {
    match column.dtype() {
        DataType::Boolean => mock_bool(rng),
        DataType::Float16 => mock_f16(rng),
        DataType::Float32 => mock_f32(rng),
        DataType::Float64 => mock_f64(rng),
        DataType::Int16 => mock_i16(rng),
        DataType::Int32 => mock_i32(rng),
        DataType::Int64 => mock_i64(rng),
        DataType::Utf8 => mock_utf8(rng),
        DataType::LargeUtf8 => mock_utf8(rng),
    }
}

/// Mock a boolean with 50/50 chance of being True/False.
fn mock_bool(rng: &mut ThreadRng) -> String {
    rng.gen_bool(0.5).to_string()
}

/// Mock a f16 number in the range [-[`MOCKED_F16_MAX`], [`MOCKED_F16_MAX`]].
fn mock_f16(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_F16_MAX..=MOCKED_F16_MAX).to_string()
}

/// Mock a f32 number in the range [-[`MOCKED_F32_MAX`], [`MOCKED_F32_MAX`]].
fn mock_f32(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_F32_MAX..=MOCKED_F32_MAX).to_string()
}

/// Mock a f64 number in the range [-[`MOCKED_F64_MAX`], [`MOCKED_F64_MAX`]].
fn mock_f64(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_F64_MAX..=MOCKED_F64_MAX).to_string()
}

/// Mock a i16 number in the range [-[`MOCKED_I16_MAX`], [`MOCKED_I16_MAX`]].
fn mock_i16(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_I16_MAX..=MOCKED_I16_MAX).to_string()
}

/// Mock a i32 number in the range [-[`MOCKED_I32_MAX`], [`MOCKED_I32_MAX`]].
fn mock_i32(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_I32_MAX..=MOCKED_I32_MAX).to_string()
}

/// Mock a i64 number in the range [-[`MOCKED_I64_MAX`], [`MOCKED_I64_MAX`]].
fn mock_i64(rng: &mut ThreadRng) -> String {
    rng.gen_range(-MOCKED_I64_MAX..=MOCKED_I64_MAX).to_string()
}

/// Sample a random name uniformly from the [`FirstName`] distribution.
fn mock_utf8(rng: &mut ThreadRng) -> String {
    rng.gen::<FirstName>().to_string()
}

pub mod mocker;