-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native types instead of strings (#617)
In Snowfakery 2, all formulas are evaluated to either a string or number. In Snowfakery 3, other types are possible. Dates are the most prominent reason to prefer this.
- Loading branch information
Paul Prescod
authored
Feb 23, 2022
1 parent
21f9912
commit 7d431d6
Showing
10 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# snowfakery examples/native_types_differences.yml --plugin-option snowfakery_version 3 | ||
- snowfakery_version: 3 | ||
- object: Example | ||
fields: | ||
y: ${{datetime(year=2000, month=1, day=1)}} | ||
z: ${{y.date()}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from snowfakery import SnowfakeryPlugin | ||
from snowfakery.plugins import PluginOption | ||
|
||
plugin_options_version = ( | ||
"snowfakery.standard_plugins.SnowfakeryVersion.snowfakery_version" | ||
) | ||
|
||
|
||
class SnowfakeryVersion(SnowfakeryPlugin): | ||
allowed_options = [ | ||
PluginOption(plugin_options_version, int), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- snowfakery_version: 4 | ||
- object: Example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest | ||
from io import StringIO | ||
from snowfakery import generate_data | ||
from snowfakery import data_gen_exceptions as exc | ||
|
||
|
||
class TestDates: | ||
def test_old_dates_as_strings(self, generated_rows): | ||
yaml = """ | ||
- object: OBJ | ||
fields: | ||
basedate: ${{datetime(year=2000, month=1, day=1)}} | ||
dateplus: ${{basedate + "XYZZY"}} | ||
""" | ||
generate_data(StringIO(yaml)) | ||
date = generated_rows.table_values("OBJ", 1, "dateplus") | ||
assert date.startswith("2000") | ||
assert date.endswith("XYZZY") | ||
|
||
def test_date_math__native_types(self, generated_rows): | ||
yaml = """ | ||
- object: OBJ | ||
fields: | ||
basedate: ${{datetime(year=2000, month=1, day=1)}} | ||
dateplus: ${{basedate + relativedelta(years=22)}} | ||
""" | ||
generate_data(StringIO(yaml), plugin_options={"snowfakery_version": 3}) | ||
date = generated_rows.table_values("OBJ", 1, "dateplus") | ||
assert date == "2022-01-01T00:00:00+00:00" | ||
|
||
def test_date_math__native_types__error(self, generated_rows): | ||
yaml = """ | ||
- object: OBJ | ||
fields: | ||
basedate: ${{datetime(year=2000, month=1, day=1)}} | ||
dateplus: ${{basedate + "XYZZY"}} | ||
""" | ||
with pytest.raises(exc.DataGenValueError) as e: | ||
generate_data(StringIO(yaml), plugin_options={"snowfakery_version": 3}) | ||
assert "dateplus" in str(e.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.