Skip to content

Commit 8073129

Browse files
committed
sqlite test passing create concept counts table
1 parent ecc0af1 commit 8073129

File tree

3 files changed

+56
-107
lines changed

3 files changed

+56
-107
lines changed
+22-100
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{DEFAULT @table_is_temp = FALSE}
2-
<<<<<<< HEAD
32
{DEFAULT @remove_current_table = TRUE}
4-
=======
5-
{DEFAULT @use_achilles = FALSE}
6-
>>>>>>> darwin_sprint
73

84
{@remove_current_table} ? {
95
{@table_is_temp} ? {
@@ -23,162 +19,88 @@ INTO @concept_counts_table
2319
} : {
2420
INTO @work_database_schema.@concept_counts_table
2521
}
26-
27-
{@use_achilles} ? {
28-
29-
-- Achilles analysis ids used for creating the concept counts table
30-
-- condition: 400 (persons), 401 (standard concepts), 425 (source concepts)
31-
-- drug: 700 (persons), 701 (standard concepts), 725 (source concepts)
32-
-- procedure: 600 (persons), 601 (standard concepts), 625 (source concepts)
33-
-- measurement: 1800 (persons), 1801 (standard concepts), 1825 (source concepts)
34-
-- observation: 800 (persons), 801 (standard concepts), 825 (source concepts)
35-
FROM (
36-
SELECT
37-
CAST(stratum_1 AS INT) AS concept_id,
38-
count_value AS concept_count
39-
FROM @achilles_database_schema.achilles_results
40-
WHERE analysis_id IN (401,601,701,801,1801,425,625,725,825,1825) AND stratum_1 != '0'
41-
) q1
42-
LEFT JOIN
43-
(
44-
SELECT
45-
CAST(stratum_1 AS INT) AS concept_id,
46-
count_value AS concept_subjects
47-
FROM @achilles_database_schema.achilles_results
48-
WHERE analysis_id IN (400,600,700,800,1800) AND stratum_1 != '0'
49-
) q2
50-
ON q1.concept_id = q2.concept_id
51-
52-
} : {
53-
5422
FROM (
5523
SELECT condition_concept_id AS concept_id,
5624
COUNT_BIG(*) AS concept_count,
57-
COUNT_BIG(DISTINCT co.person_id) AS concept_subjects
58-
FROM @cdm_database_schema.condition_occurrence co
59-
JOIN @cdm_database_schema.observation_period op
60-
ON co.person_id = op.person_id
61-
AND co.condition_start_date >= op.observation_period_start_date
62-
AND co.condition_start_date <= op.observation_period_end_date
25+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
26+
FROM @cdm_database_schema.condition_occurrence
6327
GROUP BY condition_concept_id
6428

6529
UNION ALL
6630

6731
SELECT condition_source_concept_id AS concept_id,
6832
COUNT_BIG(*) AS concept_count,
69-
COUNT_BIG(DISTINCT co.person_id) AS concept_subjects
70-
FROM @cdm_database_schema.condition_occurrence co
71-
JOIN @cdm_database_schema.observation_period op
72-
ON co.person_id = op.person_id
73-
AND co.condition_start_date >= op.observation_period_start_date
74-
AND co.condition_start_date <= op.observation_period_end_date
33+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
34+
FROM @cdm_database_schema.condition_occurrence
7535
GROUP BY condition_source_concept_id
7636

7737
UNION ALL
7838

7939
SELECT drug_concept_id AS concept_id,
8040
COUNT_BIG(*) AS concept_count,
81-
COUNT_BIG(DISTINCT de.person_id) AS concept_subjects
82-
FROM @cdm_database_schema.drug_exposure de
83-
JOIN @cdm_database_schema.observation_period op
84-
ON de.person_id = op.person_id
85-
AND de.drug_exposure_start_date >= op.observation_period_start_date
86-
AND de.drug_exposure_start_date <= op.observation_period_end_date
41+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
42+
FROM @cdm_database_schema.drug_exposure
8743
GROUP BY drug_concept_id
8844

8945
UNION ALL
9046

9147
SELECT drug_source_concept_id AS concept_id,
9248
COUNT_BIG(*) AS concept_count,
93-
COUNT_BIG(DISTINCT de.person_id) AS concept_subjects
94-
FROM @cdm_database_schema.drug_exposure de
95-
JOIN @cdm_database_schema.observation_period op
96-
ON de.person_id = op.person_id
97-
AND de.drug_exposure_start_date >= op.observation_period_start_date
98-
AND de.drug_exposure_start_date <= op.observation_period_end_date
49+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
50+
FROM @cdm_database_schema.drug_exposure
9951
GROUP BY drug_source_concept_id
10052

10153
UNION ALL
10254

10355
SELECT procedure_concept_id AS concept_id,
10456
COUNT_BIG(*) AS concept_count,
105-
COUNT_BIG(DISTINCT po.person_id) AS concept_subjects
106-
FROM @cdm_database_schema.procedure_occurrence po
107-
JOIN @cdm_database_schema.observation_period op
108-
ON po.person_id = op.person_id
109-
AND po.procedure_date >= op.observation_period_start_date
110-
AND po.procedure_date <= op.observation_period_end_date
57+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
58+
FROM @cdm_database_schema.procedure_occurrence
11159
GROUP BY procedure_concept_id
11260

11361
UNION ALL
11462

11563
SELECT procedure_source_concept_id AS concept_id,
11664
COUNT_BIG(*) AS concept_count,
117-
COUNT_BIG(DISTINCT po.person_id) AS concept_subjects
118-
FROM @cdm_database_schema.procedure_occurrence po
119-
JOIN @cdm_database_schema.observation_period op
120-
ON po.person_id = op.person_id
121-
AND po.procedure_date >= op.observation_period_start_date
122-
AND po.procedure_date <= op.observation_period_end_date
65+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
66+
FROM @cdm_database_schema.procedure_occurrence
12367
GROUP BY procedure_source_concept_id
12468

12569
UNION ALL
12670

12771
SELECT measurement_concept_id AS concept_id,
12872
COUNT_BIG(*) AS concept_count,
129-
COUNT_BIG(DISTINCT m.person_id) AS concept_subjects
130-
FROM @cdm_database_schema.measurement m
131-
JOIN @cdm_database_schema.observation_period op
132-
ON m.person_id = op.person_id
133-
AND m.measurement_date >= op.observation_period_start_date
134-
AND m.measurement_date <= op.observation_period_end_date
73+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
74+
FROM @cdm_database_schema.measurement
13575
GROUP BY measurement_concept_id
13676

13777
UNION ALL
138-
78+
13979
SELECT measurement_source_concept_id AS concept_id,
14080
COUNT_BIG(*) AS concept_count,
141-
COUNT_BIG(DISTINCT m.person_id) AS concept_subjects
142-
FROM @cdm_database_schema.measurement m
143-
JOIN @cdm_database_schema.observation_period op
144-
ON m.person_id = op.person_id
145-
AND m.measurement_date >= op.observation_period_start_date
146-
AND m.measurement_date <= op.observation_period_end_date
81+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
82+
FROM @cdm_database_schema.measurement
14783
GROUP BY measurement_source_concept_id
14884

14985
UNION ALL
15086

15187
SELECT observation_concept_id AS concept_id,
15288
COUNT_BIG(*) AS concept_count,
153-
COUNT_BIG(DISTINCT o.person_id) AS concept_subjects
154-
FROM @cdm_database_schema.observation o
155-
JOIN @cdm_database_schema.observation_period op
156-
ON o.person_id = op.person_id
157-
AND o.observation_date >= op.observation_period_start_date
158-
AND o.observation_date <= op.observation_period_end_date
89+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
90+
FROM @cdm_database_schema.observation
15991
GROUP BY observation_concept_id
16092

16193
UNION ALL
16294

16395
SELECT observation_source_concept_id AS concept_id,
16496
COUNT_BIG(*) AS concept_count,
165-
COUNT_BIG(DISTINCT o.person_id) AS concept_subjects
166-
FROM @cdm_database_schema.observation o
167-
JOIN @cdm_database_schema.observation_period op
168-
ON o.person_id = op.person_id
169-
AND o.observation_date >= op.observation_period_start_date
170-
AND o.observation_date <= op.observation_period_end_date
97+
COUNT_BIG(DISTINCT person_id) AS concept_subjects
98+
FROM @cdm_database_schema.observation
17199
GROUP BY observation_source_concept_id
172-
<<<<<<< HEAD
173100
) tmp;
174101

175102
{@table_is_temp} ? {} : {
176103
ALTER TABLE @work_database_schema.@concept_counts_table
177104
ADD vocabulary_version VARCHAR(20) NULL;
178105
UPDATE @work_database_schema.@concept_counts_table SET vocabulary_version = (SELECT vocabulary_version FROM @cdm_database_schema.vocabulary WHERE vocabulary_id = 'None');
179-
}
180-
=======
181-
) tmp
182-
WHERE concept_id != 0;
183-
}
184-
>>>>>>> darwin_sprint
106+
}

