XUnit/FsUnit framework throwing "Object reference not set to an instance of an objct" - f#

I have written this simple test case for my code
module CustomerTests
open Xunit
open FsUnit
open MyProject.Customer
open MyProject.Customer.Domain
module ``When upgrading customer`` =
let customerVIP = {Id = 1; IsVip = true; Credit = 0.0M}
let customerSTD = {Id = 2; IsVip = false; Credit = 100.0M}
[<Fact>]
let ``should give VIP customer more credit`` () =
let expected = {customerVIP with Credit = customerVIP.Credit + 100.0M }
let actual = upgradeCustomer customerVIP
actual |> should equal expected
Very surprisingly this code fails with error
[xUnit.net 00:00:00.64] CustomerTests+When upgrading customer.should give VIP cstomer more credit [FAIL]
Failed CustomerTests+When upgrading customer.should give VIP cstomer more credit [3 ms]
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at CustomerTests.When upgrading customer.should give VIP cstomer more credit() in /Users/user/code/fsharp/CustomerProject/CustomerTests.fs:line 12
But line 12 is just a record being created so its not possible for that line to throw an Object reference not set to an instance of object. This is totally puzzling me.
In the dotnet fsi repl I can execute all my method and there is no object reference problem in my function which are being called from my tests here.

As this SO answer explains, XUnit loads the test in a way that skips initialization of those values. One easy fix is to use a class instead of a module:
type ``When upgrading customer``() =
let customerVIP = {Id = 1; isVip = true; Credit = 0.0M}
let customerSTD = {Id = 2; isVip = false; Credit = 100.0M}
[<Fact>]
let ``should give VIP cstomer more credit`` () =
let expected = {customerVIP with Credit = customerVIP.Credit + 100.0M }
let actual = upgradeCustomer customerVIP
actual |> should equal expected
That way, initialization of those values happens as it should.

Related

creating learner in mlr3: Error in sprintf(msg, ...) : too few arguments

I want to create a learner in mlr3, using the distRforest package.
my code:
library(mlr3extralearners)
create_learner( pkg = "." ,
classname = 'distRforest',
algorithm = 'regression tree',
type = 'regr',
key = 'distRforest',
package = 'distRforest',
caller = 'rpart',
feature_types = c("logical", "integer", "numeric","factor", "ordered"),
predict_types = c('response'),
properties = c("importance", "missings", "multiclass",
"selected_features", "twoclass", "weights"),
references = FALSE,
gh_name = 'CL'
)
gives the following error : Error in sprintf(msg, ...) : too few arguments
in fact, replicating the code in the tutorial https://mlr3book.mlr-org.com/extending-learners.html throws the same error.
Any ideas? Thanks a lot - c
thanks for your interest in extending the mlr3 universe!
Couple of things, firstly the example in the book works fine for me, and secondly your example cannot work because you are including classif properties for a regr learner. As I am unable to reproduce your error it's hard for me to debug what's going wrong, it would be helpful if you could run the following:
reprex::reprex({
create_learner(
pkg = ".",
classname = "Rpart",
algorithm = "decision tree",
type = "classif",
key = "rpartddf",
package = "rpart",
caller = "rpart",
feature_types = c("logical", "integer", "numeric", "factor", "ordered"),
predict_types = c("response", "prob"),
properties = c("importance", "missings", "multiclass", "selected_features", "twoclass", "weights"),
references = TRUE,
gh_name = "CL"
)
}, si = TRUE)
If you're still getting an error and the output is too long to print here then head over to the GitHub and open an issue there.

Calling expand entity from SAP Gateway Client

