-
Notifications
You must be signed in to change notification settings - Fork 125
Unit Testing A Schema
It's a great time to be an Engineer. You can test things before you build and ship them. Yay! Let's talk about the cuttlefish_unit
module.
To generate an app.config
in an eunit test. Use the cuttlefish_unit:generated_template_config/3
. The three arguments are:
This is the list of schema files you're testing. If you've followed our conventional approach, you've stored myappname.schema
in your priv
directory.
to access it, set this argument to ["../priv/myappname.schema"]
. If it gets too big and you need multiple files, just append to this list.
This is in the same syntax and the Conf proplist that translations expect. See Translations and the Conf Proplist
If you're using rebar's mustache templates to substitute values into your schema, please include them in a proplist for this argument.
[
{handoff_port, "8099"},
{ring_state_dir , "./ring"},
{platform_bin_dir , "./bin"},
{platform_data_dir, "./data"},
{platform_etc_dir , "./etc"},
{platform_lib_dir , "./lib"},
{platform_log_dir , "./log"}
].
If you have a mapping like this:
{mapping, "a.b", "app.b", [
{default, {{handoff_port}} }
]}.
then {{handoff_port}}
would turn into "8099".
NOTE: for some unidentifiable reason, if you don't put a space between the second and third }
s, the whole thing explodes. This is a mustache issue, deal with it.
OTHER NOTE: these substitutions are made with rebar's mustache module. If you're running eunit outside of rebar, these won't work.
Config
is the return from cuttlefish_unit:generated_template_config/3
.
cuttlefish_unit:assert_config(Config,
"riak_core.default_bucket_props.n_val",
3),
Assert that Config (which is a valid app.config) that has the nested value 3, like so:
[{riak_core, [
{default_bucket_props, [
{n_val, 3}
]}
]}
]
cuttlefish_unit:assert_not_configured(Config,
"riak_core.ssl.certfile"),
Asserts that there is no value configured in the Config (app.config). This is different from the atom undefined
.
cuttlefish_unit:assert_error(Config),
Asserts that this config is an error state.
cuttlefish_unit:assert_error_in_phase(Config, Phase),
There are several phases to generating with cuttlefish: add_defaults
, transform_datatypes
, validation
, apply_translations
cuttlefish_unit:assert_error_message(Config, Message),
Asserts an error state, and that one of the errors is the message Message
cuttlefish_unit:assert_valid_config(Config)
It's basically the opposite of assert_error/1
.