From 96abce51b7071d9f0f67a45abaa552f2e444b95f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=B3ra=20Kocsis?= <dora@dynare.org>
Date: Mon, 30 Mar 2020 12:58:16 +0200
Subject: [PATCH] add scripts for GitLab testing

---
 .gitignore                                    |  2 +
 .gitlab-ci.yml                                | 11 ++++
 Makefile                                      | 15 +++++
 {tests => archive}/run_all_tests_matlab.m     |  0
 tests/runalltests.m                           | 63 +++++++++++++++++++
 tests/test_fetch_series_by_api_link.m         | 15 ++++-
 tests/test_fetch_series_by_code.m             | 15 ++++-
 tests/test_fetch_series_by_code_mask.m        | 15 ++++-
 ...est_fetch_series_by_code_mask_with_plus.m} | 15 ++++-
 tests/test_fetch_series_by_dimension.m        | 15 ++++-
 tests/test_fetch_series_by_id.m               | 15 ++++-
 ...etch_series_by_ids_in_different_datasets.m | 15 ++++-
 ...test_fetch_series_by_ids_in_same_dataset.m | 15 ++++-
 tests/test_fetch_series_of_dataset.m          | 15 ++++-
 ...t_fetch_series_with_filter_on_one_series.m | 15 ++++-
 tests/test_fetch_series_with_max_nb_series.m  | 15 ++++-
 tests/test_fetch_series_with_na_values.m      | 15 ++++-
 17 files changed, 247 insertions(+), 24 deletions(-)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 Makefile
 rename {tests => archive}/run_all_tests_matlab.m (100%)
 create mode 100644 tests/runalltests.m
 rename tests/{test_fetch_series_by_code_mask_with_plus_in_dimension_code.m => test_fetch_series_by_code_mask_with_plus.m} (71%)

diff --git a/.gitignore b/.gitignore
index 5db5c40..c1d403d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,5 +11,7 @@
 \#*\#
 *.mat
 *.asv
+tests/git.info
+tests/git.last-commit-hash
 
 /missing_dbnomics