tests/testthat/setup.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ library(testthat)
33

44
dbmsToTest <- c(
55
"sqlite"#,
6-
# "duckdb",
6+
# "duckdb"#,
77
# "postgresql",
88
# "redshift",
99
# "sql server",
@@ -238,3 +238,5 @@ for (nm in names(testServers)) {
238238
} else {
239239
message(paste("Skipping cohort generation on test server", nm))
240240
}
241+
242+
}

tests/testthat/test-externalConceptCounts.R

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
test_that("Creating and checking externalConceptCounts table", {
2-
if (dbms == "sqlite") {
3-
# Creating externalConceptCounts
4-
sql_lite_path <- file.path(test_path(), databaseFile)
5-
connectionDetails <- createConnectionDetails(dbms= "sqlite", server = sql_lite_path)
2+
if (dbmsToTest == "sqlite") {
3+
connectionDetails <- testServers[["sqlite"]]$connectionDetails
64
connection <- connect(connectionDetails)
7-
cdmDatabaseSchema <- "main"
5+
cdmDatabaseSchema <- testServers[["sqlite"]]$cdmDatabaseSchema
86
conceptCountsTable <- "concept_counts"
97
CohortDiagnostics::createConceptCountsTable(connectionDetails = connectionDetails,
108
cdmDatabaseSchema = cdmDatabaseSchema,
@@ -42,4 +40,31 @@ test_that("Creating and checking externalConceptCounts table", {
4240
expect_equal(vocabVersion, vocabVersionExternalConceptCountsTable[1,1])
4341
}
4442

45-
})
43+
})
44+
45+
test_that("Creating and checking externalConceptCounts temp table", {
46+
if (dbmsToTest == "sqlite") {
47+
# Creating externalConceptCounts
48+
# sql_lite_path <- file.path(test_path(), databaseFile)
49+
connectionDetails <- testServers[["sqlite"]]$connectionDetails
50+
connection <- connect(connectionDetails)
51+
cdmDatabaseSchema <- testServers[["sqlite"]]$cdmDatabaseSchema
52+
conceptCountsTable <- "concept_counts"
53+
CohortDiagnostics::createConceptCountsTable(connectionDetails = connectionDetails,
54+
cdmDatabaseSchema = cdmDatabaseSchema,
55+
tempEmulationSchema = NULL,
56+
conceptCountsTable = conceptCountsTable,
57+
conceptCountsDatabaseSchema = cdmDatabaseSchema,
58+
conceptCountsTableIsTemp = TRUE,
59+
removeCurrentTable = TRUE)
60+
61+
concept_counts_info <- querySql(connection, "PRAGMA table_info(concept_counts)")
62+
expect_equal(concept_counts_info$NAME, c("concept_id",
63+
"concept_count",
64+
"concept_subjects"))
65+
checkConceptCountsTableExists <- DatabaseConnector::dbExistsTable(connection,
66+
name = conceptCountsTable,
67+
databaseSchema = cdmDatabaseSchema)
68+
expect_true(checkConceptCountsTableExists)
69+
70+
})

0 commit comments

Comments
 (0)