Skip to content

Instantly share code, notes, and snippets.

@ToJans
Last active December 22, 2015 19:59
Show Gist options
  • Save ToJans/6523086 to your computer and use it in GitHub Desktop.
Save ToJans/6523086 to your computer and use it in GitHub Desktop.
Porting the DDD app or not?
-module(cargo_tests).
-include_lib("eunit/include/eunit.hrl").
-compile([export_all]).
cargo_construction_test() ->
Cargo = start_a_cargo(),
?assertEqual(not_routed, cargo_delivery:routing_status(Cargo)),
?assertEqual(not_received, cargo_delivery:transport_status(Cargo)),
?assertEqual(cargo:location(unknown), cargo_delivery:last_known_location(Cargo)),
?assertEqual(none, cargo_delivery:current_voyage(Cargo)),
stop_a_cargo(Cargo).
routing_status_test() ->
Cargo = start_a_cargo(),
cargo:specify_new_route(a_route_that_only_accepts_the_good_itenerary()),
?assertEqual(not_routed, cargo_delivery:routing_status(Cargo)),
cargo:assign_to_route(the_bad_itenerary()),
?assertEqual(misrouted, cargo_delivery:routing_status(Cargo)),
cargo:assign_to_route(the_good_itenerary()),
?assertEqual(routed, cargo_delivery:routing_status(Cargo)),
stop_a_cargo(Cargo).
last_known_location_when_no_handling_events_test() ->
Cargo = start_a_cargo(),
?assertEqual(cargo:location(unknown),cargo_delivery:last_known_location(Cargo)).
last_known_location_received_test() ->
Cargo = start_a_cargo(fun (Cargo) ->
[handling_on_day(Cargo, 1, received, stockholm)] end),
?assertEqual(cargo:location(stockholm),cargo_delivery:last_known_location(Cargo)).
last_known_location_claimed_test() ->
Cargo = start_a_cargo(fun (Cargo) ->
[handling_on_day(Cargo, 1, loaded, stockholm ),
handling_on_day(Cargo, 2, unloaded, hamburg ),
handling_on_day(Cargo, 3, loaded, hamburg ),
handling_on_day(Cargo, 4, unloaded, hongkong ),
handling_on_day(Cargo, 5, loaded, hamburg ),
handling_on_day(Cargo, 7, unloaded, melbourne ),
handling_on_day(Cargo, 9, claimed, melbourne )] end),
?assertEqual(cargo:location(melbourne), cargo_delivery:last_known_location(Cargo)).
last_known_location_unloaded_test() ->
Cargo = start_a_cargo(fun (Cargo) ->
[handling_on_day(Cargo, 1, loaded, stockholm ),
handling_on_day(Cargo, 2, unloaded, hamburg ),
handling_on_day(Cargo, 3, loaded, hamburg ),
handling_on_day(Cargo, 4, unloaded, hongkong )] end),
?assertEqual(cargo:location(hongkong), cargo_delivery:last_known_location(Cargo)).
last_known_location_loaded_test() ->
Cargo = start_a_cargo(fun (Cargo) ->
[handling_on_day(Cargo, 1, loaded, stockholm ),
handling_on_day(Cargo, 2, unloaded, hamburg ),
handling_on_day(Cargo, 3, loaded, hamburg )] end),
?assertEqual(cargo:location(hamburg), cargo_delivery:last_known_location(Cargo)).
unloaded_at_final_destination_test() ->
Cargo = start_a_cargo_with_itinerary(hangzou,tokyo,new_york),
?assertNot(cargo_delivery:is_unloaded_at_destination(Cargo)),
% Adding an event unrelated to the unloading at destination
cargo:derive_delivery_progress(Cargo,[handling_on_day(Cargo, 10, received, hangzou)]),
?assertNot(cargo_delivery:is_unloaded_at_destination(Cargo)),
% Adding an unload event but not at the final destination
cargo:derive_delivery_progress(Cargo,[handling_on_day(Cargo, 20, unloaded, tokyo)]),
?assertNot(cargo_delivery:is_unloaded_at_destination(Cargo)),
% Finally the cargo is unloaded at the final destination
cargo:derive_delivery_progress(Cargo,[handling_on_day(Cargo, 30, unloaded, new_york)]),
?assert(cargo_delivery:is_unloaded_at_destination(Cargo)).
is_misdirected_without_itenerary_test() ->
Cargo = start_a_cargo(),
?assertNot(cargo_delivery:is_misdirected(Cargo)).
is_misdirected_with_an_itenary_test() ->
Cargo = start_a_cargo_with_itinerary(shangai, rotterdam, gothenburg),
?assertNot(cargo_delivery:is_misdirected(Cargo)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helper functions %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start_a_cargo() ->
cargo:start(a_tracking_id(), a_route_specification()).
start_a_cargo(FunGeneratingHandlingEvents) ->
Cargo = start_a_cargo(),
HandlingEvents = FunGeneratingHandlingEvents(Cargo),
Cargo:derive_delivery_progress(Cargo,HandlingEvents).
start_a_cargo_with_itinerary(Origin,Midpoint,Destination) ->
[LocOrig,LocMid,LocDest] = [cargo:location(Loc)||Loc <- [Origin,Midpoint,Destination]],
RouteSpecification = cargo:route_specification(LocOrig,LocDest,today()),
Cargo = cargo:start(a_tracking_id(), RouteSpecification),
Itenerary = cargo:itenerary([
cargo:itenerary_leg(a_voyage(),LocOrig,LocMid,today()),
cargo:itenerary_leg(a_voyage(),LocMid,LocDest,today())]),
cargo:assign_to_route(Cargo, Itenerary).
stop_a_cargo(Cargo) ->
cargo:stop(Cargo).
a_route_specification() ->
Origin = cargo:location(stockholm),
Destination = cargo:location(melbourne),
ArrivalDate = {2009, 03, 13},
cargo:route_specification(Origin, Destination, ArrivalDate).
a_tracking_id() ->
cargo:tracking_id(xyz).
the_good_itenerary() ->
cargo:itenerary().
the_bad_itenerary() ->
cargo:itenerary().
a_route_that_only_accepts_the_good_itenerary() ->
todo.
today() ->
{Date,_Time} = calendar:local_time(),
Date.
a_voyage() ->
cargo:voyage(cargo:voyage_number("0123"),cargo:location(stockholm),[
cargo:movement(hamburg,today(),today()),
cargo:movement(hongkong,today(),today()),
cargo:movement(melbourne,today(),today())]).
handling_on_day(Cargo,Days,Type,Location) ->
cargo:handling_event(Cargo,{2007,12,Days}, today(), Type, cargo:location(Location), a_voyage()).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment