From 322bdcbb4c9969143efb588df5344d7ac48429f0 Mon Sep 17 00:00:00 2001 From: herrmannj Date: Mon, 17 Apr 2023 23:39:39 +0000 Subject: [PATCH] 98_JsonMod.pm: unit test git-svn-id: https://svn.fhem.de/fhem/trunk@27458 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/t/FHEM/98_JsonMod/10_General.cfg | 9 ++ fhem/t/FHEM/98_JsonMod/10_General.t | 114 ++++++++++++++++++++++++++ fhem/t/FHEM/98_JsonMod/goessner.json | 31 +++++++ 3 files changed, 154 insertions(+) create mode 100644 fhem/t/FHEM/98_JsonMod/10_General.cfg create mode 100644 fhem/t/FHEM/98_JsonMod/10_General.t create mode 100644 fhem/t/FHEM/98_JsonMod/goessner.json diff --git a/fhem/t/FHEM/98_JsonMod/10_General.cfg b/fhem/t/FHEM/98_JsonMod/10_General.cfg new file mode 100644 index 000000000..a66d6e322 --- /dev/null +++ b/fhem/t/FHEM/98_JsonMod/10_General.cfg @@ -0,0 +1,9 @@ +defmod 10_JsonMod JsonMod file://t/FHEM/98_JsonMod/goessner.json +attr 10_JsonMod readingList complete() +attr 10_JsonMod update-on-start 0 +attr 10_JsonMod verbose 1 + +defmod 11_JsonMod JsonMod https://jsonplaceholder.typicode.com/users +attr 11_JsonMod readingList multi(jsonPath('$.[*]'), concat(count, '-', property('id')), concat(property('email'), ' ', property('address.city'))) +attr 11_JsonMod update-on-start 0 +attr 11_JsonMod verbose 1 \ No newline at end of file diff --git a/fhem/t/FHEM/98_JsonMod/10_General.t b/fhem/t/FHEM/98_JsonMod/10_General.t new file mode 100644 index 000000000..2259ec499 --- /dev/null +++ b/fhem/t/FHEM/98_JsonMod/10_General.t @@ -0,0 +1,114 @@ +# perl fhem.pl -t t/FHEM/98_JsonMod/10_General.t +# test general funcionality +use strict; +use warnings; +use Test::More; + +my %_10_test_data = ( + 'store.bicycle.color' => 'red', + 'store.bicycle.price' => '19.95', + 'store.book.0.author' => 'Nigel Rees', + 'store.book.0.category' => 'reference', + 'store.book.0.price' => '8.95', + 'store.book.0.title' => 'Sayings of the Century', + 'store.book.1.author' => 'Evelyn Waugh', + 'store.book.1.category' => 'fiction', + 'store.book.1.price' => '12.99', + 'store.book.1.title' => 'Sword of Honour', + 'store.book.2.author' => 'Herman Melville', + 'store.book.2.category' => 'fiction', + 'store.book.2.isbn' => '0-553-21311-3', + 'store.book.2.price' => '8.99', + 'store.book.2.title' => 'Moby Dick', + 'store.book.3.author' => 'J. R. R. Tolkien', + 'store.book.3.category' => 'fiction', + 'store.book.3.isbn' => '0-395-19395-8', + 'store.book.3.price' => '22.99', + 'store.book.3.title' => 'The Lord of the Rings', +); + + +fhem('set 10_JsonMod reread'); + +my @readings = sort split ',', ReadingsVal('10_JsonMod', '.computedReadings', undef); + +# create test data set once: +if (0) { + print "\%_10_test_data = (\n"; + foreach (@readings) { + print "\t'$_' => '" . $defs{'10_JsonMod'}->{'READINGS'}->{$_}->{'VAL'} . "',\n"; + } + print ");\n"; + printf "%d keys created \n", scalar @readings; +}; + +plan tests => 33; + +print "\tvalidate basic functionallity:\n"; +my @expected = sort keys %_10_test_data; +is((@readings), (@expected), 'all expected readings created'); + +# validate the readings value +foreach (@readings) { + my $val; + is($val = ReadingsVal('10_JsonMod', $_, undef), $_10_test_data{$_}, "reading '$_' is '$val'"); +}; + +# test http(s) functionallity +print "\tvalidate basic http(s) functionallity:\n"; + +my %_11_test_data = ( + '0-1' => 'Sincere@april.biz Gwenborough', + '1-2' => 'Shanna@melissa.tv Wisokyburgh', + '2-3' => 'Nathan@yesenia.net McKenziehaven', + '3-4' => 'Julianne.OConner@kory.org South Elvis', + '4-5' => 'Lucio_Hettinger@annie.ca Roscoeview', + '5-6' => 'Karley_Dach@jasper.info South Christy', + '6-7' => 'Telly.Hoeger@billy.biz Howemouth', + '7-8' => 'Sherwood@rosamond.me Aliyaview', + '8-9' => 'Chaim_McDermott@dana.io Bartholomebury', + '9-10' => 'Rey.Padberg@karina.biz Lebsackbury', +); + +# we need to make sure that the async request is processed before starting the test +# to do so we create a temporary wrapper to catch the result of the request + +sub wrapper { + my $orig_func = shift; + $orig_func->(@_); + ok($_[0]->{'code'} == 200, "http request: 200 OK"); + @readings = sort split ',', ReadingsVal('11_JsonMod', '.computedReadings', undef); + # check num readings (10) + # is(scalar @readings, 10, "multi() 10 readings created"); + + if (0) { + print "\%_11_test_data = (\n"; + foreach (@readings) { + print "\t'$_' => '" . $defs{'11_JsonMod'}->{'READINGS'}->{$_}->{'VAL'} . "',\n"; + } + print ");\n"; + printf "%d keys created \n", scalar @readings; + }; + + @expected = sort keys %_11_test_data; + is((@readings), (@expected), 'all expected readings created'); + + foreach (@readings) { + my $val; + is($val = ReadingsVal('11_JsonMod', $_, undef), $_11_test_data{$_}, "reading '$_' is '$val'"); + }; + + done_testing; + exit(0); +}; + +{ + no warnings 'redefine'; + my $original_func_ref = \&JsonMod_ApiResponse; + *JsonMod_ApiResponse = sub { wrapper($original_func_ref, @_); }; +}; + +fhem('set 11_JsonMod reread'); + + +1; diff --git a/fhem/t/FHEM/98_JsonMod/goessner.json b/fhem/t/FHEM/98_JsonMod/goessner.json new file mode 100644 index 000000000..68da2a417 --- /dev/null +++ b/fhem/t/FHEM/98_JsonMod/goessner.json @@ -0,0 +1,31 @@ +{ "store": { + "book": [ + { "category": "reference", + "author": "Nigel Rees", + "title": "Sayings of the Century", + "price": 8.95 + }, + { "category": "fiction", + "author": "Evelyn Waugh", + "title": "Sword of Honour", + "price": 12.99 + }, + { "category": "fiction", + "author": "Herman Melville", + "title": "Moby Dick", + "isbn": "0-553-21311-3", + "price": 8.99 + }, + { "category": "fiction", + "author": "J. R. R. Tolkien", + "title": "The Lord of the Rings", + "isbn": "0-395-19395-8", + "price": 22.99 + } + ], + "bicycle": { + "color": "red", + "price": 19.95 + } + } +} \ No newline at end of file