From b91bf15c00b3c27d95904abc9cc3d571cf455c05 Mon Sep 17 00:00:00 2001
From: Lucas Fernandes de Oliveira <lfo14@inf.ufpr.br>
Date: Fri, 11 Aug 2017 10:51:58 -0300
Subject: [PATCH] Issue #32: Add test to materialized view that is not origin

Signed-off-by: Lucas Fernandes de Oliveira <lfo14@inf.ufpr.br>
---
 config/ci_test.yaml.example       | 10 ++++++++++
 src/adapter/postgres.spec.ts      | 19 +++++++++++++++++++
 test/postgres/fixtures/view9.json |  7 +++++++
 test/scenario.ts                  | 11 ++++++++++-
 4 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 test/postgres/fixtures/view9.json

diff --git a/config/ci_test.yaml.example b/config/ci_test.yaml.example
index 4a6d5858..e532112d 100644
--- a/config/ci_test.yaml.example
+++ b/config/ci_test.yaml.example
@@ -93,6 +93,16 @@ schema:
                 - "dim:6"
             metrics:
                 - "met:9"
+        -
+            alias: "view 9"
+            data: "test/postgres/fixtures/view9.json"
+            origin: false
+            dimensions:
+                - "dim:2"
+            metrics:
+                - "met:5"
+                - "met:6"
+                - "met:7"
     metrics:
         -
             name: "met:0"
diff --git a/src/adapter/postgres.spec.ts b/src/adapter/postgres.spec.ts
index 8b77b184..7f1367bc 100644
--- a/src/adapter/postgres.spec.ts
+++ b/src/adapter/postgres.spec.ts
@@ -298,4 +298,23 @@ describe("postgres adapter", () => {
             done();
         });
     });
+
+    it("should get data using COUNT when the view is not origin", (done) => {
+        let view = adapterScenario.notOriginCount;
+        adapter.getDataFromView(view, (err, result) => {
+            expect(err).to.be.a("null");
+            expect(result).to.be.an("array");
+            expect(result).to.have.length(5);
+            expect(result[0]).to.be.an("object");
+            let keys: string[] = [];
+            keys = keys.concat(view.metrics.map((item) => item.name));
+            keys = keys.concat(view.dimensions.map((item) => item.name));
+            result.forEach((row) => {
+                expect(row).to.be.an("object");
+                expect(row).to.have.all.keys(keys);
+            });
+
+            done();
+        });
+    });
 });
diff --git a/test/postgres/fixtures/view9.json b/test/postgres/fixtures/view9.json
new file mode 100644
index 00000000..4c5726a8
--- /dev/null
+++ b/test/postgres/fixtures/view9.json
@@ -0,0 +1,7 @@
+[
+{"dim:2":"1","met:5":"1","met:6":"1","met:7":"1"},
+{"dim:2":"2","met:5":"2","met:6":"1","met:7":"1"},
+{"dim:2":"3","met:5":"3","met:6":"1","met:7":"1"},
+{"dim:2":"4","met:5":"4","met:6":"1","met:7":"1"},
+{"dim:2":"5","met:5":"5","met:6":"1","met:7":"1"}
+]
diff --git a/test/scenario.ts b/test/scenario.ts
index fa42a220..19330b49 100644
--- a/test/scenario.ts
+++ b/test/scenario.ts
@@ -51,6 +51,7 @@ interface AdapterScenario {
     gtltView: View;
     geleView: View;
     notMatchFilterView: View;
+    notOriginCount: View;
 }
 
 interface DataCtrlScenario {
@@ -299,6 +300,13 @@ const withSelView = new View({
     childViews: [views[0], views[4]]
 });
 
+const notOriginCount = new View({
+    metrics: [mets[5], mets[6], mets[7]],
+    dimensions: [dims[2]],
+    materialized: true,
+    origin: false
+});
+
 export const engineScenario: EngineScenario = {
     metrics: mets,
     dimensions: dims,
@@ -322,7 +330,8 @@ export const adapterScenario: AdapterScenario = {
     notEqualView: notEqualView,
     gtltView: gtltView,
     geleView: geleView,
-    notMatchFilterView: notMatchFilterView
+    notMatchFilterView: notMatchFilterView,
+    notOriginCount: notOriginCount
 };
 
 export const dataCtrlScenario: DataCtrlScenario = {
-- 
GitLab