SummaryDbTest.scala 16.4 KB
Newer Older
1
2
3
package nl.lumc.sasc.biopet.utils.summary.db

import java.io.File
4
import java.sql.Date
5
6
7
8
9
10
11

import org.scalatest.Matchers
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

import scala.concurrent.Await
import scala.concurrent.duration.Duration
Peter van 't Hof's avatar
WIP    
Peter van 't Hof committed
12
13
import scala.concurrent.ExecutionContext.Implicits.global

Peter van 't Hof's avatar
Peter van 't Hof committed
14
15
import SummaryDb.Implicts._
import nl.lumc.sasc.biopet.utils.summary.db.SummaryDb.{ NoLibrary, NoModule, NoSample }
16

17
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
18
 * Testing voor [[SummaryDb]]
Peter van 't Hof's avatar
Peter van 't Hof committed
19
20
 * Created by pjvanthof on 14/02/2017.
 */
21
22
class SummaryDbTest extends TestNGSuite with Matchers {

23
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
24
  def testRuns(): Unit = {
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val date = new Date(System.currentTimeMillis())

    Await.result(db.getRuns(), Duration.Inf) shouldBe empty
    val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    Await.result(db.getRuns(), Duration.Inf) shouldBe Seq(Schema.Run(runId, "name", "dir", "version", "hash", date))
    val runId2 = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    Await.result(db.getRuns(), Duration.Inf) shouldBe Seq(Schema.Run(runId, "name", "dir", "version", "hash", date), Schema.Run(runId2, "name", "dir", "version", "hash", date))

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
42
  def testSamples(): Unit = {
43
44
45
46
47
48
49
50
51
52
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val date = new Date(System.currentTimeMillis())

    val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    Await.result(db.getSamples(), Duration.Inf) shouldBe empty
    val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
53
    Await.result(db.createOrUpdateSample("test_sample", runId), Duration.Inf) shouldBe sampleId
54
55
56
57
    Await.result(db.getSamples(), Duration.Inf) shouldBe Seq(Schema.Sample(sampleId, "test_sample", runId, None))
    Await.result(db.getSampleName(sampleId), Duration.Inf) shouldBe Some("test_sample")
    Await.result(db.getSampleId(runId, "test_sample"), Duration.Inf) shouldBe Some(sampleId)
    Await.result(db.getSampleTags(sampleId), Duration.Inf) shouldBe None
58
59
    Await.result(db.createOrUpdateSample("test_sample", runId, Some("""{"test": "test"}""")), Duration.Inf) shouldBe sampleId
    Await.result(db.getSampleTags(sampleId), Duration.Inf) shouldBe Some(Map("test" -> "test"))
60
61
62

    val sampleId2 = Await.result(db.createSample("test_sample2", runId, Some("""{"test": "test"}""")), Duration.Inf)
    Await.result(db.getSampleTags(sampleId2), Duration.Inf) shouldBe Some(Map("test" -> "test"))
63
    Await.result(db.getSamples(), Duration.Inf) shouldBe Seq(Schema.Sample(sampleId, "test_sample", runId, Some("""{"test": "test"}""")), Schema.Sample(sampleId2, "test_sample2", runId, Some("""{"test": "test"}""")))
64
65
66
67
68

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
69
  def testLibraries(): Unit = {
70
71
72
73
74
75
76
77
78
79
80
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val date = new Date(System.currentTimeMillis())

    val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
    Await.result(db.getLibraries(), Duration.Inf) shouldBe empty
    val libraryId = Await.result(db.createLibrary("test_lib", runId, sampleId), Duration.Inf)
81
    Await.result(db.createOrUpdateLibrary("test_lib", runId, sampleId), Duration.Inf) shouldBe libraryId
82
83
84
85
86
87
88
89
90
91
92
93
94
95

    Await.result(db.getLibraries(), Duration.Inf) shouldBe Seq(Schema.Library(libraryId, "test_lib", runId, sampleId, None))
    Await.result(db.getLibraryName(libraryId), Duration.Inf) shouldBe Some("test_lib")
    Await.result(db.getLibraryId(runId, sampleId, "test_lib"), Duration.Inf) shouldBe Some(libraryId)
    Await.result(db.getLibraryTags(sampleId), Duration.Inf) shouldBe None

    val sampleId2 = Await.result(db.createLibrary("test_lib2", runId, sampleId, Some("""{"test": "test"}""")), Duration.Inf)
    Await.result(db.getLibraryTags(sampleId2), Duration.Inf) shouldBe Some(Map("test" -> "test"))
    Await.result(db.getLibraries(), Duration.Inf) shouldBe Seq(Schema.Library(sampleId, "test_lib", runId, sampleId, None), Schema.Library(sampleId2, "test_lib2", runId, sampleId, Some("""{"test": "test"}""")))

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
96
  def testPipelines(): Unit = {
97
98
99
100
101
102
103
104
105
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val date = new Date(System.currentTimeMillis())

    val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    Await.result(db.getPipelines(), Duration.Inf) shouldBe empty
Peter van 't Hof's avatar
Peter van 't Hof committed
106
    Await.result(db.getPipelineName(0), Duration.Inf) shouldBe None
107
    val pipelineId = Await.result(db.createPipeline("test", runId), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
108
    Await.result(db.getPipelineName(pipelineId), Duration.Inf) shouldBe Some("test")
109
110
111
112
113
114
115
116
117
    Await.result(db.getPipelines(), Duration.Inf) shouldBe Seq(Schema.Pipeline(pipelineId, "test", runId))
    Await.result(db.getPipelineId(runId, "test"), Duration.Inf) shouldBe Some(pipelineId)
    Await.result(db.createPipeline("test", runId), Duration.Inf) shouldBe pipelineId
    Await.result(db.getPipelines(), Duration.Inf) shouldBe Seq(Schema.Pipeline(pipelineId, "test", runId))

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
118
  def testModules(): Unit = {
119
120
121
122
123
124
125
126
127
128
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val date = new Date(System.currentTimeMillis())

    val runId = Await.result(db.createRun("name", "dir", "version", "hash", date), Duration.Inf)
    val pipelineId = Await.result(db.createPipeline("test", runId), Duration.Inf)
    Await.result(db.getModules(), Duration.Inf) shouldBe empty
Peter van 't Hof's avatar
Peter van 't Hof committed
129
    Await.result(db.getModuleName(pipelineId, 0), Duration.Inf) shouldBe None
130
131
    val moduleId = Await.result(db.createModule("test", runId, pipelineId), Duration.Inf)
    Await.result(db.getmoduleId(runId, "test", pipelineId), Duration.Inf) shouldBe Some(moduleId)
Peter van 't Hof's avatar
Peter van 't Hof committed
132
    Await.result(db.getModuleName(pipelineId, moduleId), Duration.Inf) shouldBe Some("test")
133
134
135
136
137
138
139
    Await.result(db.getModules(), Duration.Inf) shouldBe Seq(Schema.Module(pipelineId, "test", runId, pipelineId))
    Await.result(db.createModule("test", runId, pipelineId), Duration.Inf) shouldBe pipelineId
    Await.result(db.getModules(), Duration.Inf) shouldBe Seq(Schema.Module(pipelineId, "test", runId, pipelineId))

    db.close()
  }

140
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
141
  def testSettings(): Unit = {
142
143
144
145
146
147
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test" }"""), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
148
    Await.result(db.getSetting(0, 0, NoModule, NoSample, NoLibrary), Duration.Inf) shouldBe Some(Map("content" -> "test"))
149
    Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
150
    Await.result(db.getSetting(0, 0, NoModule, NoSample, NoLibrary), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
Peter van 't Hof's avatar
Peter van 't Hof committed
151
    db.close()
152
153
  }

154
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
155
  def testSettingKeys(): Unit = {
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    Await.result(db.createOrUpdateSetting(0, 0, Some(0), Some(0), Some(0),
      """
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
170
171
172
    db.getSettingKeys(0, 0, 0, 0, 0, keyValues = Map()) shouldBe Map()
    db.getSettingKeys(0, 0, 0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map("content" -> Some("test"))
    db.getSettingKeys(0, 0, 0, 0, 0, keyValues = Map("content" -> List("content2", "key"))) shouldBe Map("content" -> Some("value"))
173
174
175
176
177

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
178
  def testSettingsForSamples(): Unit = {
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val sampleId = Await.result(db.createSample("test_sample", 0), Duration.Inf)

    Await.result(db.createOrUpdateSetting(0, 0, Some(0), Some(sampleId), None,
      """
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
195
196
    db.getSettingsForSamples(0, 0, 0, keyValues = Map()) shouldBe Map(0 -> Map())
    db.getSettingsForSamples(0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map(0 -> Map("content" -> Some("test")))
197
198
199
200
201

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
202
  def testSettingsForLibraries(): Unit = {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val sampleId = Await.result(db.createSample("test_sample", 0), Duration.Inf)
    val libraryId = Await.result(db.createLibrary("test_library", 0, sampleId), Duration.Inf)

    Await.result(db.createOrUpdateSetting(0, 0, Some(0), Some(sampleId), Some(libraryId),
      """
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
220
221
    db.getSettingsForLibraries(0, 0, 0, keyValues = Map()) shouldBe Map((0, 0) -> Map())
    db.getSettingsForLibraries(0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map((0, 0) -> Map("content" -> Some("test")))
222
223
224
225

    db.close()
  }

226
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
227
  def testStats(): Unit = {
228
229
230
231
232
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

233
234
235
236
237
238
    val runId = Await.result(db.createRun("test", "", "", "", new Date(System.currentTimeMillis())), Duration.Inf)
    val pipelineId = Await.result(db.createPipeline("test_pipeline", runId), Duration.Inf)
    val moduleId = Await.result(db.createModule("test_module", runId, pipelineId), Duration.Inf)
    val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
    val libraryId = Await.result(db.createLibrary("test_library", runId, sampleId), Duration.Inf)

239
240
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 0

241
    Await.result(db.createOrUpdateStat(runId, pipelineId, None, None, None, """{"content": "test" }"""), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
242
    Await.result(db.getStat(runId, pipelineId, NoModule, NoSample, NoLibrary), Duration.Inf) shouldBe Some(Map("content" -> "test"))
243
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 1
244
    Await.result(db.createOrUpdateStat(runId, pipelineId, None, None, None, """{"content": "test2" }"""), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
245
    Await.result(db.getStat(runId, pipelineId, NoModule, NoSample, NoLibrary), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
246
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 1
247
248
249

    // Test join queries
    Await.result(db.createOrUpdateStat(runId, pipelineId, Some(moduleId), Some(sampleId), Some(libraryId), """{"content": "test3" }"""), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
250
    Await.result(db.getStat(runId, "test_pipeline", "test_module", "test_sample", "test_library"), Duration.Inf) shouldBe Some(Map("content" -> "test3"))
251
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 2
252

Peter van 't Hof's avatar
Peter van 't Hof committed
253
254
255
    db.close()
  }

256
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
257
  def testStatKeys(): Unit = {
258
259
260
261
262
263
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    Await.result(db.createOrUpdateStat(0, 0, Some(0), Some(0), Some(0),
Peter van 't Hof's avatar
Peter van 't Hof committed
264
      """
265
266
267
268
269
270
271
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
272
273
274
    db.getStatKeys(0, 0, 0, 0, 0, keyValues = Map()) shouldBe Map()
    db.getStatKeys(0, 0, 0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map("content" -> Some("test"))
    db.getStatKeys(0, 0, 0, 0, 0, keyValues = Map("content" -> List("content2", "key"))) shouldBe Map("content" -> Some("value"))
275
276
277
278
279

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
280
  def testStatsForSamples(): Unit = {
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val sampleId = Await.result(db.createSample("test_sample", 0), Duration.Inf)

    Await.result(db.createOrUpdateStat(0, 0, Some(0), Some(sampleId), None,
      """
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
297
298
    db.getStatsForSamples(0, 0, 0, keyValues = Map()) shouldBe Map(0 -> Map())
    db.getStatsForSamples(0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map(0 -> Map("content" -> Some("test")))
299
300
301
302
303

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
304
  def testStatsForLibraries(): Unit = {
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val sampleId = Await.result(db.createSample("test_sample", 0), Duration.Inf)
    val libraryId = Await.result(db.createLibrary("test_library", 0, sampleId), Duration.Inf)

    Await.result(db.createOrUpdateStat(0, 0, Some(0), Some(sampleId), Some(libraryId),
      """
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
322
323
    db.getStatsForLibraries(0, 0, 0, keyValues = Map()) shouldBe Map((0, 0) -> Map())
    db.getStatsForLibraries(0, 0, 0, keyValues = Map("content" -> List("content"))) shouldBe Map((0, 0) -> Map("content" -> Some("test")))
324
325
326
327

    db.close()
  }

328
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
329
  def testFiles(): Unit = {
330
331
332
333
334
335
336
337
338
339
340
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    val runId = Await.result(db.createRun("test", "", "", "", new Date(System.currentTimeMillis())), Duration.Inf)
    val pipelineId = Await.result(db.createPipeline("test_pipeline", runId), Duration.Inf)
    val moduleId = Await.result(db.createModule("test_module", runId, pipelineId), Duration.Inf)
    val sampleId = Await.result(db.createSample("test_sample", runId), Duration.Inf)
    val libraryId = Await.result(db.createLibrary("test_library", runId, sampleId), Duration.Inf)

Peter van 't Hof's avatar
Peter van 't Hof committed
341
    Await.result(db.createOrUpdateFile(runId, pipelineId, None, None, None, "key", "path", "md5", link = false, 1), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
342
    Await.result(db.getFile(runId, pipelineId, NoModule, NoSample, NoLibrary, "key"), Duration.Inf) shouldBe Some(Schema.File(0, 0, None, None, None, "key", "path", "md5", link = false, 1))
Peter van 't Hof's avatar
Peter van 't Hof committed
343
    Await.result(db.getFiles(), Duration.Inf) shouldBe Seq(Schema.File(0, 0, None, None, None, "key", "path", "md5", link = false, 1))
Peter van 't Hof's avatar
Peter van 't Hof committed
344
    Await.result(db.createOrUpdateFile(runId, pipelineId, None, None, None, "key", "path2", "md5", link = false, 1), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
345
    Await.result(db.getFile(runId, pipelineId, NoModule, NoSample, NoLibrary, "key"), Duration.Inf) shouldBe Some(Schema.File(0, 0, None, None, None, "key", "path2", "md5", link = false, 1))
Peter van 't Hof's avatar
Peter van 't Hof committed
346
    Await.result(db.getFiles(), Duration.Inf) shouldBe Seq(Schema.File(0, 0, None, None, None, "key", "path2", "md5", link = false, 1))
347
348

    // Test join queries
Peter van 't Hof's avatar
Peter van 't Hof committed
349
    Await.result(db.createOrUpdateFile(runId, pipelineId, Some(moduleId), Some(sampleId), Some(libraryId), "key", "path3", "md5", link = false, 1), Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
350
    Await.result(db.getFile(runId, "test_pipeline", "test_module", "test_sample", "test_library", "key"), Duration.Inf) shouldBe Some(Schema.File(0, 0, Some(moduleId), Some(sampleId), Some(libraryId), "key", "path3", "md5", link = false, 1))
Peter van 't Hof's avatar
Peter van 't Hof committed
351
    Await.result(db.getFiles(), Duration.Inf) shouldBe Seq(Schema.File(0, 0, None, None, None, "key", "path2", "md5", link = false, 1), Schema.File(0, 0, Some(moduleId), Some(sampleId), Some(libraryId), "key", "path3", "md5", link = false, 1))
352
353
354
355

    db.close()
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
356
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
357
  def testExecutable(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
358
359
360
361
362
363
364
365
366
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

    Await.result(db.createOrUpdateExecutable(0, "name"), Duration.Inf)
    Await.result(db.createOrUpdateExecutable(0, "name", Some("test")), Duration.Inf)
    Await.result(db.getExecutables(Some(0)), Duration.Inf).head shouldBe Schema.Executable(0, "name", Some("test"))
    db.close()
367
368
369
  }

}