I try to call a GET_EXPANDED_ENTITYSET method of SRA020_PO_TRACKING Project, po tracking project. The following is the method:
METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.
DATA: lv_ponumber TYPE bapiekko-po_number,
lv_leading_msg TYPE boolean.
DATA: lo_api TYPE REF TO cl_sra020_po_tracking_api.
DATA: ls_po_details TYPE cl_sra020_po_tracking_api=>ts_podetails,
lt_return TYPE bapirettab,
lo_msgcontainer TYPE REF TO /iwbep/if_message_container.
DATA: lt_poitem_details TYPE cl_sra020_po_tracking_api=>tt_poitemdetail.
FIELD-SYMBOLS: <fs_return> TYPE bapiret2,
<fs_key_tab> TYPE /iwbep/s_mgw_name_value_pair.
lo_api = cl_sra020_po_tracking_api=>get_instance( ).
*========================================================================================
* Read Entities key values within oData service URL
*========================================================================================
IF it_key_tab IS NOT INITIAL.
LOOP AT it_key_tab ASSIGNING <fs_key_tab>.
CASE <fs_key_tab>-name.
WHEN if_sra020_po_tracking_constant=>cc_po_number.
lv_ponumber = <fs_key_tab>-value.
ENDCASE.
ENDLOOP.
ENDIF.
TRY.
CASE iv_entity_set_name.
WHEN if_sra020_po_tracking_constant=>cc_podetaileddatas.
CALL METHOD lo_api->get_po_details
EXPORTING
iv_item_additional_data = 'X' "#EC NOTEXT
iv_po_number = lv_ponumber
IMPORTING
es_po_details = ls_po_details
et_return = lt_return.
IF NOT lt_return IS INITIAL.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception.
ENDIF.
lt_poitem_details = ls_po_details-poitemdetaildatas.
no_cache( ).
* Return the expanded clauses
APPEND cl_sra020_po_tracking_mpc=>gc_poitemdocflow TO et_expanded_tech_clauses.
APPEND cl_sra020_po_tracking_mpc=>gc_accounting TO et_expanded_tech_clauses.
APPEND cl_sra020_po_tracking_mpc=>gc_pricingconditions TO et_expanded_tech_clauses.
APPEND cl_sra020_po_tracking_mpc=>gc_confirmation TO et_expanded_tech_clauses.
CALL METHOD copy_data_to_ref
EXPORTING
is_data = lt_poitem_details
CHANGING
cr_data = er_entityset.
ENDCASE.
CATCH /iwbep/cx_mgw_busi_exception.
lo_msgcontainer = me->mo_context->get_message_container( ).
LOOP AT lt_return ASSIGNING <fs_return> WHERE type EQ 'E' OR type EQ 'A'. "#EC NOTEXT
IF <fs_return>-id EQ 'SRA020'.
lv_leading_msg = abap_true.
ELSE.
lv_leading_msg = abap_false.
ENDIF.
lo_msgcontainer->add_message(
iv_msg_type = <fs_return>-type
iv_msg_id = <fs_return>-id
iv_msg_number = <fs_return>-number
iv_msg_text = <fs_return>-message
iv_msg_v1 = <fs_return>-message_v1
iv_msg_v2 = <fs_return>-message_v2
iv_msg_v3 = <fs_return>-message_v3
iv_msg_v4 = <fs_return>-message_v4
iv_is_leading_message = lv_leading_msg
).
ENDLOOP.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message_container = lo_msgcontainer.
ENDTRY.
ENDMETHOD.
Where
if_sra020_po_tracking_constant=>cc_po_number = PONumber
if_sra020_po_tracking_constant=>cc_podetaileddatas = PODetailedDatas
and PODetailedData entity has navigation properties of POItemDetailDatas,POItems,and POList
I try to call the method from Gateway Service Client by executing
/sap/opu/odata/SAP/SRA020_PO_TRACKING_SRV/PODetailedDatas?$expand=POItemDetailDatas,POItems,POList
However I got status code 400 Bad Request instead. What did I miss?
regards
Edit:
The message in the Bad Request says: Error while reading Purchase Order
edit 2:
association screen shot:
navigation screen shot:
Turns out I have to provide PONumber, so that:
PODetailedDatas(PONumber='4500000039')?$expand=POItems
works fine

