Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
80017100
Unverified
Commit
80017100
authored
1 year ago
by
metamben
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add test for available-metrics and available-segments (#37767)
Part of #37173.
parent
09dc4515
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/metabase/lib/metric.cljc
+16
-15
16 additions, 15 deletions
src/metabase/lib/metric.cljc
test/metabase/lib/metric_test.cljc
+27
-8
27 additions, 8 deletions
test/metabase/lib/metric_test.cljc
test/metabase/lib/segment_test.cljc
+22
-8
22 additions, 8 deletions
test/metabase/lib/segment_test.cljc
with
65 additions
and
31 deletions
src/metabase/lib/metric.cljc
+
16
−
15
View file @
80017100
...
...
@@ -97,18 +97,19 @@
(
available-metrics
query
-1
))
([
query
:-
::lib.schema/query
stage-number
:-
:int
]
(
when-let
[
source-table-id
(
lib.util/source-table-id
query
)]
(
let
[
metrics
(
lib.metadata.protocols/metrics
(
lib.metadata/->metadata-provider
query
)
source-table-id
)
metric-aggregations
(
into
{}
(
keep-indexed
(
fn
[
index
aggregation-clause
]
(
when
(
lib.util/clause-of-type?
aggregation-clause
:metric
)
[(
get
aggregation-clause
2
)
index
])))
(
:aggregation
(
lib.util/query-stage
query
stage-number
)))]
(
cond
(
empty?
metrics
)
nil
(
empty?
metric-aggregations
)
(
vec
metrics
)
:else
(
mapv
(
fn
[
metric-metadata
]
(
let
[
aggregation-pos
(
->
metric-metadata
:id
metric-aggregations
)]
(
cond->
metric-metadata
aggregation-pos
(
assoc
:aggregation-position
aggregation-pos
))))
metrics
))))))
(
when
(
zero?
(
lib.util/canonical-stage-index
query
stage-number
))
(
when-let
[
source-table-id
(
lib.util/source-table-id
query
)]
(
let
[
metrics
(
lib.metadata.protocols/metrics
(
lib.metadata/->metadata-provider
query
)
source-table-id
)
metric-aggregations
(
into
{}
(
keep-indexed
(
fn
[
index
aggregation-clause
]
(
when
(
lib.util/clause-of-type?
aggregation-clause
:metric
)
[(
get
aggregation-clause
2
)
index
])))
(
:aggregation
(
lib.util/query-stage
query
stage-number
)))]
(
cond
(
empty?
metrics
)
nil
(
empty?
metric-aggregations
)
(
vec
metrics
)
:else
(
mapv
(
fn
[
metric-metadata
]
(
let
[
aggregation-pos
(
->
metric-metadata
:id
metric-aggregations
)]
(
cond->
metric-metadata
aggregation-pos
(
assoc
:aggregation-position
aggregation-pos
))))
metrics
)))))))
This diff is collapsed.
Click to expand it.
test/metabase/lib/metric_test.cljc
+
27
−
8
View file @
80017100
...
...
@@ -16,14 +16,18 @@
:aggregation
[[
:sum
[
:field
(
meta/id
:venues
:price
)
nil
]]]
:filter
[
:=
[
:field
(
meta/id
:venues
:price
)
nil
]
4
]})
(
def
^
:private
metrics-db
{
:metrics
[{
:id
metric-id
:name
"Sum of Cans"
:table-id
(
meta/id
:venues
)
:definition
metric-definition
:description
"Number of toucans plus number of pelicans"
}]})
(
def
^
:private
metadata-provider
(
lib.tu/mock-metadata-provider
meta/metadata-provider
{
:metrics
[{
:id
metric-id
:name
"Sum of Cans"
:table-id
(
meta/id
:venues
)
:definition
metric-definition
:description
"Number of toucans plus number of pelicans"
}]}))
(
lib.tu/mock-metadata-provider
meta/metadata-provider
metrics-db
))
(
def
^
:private
metadata-provider-with-cards
(
lib.tu/mock-metadata-provider
lib.tu/metadata-provider-with-mock-cards
metrics-db
))
(
def
^
:private
metric-clause
[
:metric
{
:lib/uuid
(
str
(
random-uuid
))}
metric-id
])
...
...
@@ -119,7 +123,22 @@
(
map
#
(
lib/display-info
query-with-metric
%
)
metrics
)))))))
(
testing
"query with different Table -- don't return Metrics"
(
is
(
nil?
(
lib/available-metrics
(
lib/query
metadata-provider
(
meta/table-metadata
:orders
)))))))
(
is
(
nil?
(
lib/available-metrics
(
lib/query
metadata-provider
(
meta/table-metadata
:orders
))))))
(
testing
"for subsequent stages -- don't return Metrics (#37173)"
(
let
[
query
(
lib/append-stage
(
lib/query
metadata-provider
(
meta/table-metadata
:venues
)))]
(
is
(
nil?
(
lib/available-metrics
query
)))
(
are
[
stage-number
]
(
nil?
(
lib/available-metrics
query
stage-number
))
1
-1
)))
(
testing
"query with different source table joining the metrics table -- don't return Metrics"
(
let
[
query
(
->
(
lib/query
metadata-provider
(
meta/table-metadata
:categories
))
(
lib/join
(
->
(
lib/join-clause
(
lib/query
metadata-provider
(
meta/table-metadata
:venues
))
[(
lib/=
(
meta/field-metadata
:venues
:price
)
4
)])
(
lib/with-join-fields
:all
))))]
(
is
(
nil?
(
lib/available-metrics
query
)))))
(
testing
"query based on a card -- don't return Metrics"
(
doseq
[
card-key
[
:venues
:venues/native
]]
(
let
[
query
(
lib/query
metadata-provider-with-cards
(
card-key
lib.tu/mock-cards
))]
(
is
(
nil?
(
lib/available-metrics
(
lib/append-stage
query
))))))))
(
deftest
^
:parallel
aggregate-with-metric-test
(
testing
"Should be able to pass a Metric metadata to `aggregate`"
...
...
This diff is collapsed.
Click to expand it.
test/metabase/lib/segment_test.cljc
+
22
−
8
View file @
80017100
...
...
@@ -19,14 +19,18 @@
[
:>
[
:field
(
meta/id
:venues
:id
)
nil
]
[
:*
[
:field
(
meta/id
:venues
:price
)
nil
]
11
]]
[
:contains
[
:field
(
meta/id
:venues
:name
)
nil
]
"BBQ"
{
:case-sensitive
true
}]]})
(
def
^
:private
segments-db
{
:segments
[{
:id
segment-id
:name
"PriceID-BBQ"
:table-id
(
meta/id
:venues
)
:definition
segment-definition
:description
"The ID is greater than 11 times the price and the name contains \"BBQ\"."
}]})
(
def
^
:private
metadata-provider
(
lib.tu/mock-metadata-provider
meta/metadata-provider
{
:segments
[{
:id
segment-id
:name
"PriceID-BBQ"
:table-id
(
meta/id
:venues
)
:definition
segment-definition
:description
"The ID is greater than 11 times the price and the name contains \"BBQ\"."
}]}))
(
lib.tu/mock-metadata-provider
meta/metadata-provider
segments-db
))
(
def
^
:private
metadata-provider-with-cards
(
lib.tu/mock-metadata-provider
lib.tu/metadata-provider-with-mock-cards
segments-db
))
(
def
^
:private
segment-clause
[
:segment
{
:lib/uuid
(
str
(
random-uuid
))}
segment-id
])
...
...
@@ -97,7 +101,17 @@
(
lib/available-segments
multi-stage-query
0
)
(
lib/available-segments
multi-stage-query
-2
)))))))
(
testing
"query with different Table -- don't return Segments"
(
is
(
nil?
(
lib/available-segments
(
lib/query
metadata-provider
(
meta/table-metadata
:orders
)))))))
(
is
(
nil?
(
lib/available-segments
(
lib/query
metadata-provider
(
meta/table-metadata
:orders
))))))
(
testing
"query with different source table joining the segments table -- don't return Segments"
(
let
[
query
(
->
(
lib/query
metadata-provider
(
meta/table-metadata
:categories
))
(
lib/join
(
->
(
lib/join-clause
(
lib/query
metadata-provider
(
meta/table-metadata
:venues
))
[(
lib/=
(
meta/field-metadata
:venues
:price
)
4
)])
(
lib/with-join-fields
:all
))))]
(
is
(
nil?
(
lib/available-segments
query
)))))
(
testing
"query based on a card -- don't return Segments"
(
doseq
[
card-key
[
:venues
:venues/native
]]
(
let
[
query
(
lib/query
metadata-provider-with-cards
(
card-key
lib.tu/mock-cards
))]
(
is
(
nil?
(
lib/available-segments
(
lib/append-stage
query
))))))))
(
deftest
^
:parallel
filter-with-segment-test
(
testing
"Should be able to pass a Segment metadata to `filter`"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment