Skip to content

Unit Testing A Schema

John Daily edited this page Aug 7, 2014 · 4 revisions

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.

Setup

To generate an app.config in an eunit test. Use the cuttlefish_unit:generated_template_config/3. The three arguments are:

List Of Schema Files

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.

Expected Processed Conf

This is in the same syntax and the Conf proplist that translations expect. See Translations and the Conf Proplist

Mustache Context

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.

Free Assertions (who doesn't want those?)

Config is the return from cuttlefish_unit:generated_template_config/3.

assert_config

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}
			]}
	]}
]

assert_not_configured

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.

assert_error

cuttlefish_unit:assert_error(Config),

Asserts that this config is an error state.

assert_error_in_phase

cuttlefish_unit:assert_error_in_phase(Config, Phase),

There are several phases to generating with cuttlefish: add_defaults, transform_datatypes, validation, apply_translations

assert_error_message

cuttlefish_unit:assert_error_message(Config, Message),

Asserts an error state, and that one of the errors is the message Message

assert_valid_config

cuttlefish_unit:assert_valid_config(Config)

It's basically the opposite of assert_error/1.