\ No newline at end of file
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..af480e6
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,11 @@
+variables:
+  GIT_SUBMODULE_STRATEGY: normal
+  TERM: linux
+
+before_script:
+  - git clone https://git.dynare.org/Dynare/m-unit-tests
+
+test_matlab:
+  stage: test
+  script:
+    - make check-matlab
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..926f34e
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,15 @@
+OCTAVE ?= octave-cli
+MATLAB ?= $(shell which matlab)
+
+all: check-matlab
+
+m-unit-tests/src/mtest.m:
+	git clone https://git.dynare.org/Dynare/m-unit-tests
+
+check-matlab: m-unit-tests/src/mtest.m
+	@$(MATLAB)  -nosplash -nodisplay -r "addpath([pwd '/m-unit-tests/src']); cd tests; runalltests; quit" && [ ! -f ./tests/failed ] && [ -f ./tests/pass ]
+
+check-clean:
+	rm -f tests/*_test_*.m tests/*.csv tests/*.xls tests/*.xlsx tests/*.mat tests/failed tests/datafile_for_test
+	rm -f git.info git.last-commit-hash tests/pass tests/failed tests/*.spc
+	rm -rf m-unit-tests
diff --git a/tests/run_all_tests_matlab.m b/archive/run_all_tests_matlab.m
similarity index 100%
rename from tests/run_all_tests_matlab.m
rename to archive/run_all_tests_matlab.m
diff --git a/tests/runalltests.m b/tests/runalltests.m
new file mode 100644
index 0000000..e0c15a2
--- /dev/null
+++ b/tests/runalltests.m
@@ -0,0 +1,63 @@
+function runalltests()
+
+% Copyright (C) 2020 Dynare Team
+%
+% This code is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare dseries submodule is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+opath = path();
+
+system('rm -f failed');
+system('rm -f pass');
+
+% Check that the m-unit-tests module is available.
+try
+    initialize_unit_tests_toolbox;
+catch
+    error('Missing dependency: m-unit-tests module is not available.')
+end
+
+% Get path to the current script
+unit_tests_root = strrep(which('runalltests'),'runalltests.m','');
+
+% Initialize the mdbnomics module
+try
+    initialize_mdbnomics();
+catch
+    addpath([unit_tests_root '../src']);
+    initialize_mdbnomics();
+end
+
+warning off
+
+if isoctave()
+    if ~user_has_octave_forge_package('io')
+        error('Missing dependency: io package is not available.')
+    end
+    more off;
+end
+
+r = run_unitary_tests_in_directory(unit_tests_root(1:end-1));
+
+delete('*.log');
+
+if any(~[r{:,3}])
+    system('touch failed');
+else
+    system('touch pass');
+end
+
+warning on
+path(opath);
+
+display_report(r);
diff --git a/tests/test_fetch_series_by_api_link.m b/tests/test_fetch_series_by_api_link.m
index 45e5825..9ba7ccc 100644
--- a/tests/test_fetch_series_by_api_link.m
+++ b/tests/test_fetch_series_by_api_link.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_api_link()
+function test_fetch_series_by_api_link() % --*-- Unitary tests --*--
 df = fetch_series_by_api_link("https://api.db.nomics.world/v22/series/BIS/long_pp?limit=1000&offset=0&q=&observations=1&align_periods=1&dimensions=%7B%7D");
     
 idx = find(strcmp('provider_code',df(1,:)));
@@ -14,4 +14,15 @@ assert(dataset_codes{1} == "long_pp");
 idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) > 1);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_api_link();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_code.m b/tests/test_fetch_series_by_code.m
index 6386507..cd91908 100644
--- a/tests/test_fetch_series_by_code.m
+++ b/tests/test_fetch_series_by_code.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_code()
+function test_fetch_series_by_code() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "AMECO", 'dataset_code', "ZUTN", 'series_code',"EA19.1.0.0.0.ZUTN");
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -15,4 +15,15 @@ idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 1);
 assert(series_codes{1} == "EA19.1.0.0.0.ZUTN");
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_code();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_code_mask.m b/tests/test_fetch_series_by_code_mask.m
index f2e6b89..55f59e8 100644
--- a/tests/test_fetch_series_by_code_mask.m
+++ b/tests/test_fetch_series_by_code_mask.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_code_mask()
+function test_fetch_series_by_code_mask() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "IMF", 'dataset_code', "CPI", 'series_code', "M.FR+DE.PCPIEC_IX+PCPIA_IX");
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -14,4 +14,15 @@ assert(dataset_codes{1} == "CPI");
 idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 4);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_code_mask();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_code_mask_with_plus_in_dimension_code.m b/tests/test_fetch_series_by_code_mask_with_plus.m
similarity index 71%
rename from tests/test_fetch_series_by_code_mask_with_plus_in_dimension_code.m
rename to tests/test_fetch_series_by_code_mask_with_plus.m
index 757de4f..f426f8b 100644
--- a/tests/test_fetch_series_by_code_mask_with_plus_in_dimension_code.m
+++ b/tests/test_fetch_series_by_code_mask_with_plus.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_code_mask_with_plus_in_dimension_code()
+function test_fetch_series_by_code_mask_with_plus() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "SCB", 'dataset_code', "AKIAM", 'series_code', '"J+K"+"G+H".AM0301C1');
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -16,4 +16,15 @@ series_codes = unique(df(2:end,idx));
 check = {'J+K.AM0301C1', 'G+H.AM0301C1'}';  
 logA = cell2mat(cellfun(@(c)strcmp(c,series_codes),check,'UniformOutput',false));
 assert(sum(logA)==2);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_code_mask_with_plus();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
diff --git a/tests/test_fetch_series_by_dimension.m b/tests/test_fetch_series_by_dimension.m
index f30e8a1..a1afd9e 100644
--- a/tests/test_fetch_series_by_dimension.m
+++ b/tests/test_fetch_series_by_dimension.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_dimension()
+function test_fetch_series_by_dimension() % --*-- Unitary tests --*--
 df = fetch_series('provider_code',"WB",'dataset_code',"DB", 'dimensions', '{"country":["ES","FR","IT"],"indicator":["IC.REG.COST.PC.FE.ZS.DRFN"]}');
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -14,4 +14,15 @@ assert(dataset_codes{1} == "DB");
 idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 3);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_dimension();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_id.m b/tests/test_fetch_series_by_id.m
index a24eb70..b74b42a 100644
--- a/tests/test_fetch_series_by_id.m
+++ b/tests/test_fetch_series_by_id.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_id()
+function test_fetch_series_by_id() % --*-- Unitary tests --*--
 df = fetch_series('series_ids',"AMECO/ZUTN/EA19.1.0.0.0.ZUTN");
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -15,4 +15,15 @@ idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 1);
 assert(series_codes{1} == "EA19.1.0.0.0.ZUTN");
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_id();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_ids_in_different_datasets.m b/tests/test_fetch_series_by_ids_in_different_datasets.m
index 9bc2103..1f25d82 100644
--- a/tests/test_fetch_series_by_ids_in_different_datasets.m
+++ b/tests/test_fetch_series_by_ids_in_different_datasets.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_ids_in_different_datasets()
+function test_fetch_series_by_ids_in_different_datasets() % --*-- Unitary tests --*--
 df = fetch_series('series_ids', ["AMECO/ZUTN/EA19.1.0.0.0.ZUTN", "BIS/cbs/Q.S.5A.4B.F.B.A.A.LC1.A.1C"]);
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -18,4 +18,15 @@ series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 2);
 assert(series_codes{1} == "EA19.1.0.0.0.ZUTN");
 assert(series_codes{2} == "Q.S.5A.4B.F.B.A.A.LC1.A.1C");
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_ids_in_different_datasets();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_by_ids_in_same_dataset.m b/tests/test_fetch_series_by_ids_in_same_dataset.m
index 7924f0b..9be930a 100644
--- a/tests/test_fetch_series_by_ids_in_same_dataset.m
+++ b/tests/test_fetch_series_by_ids_in_same_dataset.m
@@ -1,4 +1,4 @@
-function test_fetch_series_by_ids_in_same_dataset()
+function test_fetch_series_by_ids_in_same_dataset() % --*-- Unitary tests --*--
 df = fetch_series('series_ids', ["AMECO/ZUTN/EA19.1.0.0.0.ZUTN",...
                                  "AMECO/ZUTN/DNK.1.0.0.0.ZUTN"]);
     
@@ -17,4 +17,15 @@ series_codes = unique(df(2:end,idx));
 assert(length(series_codes) == 2);
 assert(series_codes{1} == "DNK.1.0.0.0.ZUTN");
 assert(series_codes{2} == "EA19.1.0.0.0.ZUTN");
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_by_ids_in_same_dataset();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_of_dataset.m b/tests/test_fetch_series_of_dataset.m
index 3798526..4f86268 100644
--- a/tests/test_fetch_series_of_dataset.m
+++ b/tests/test_fetch_series_of_dataset.m
@@ -1,4 +1,4 @@
-function test_fetch_series_of_dataset()
+function test_fetch_series_of_dataset() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "AMECO", 'dataset_code', "ZUTN");
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -14,4 +14,15 @@ assert(dataset_codes{1} == "ZUTN");
 idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) > 1);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_of_dataset();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_with_filter_on_one_series.m b/tests/test_fetch_series_with_filter_on_one_series.m
index 27dea01..596c24d 100644
--- a/tests/test_fetch_series_with_filter_on_one_series.m
+++ b/tests/test_fetch_series_with_filter_on_one_series.m
@@ -1,4 +1,4 @@
-function test_fetch_series_with_filter_on_one_series()
+function test_fetch_series_with_filter_on_one_series() % --*-- Unitary tests --*--
 filters_ = '[{"code": "interpolate", "parameters": {"frequency": "monthly", "method": "spline"}}]';
 df = fetch_series('provider_code', "AMECO", 'dataset_code', "ZUTN", 'series_code', "DEU.1.0.0.0.ZUTN", 'dbnomics_filters', filters_);
 
@@ -21,4 +21,15 @@ idx_freq = find(strcmp('x_frequency',df(1,:)));
 frequencies = unique(df(2:end, idx_freq));
 assert(any(strcmp(frequencies, "monthly")));
 assert(any(contains(series_codes, "_filtered")));
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_with_filter_on_one_series();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_with_max_nb_series.m b/tests/test_fetch_series_with_max_nb_series.m
index d09c92d..5bae432 100644
--- a/tests/test_fetch_series_with_max_nb_series.m
+++ b/tests/test_fetch_series_with_max_nb_series.m
@@ -1,4 +1,4 @@
-function test_fetch_series_with_max_nb_series()
+function test_fetch_series_with_max_nb_series() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "AMECO", 'dataset_code', "ZUTN", 'max_nb_series',20);
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -14,4 +14,15 @@ assert(dataset_codes{1} == "ZUTN");
 idx = find(strcmp('series_code',df(1,:)));
 series_codes = unique(df(2:end,idx));
 assert(length(series_codes) <= 20);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_with_max_nb_series();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
diff --git a/tests/test_fetch_series_with_na_values.m b/tests/test_fetch_series_with_na_values.m
index 7195532..ae3b873 100644
--- a/tests/test_fetch_series_with_na_values.m
+++ b/tests/test_fetch_series_with_na_values.m
@@ -1,4 +1,4 @@
-function test_fetch_series_with_na_values()
+function test_fetch_series_with_na_values() % --*-- Unitary tests --*--
 df = fetch_series('provider_code', "AMECO", 'dataset_code', "ZUTN", 'series_code', "DEU.1.0.0.0.ZUTN");
 
 idx = find(strcmp('provider_code',df(1,:)));
@@ -20,4 +20,15 @@ assert(any(strcmp('NA', df(2:end,idx))) == true);
 
 idx = find(strcmp('value',df(1,:)));
 assert(any(isnan(cell2mat(df(2:end,idx)))) == true);
-end
\ No newline at end of file
+end
+
+%@test:1
+%$ try
+%$    test_fetch_series_with_na_values();
+%$    t(1) = true;
+%$ catch
+%$    t(1) = false;
+%$ end
+%$
+%$ T = t(1);
+%@eof:1
\ No newline at end of file
-- 
GitLab