How to create a DynamoDB entry from F#?

There is the following code on the AWS documentation page (I simplified):
Table table = Table.LoadTable(client, "ProductCatalog");
var book = new Document();
book["Id"] = 101;
book["Title"] = "Book 101 Title";
book["ISBN"] = "11-11-11-11";
table.PutItem(book);
When I try to the same from Fsharp I get an error:
let seqNumberDocument = Document()
seqNumberDocument.["key"] <- 101
Type constraint mismatch. The type
'int' is not compatible with type
'DynamoDBEntry'
Is there a way to get this working in Fsharp?
You'll need to call the implicit conversion to DynamoDBEntry like this:
let seqNumberDocument = Document()
seqNumberDocument.["key"] <- DynamoDBEntry.op_Implicit 101
F# doesn't itself support implicit conversion by design.
If that looks a bit ugly, you can use the alternative:
let v2 = DynamoDBEntryConversion.V2
let seqNumberDocument = Document()
seqNumberDocument.["key"] <- v2.ConvertToEntry 101

Rewrite variable in Erlang

I am playing with records and list. Please, I want to know how to use one variable twice. When I assign any values into variable _list and after that I try rewrite this variable then raising error:
** exception error: no match of right hand side value
-module(hello).
-author("anx00040").
-record(car, {evc, type, color}).
-record(person, {name, phone, addresa, rc}).
-record(driver, {rc, evc}).
-record(list, {cars = [], persons = [], drivers = []} ).
%% API
-export([helloIF/1, helloCase/1, helloResult/1, helloList/0, map/2, filter/2, helloListCaA/0, createCar/3, createPerson/4, createDriver/2, helloRecords/0, empty_list/0, any_data/0, del_Person/1, get_persons/1, do_it_hard/0, add_person/2]).
createCar(P_evc, P_type, P_color) -> _car = #car{evc = P_evc, type = P_type, color = P_color}, _car
.
createPerson(P_name, P_phone, P_addres, P_rc) -> _person= #person{name = P_name, phone = P_phone, addresa = P_addres, rc = P_rc}, _person
.
createDriver(P_evc, P_rc) -> _driver = #driver{rc = P_rc, evc = P_evc}, _driver
.
empty_list() ->
#list{}.
any_data() ->
_car1 = hello:createCar("BL 4", "Skoda octavia", "White"),
_person1 = hello:createPerson("Eduard B.","+421 917 111 711","Kr, 81107 Bratislava1", "8811235"),
_driver1 = hello:createDriver(_car1#car.evc, _person1#person.rc),
_car2 = hello:createCar("BL 111 HK", "BMW M1", "Red"),
_person2 = hello:createPerson("Lenka M","+421 917 111 111","Krizn0, 81107 Bratislava1", "8811167695"),
_driver2 = hello:createDriver(_car2#car.evc, _person2#person.rc),
_car3 = hello:createCar("BL 123 AB", "Audi A1 S", "Black"),
_person3 = hello:createPerson("Stela Ba.","+421 918 111 711","Azna 20, 81107 Bratislava1", "8811167695"),
_driver3 = hello:createDriver(_car3#car.evc, _person3#person.rc),
_list = #list{
cars = [_car1,_car2,_car3],
persons = [_person1, _person2, _person3],
drivers = [_driver1, _driver2, _driver3]},
_list.
add_person(List, Person) ->
List#list{persons = lists:append([Person], List#list.persons) }.
get_persons(#list{persons = P}) -> P.
do_it_hard()->
empty_list(),
_list = add_person(any_data(), #person{name = "Test",phone = "+421Test", addresa = "Testova 20 81101 Testovo", rc =88113545}),
io:fwrite("\n"),
get_persons(add_person(_list, #person{name = "Test2",phone = "+421Test2", addresa = "Testova 20 81101 Testovo2", rc =991135455}))
.
But it raising error when i use variable _list twice:
do_it_hard()->
empty_list(),
_list = add_person(any_data(), #person{name = "Test",phone = "+421Test", addresa = "Testova 20 81101 Testovo", rc =88113545}),
_list =add_person(_list, #person{name = "Test2",phone = "+421Test2", addresa = "Testova 20 81101 Testovo2", rc =991135455}),
get_persons(_list)
.
In the REPL, it can be convenient to experiment with things while re-using variable names. There, you can do f(A). to have Erlang "forget" the current assignment of A.
1> Result = connect("goooogle.com").
{error, "server not found"}
2> % oops! I misspelled the server name
2> f(Result).
ok
3> Result = connect("google.com").
{ok, <<"contents of the page">>}
Note that this is only a REPL convenience feature. You can't do this in actual code.
In actual code, variables can only be assigned once. In a procedural language (C, Java, Python, etc), the typical use-case for reassignment is loops:
for (int i = 0; i < max; i++) {
conn = connect(servers[i]);
reply = send_data(conn);
print(reply);
}
In the above, the variables i, conn, and reply are reassigned in each iteration of the loop.
Functional languages use recursion to perform their loops:
send_all(Max, Servers) ->
send_loop(1, Max, Servers).
send_loop(Current, Max, _Servers) when Current =:= Max->
ok;
send_loop(Current, Max, Servers) ->
Conn = connect(lists:nth(Current, Servers)),
Reply = send_data(Conn),
print(Reply).
This isn't very idiomatic Erlang; I'm trying to make it mirror the procedural code above.
As you can see, I'm getting the same effect, but my assignments within a function are fixed.
As a side note, you are using a lot of variable names beginning with underscore. In Erlang this is a way of hinting that you will not be using the value of these variables. (Like in the above example, when I've reached the end of my list, I don't care about the list of servers.) Using a leading underscore as in your code turns off some useful compiler warnings and will confuse any other developers who look at your code.
In some situations it is convenient to use use SeqBind:
SeqBind is a parse transformation that auto-numbers all occurrences of these bindings following the suffix # (creating L#0, L#1, Req#0, Req#1) and so on.
Simple example:
...
-compile({parse_transform,seqbind}).
...
List# = lists:seq(0, 100),
List# = lists:filter(fun (X) -> X rem 2 == 0 end, List#)
...
I used google...
Erlang is a single-assignment language. That is, once a variable has been given a value, it cannot be given a different value. In this sense it is like algebra rather than like most conventional programming languages.
http://www.cis.upenn.edu/~matuszek/General/ConciseGuides/concise-erlang.html

Error in erlang when using records

I have created a simple program that should simulate an address book:
%% API
-export([]).
-export([createAddressBook/0]).
-export([checkIfExist/3]).
-export([addContact/3]).
-record(entry,{nameAndSurname,n_phone,email}).
createAddressBook() ->
Test = #entry{nameAndSurname = {"Marcin", "Majewski"}, n_phone = [997], email=["call#call.tv"]},
[Test].
checkIfExist(_,_,[]) -> false;
checkIfExist(Name,Surname,[H|L]) ->
if
H#entry.nameAndSurname == {Name,Surname} -> true;
true -> checkIfExist(Name,Surname,L)
end.
addContact(Name,Surname,L)->
case checkIfExist(Name,Surname,L) of
true -> io:format("Entry already exist, nothing was created!");
_ -> newEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email = []},lists:append(L,newEntry)
end.
But when I invoke:
X=module_name:createAddressBook().
B=module_name:addContact("Name","Surname",X).
I am getting an error:
** exception error: no match of right hand side value
{entry,{"Name","Surname"},[],[]}
in function addContact
I do not understand what causes this problem.
You are tryin to assign record to atom:
newEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email = []},lists:append(L,newEntry)
Since newEntry starts from lowercase letter, erlang treats it as atom, but not variable. Just change it:
NewEntry = #entry{nameAndSurname = {Name,Surname}, n_phone = [],email]},
lists:append(L,NewEntry)

Resources