def test_NumberField_returns_value_within_range_of_two_embedded_fields( monkeypatch): fset = Fieldset(fields=[ field.NumberField( name="num", distribution="scipy.stats.norm", sd=3, mean=0, min=field.NumberField(name="floor", distribution="scipy.stats.norm", sd=1, mean=-5, min=-3, dp=0), max=field.NumberField(name="ceil", distribution="scipy.stats.norm", sd=1, mean=5, max=3, dp=0), dp=1) ]) df = fset.generate_data(5) for idx, item in df.iterrows(): assert item['num'] > -3 and item['num'] < 3
def test_NumberField_returns_value_within_range_of_two_value(monkeypatch): fset = Fieldset(fields=[ field.NumberField(name="num", distribution="scipy.stats.norm", sd=3, mean=0, min=-2, max=2, dp=1) ]) HeadFake.set_seed(1234) df = fset.generate_data(5) for idx, item in df.iterrows(): assert item['num'] > -2 and item['num'] < 2
def test_DateField_returns_value_within_range_of_two_values_with_specified_sd_in_years( monkeypatch): min = datetime.date(1954, 1, 1) max = datetime.date(2020, 6, 1) fset = Fieldset(fields=[ field.DateField(name="date", distribution="scipy.stats.norm", sd=5, mean=datetime.date(1970, 3, 1), min=min, max=max, use_years=True) ]) df = fset.generate_data(5) for idx, item in df.iterrows(): assert item['date'] > min and item['date'] < max
def test_IfElseField_handles_condition_as_dictionary(): fset = Fieldset(fields={"marital_status": "M"}) HeadFake.set_seed(5) gender_cond = {"field": "gender", "operator": operator.eq, "value": "M"} ms_test_cond = { "field": "marital_status", "operator": operator.eq, "value": "M" } female_ov = field.OptionValueField(probabilities={ "MISS": 0.7, "MS": 0.1, "DR": 0.1, "PROF": 0.1 }) ms_if_else = field.IfElseField(condition=ms_test_cond, true_value="MRS", false_value=female_ov) gender_if_else = field.IfElseField(condition=gender_cond, true_value="MR", false_value=ms_if_else) gender_if_else.init_from_fieldset(fset) assert gender_if_else.next_value({ "gender": "M", "marital_status": "M" }) == "MR" assert gender_if_else.next_value({ "gender": "M", "marital_status": "S" }) == "MR" assert gender_if_else.next_value({ "gender": "F", "marital_status": "S" }) == "PROF" assert gender_if_else.next_value({ "gender": "F", "marital_status": "S" }) == "MISS" assert gender_if_else.next_value({ "gender": "F", "marital_status": "S" }) == "MISS"
def test_IfElseField_handles_simple_setup(): fset = Fieldset(fields={"gender": ""}) gender_cond = field.Condition(field="gender", operator=operator.eq, value="M") if_else = field.IfElseField(condition=gender_cond, true_value="MR", false_value="MRS") if_else.init_from_fieldset(fset) assert if_else.next_value({"gender": "M"}) == "MR" assert if_else.next_value({"gender": "F"}) == "MRS"
def test_LastNameField_returns_male_last_name_if_gender_is_male(monkeypatch): f = mock.Mock() f.return_value.last_name_male.return_value = MALE_SURNAME monkeypatch.setattr("faker.Faker", f) ln_field = field.LastNameField(gender_field="gender") gender = field.GenderField(male_value=MALE_VALUE, female_value=FEMALE_VALUE) fset = Fieldset(fields={"gender": gender}) ln_field.init_from_fieldset(fset) obs = ln_field.next_value({"gender": MALE_VALUE}) assert obs == MALE_SURNAME
def test_FirstNameField_returns_female_name_if_gender_field_is_female( monkeypatch): f = mock.Mock() f.return_value.first_name_female.return_value = FEMALE_NAME monkeypatch.setattr("faker.Faker", f) fn_field = field.FirstNameField(gender_field="gender") gender = field.GenderField(male_value=MALE_VALUE, female_value=FEMALE_VALUE) fset = Fieldset(fields={"gender": gender}) fn_field.init_from_fieldset(fset) obs = fn_field.next_value({"gender": FEMALE_VALUE}) assert obs == FEMALE_NAME
def test_IfElseField_handles_nested_setup(): fset = Fieldset(fields={"marital_status": "M"}) gender_cond = field.Condition(field="gender", operator=operator.eq, value="M") ms_test_cond = field.Condition(field="marital_status", operator=operator.eq, value="M") ms_if_else = field.IfElseField(condition=ms_test_cond, true_value="MRS", false_value="MISS") gender_if_else = field.IfElseField(condition=gender_cond, true_value="MR", false_value=ms_if_else) gender_if_else.init_from_fieldset(fset) assert gender_if_else.next_value({ "gender": "M", "marital_status": "M" }) == "MR" assert gender_if_else.next_value({ "gender": "M", "marital_status": "S" }) == "MR" assert gender_if_else.next_value({ "gender": "F", "marital_status": "S" }) == "MISS" assert gender_if_else.next_value({ "gender": "F", "marital_status": "M" }) == "MRS"
def test_MiddleNameField_returns_different_name_if_same_as_first_name( monkeypatch): f = mock.Mock() f.return_value.first_name_female.side_effect = [FEMALE_NAME, FEMALE_NAME2] monkeypatch.setattr("faker.Faker", f) fn_field = field.FirstNameField(gender_field="gender") mn_field = field.MiddleNameField(gender_field="gender", first_name_field="first_name") gender = field.GenderField(male_value=MALE_VALUE, female_value=FEMALE_VALUE) fset = Fieldset(fields={"first_name": fn_field, "gender": gender}) mn_field.init_from_fieldset(fset) obs = mn_field.next_value({ "gender": FEMALE_VALUE, "first_name": FEMALE_NAME }) assert obs == FEMALE_NAME2