SummaryDbTest.scala 16.3 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
12

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

import scala.concurrent.Await
import scala.concurrent.duration.Duration

13
14
15
import scalaz._
import Scalaz._

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

22
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
23
  def testRuns(): Unit = {
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    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
41
  def testSamples(): Unit = {
42
43
44
45
46
47
48
49
50
51
    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)
52
    Await.result(db.createOrUpdateSample("test_sample", runId), Duration.Inf) shouldBe sampleId
53
54
55
56
    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
57
58
    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"))
59
60
61

    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"))
62
    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"}""")))
63
64
65
66
67

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
68
  def testLibraries(): Unit = {
69
70
71
72
73
74
75
76
77
78
79
    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)
80
    Await.result(db.createOrUpdateLibrary("test_lib", runId, sampleId), Duration.Inf) shouldBe libraryId
81
82
83
84
85
86
87
88
89
90
91
92
93
94

    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
95
  def testPipelines(): Unit = {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    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
    val pipelineId = Await.result(db.createPipeline("test", runId), Duration.Inf)
    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
115
  def testModules(): Unit = {
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    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
    val moduleId = Await.result(db.createModule("test", runId, pipelineId), Duration.Inf)
    Await.result(db.getmoduleId(runId, "test", pipelineId), Duration.Inf) shouldBe Some(moduleId)
    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()
  }

135
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
136
  def testSettings(): Unit = {
137
138
139
140
141
142
    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)
143
    Await.result(db.getSetting(0, 0.left, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
144
    Await.result(db.createOrUpdateSetting(0, 0, None, None, None, """{"content": "test2" }"""), Duration.Inf)
145
    Await.result(db.getSetting(0, 0.left, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
Peter van 't Hof's avatar
Peter van 't Hof committed
146
    db.close()
147
148
  }

149
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
150
  def testSettingKeys(): Unit = {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    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)

165
166
167
    db.getSettingKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map()) shouldBe Map()
    db.getSettingKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map("content" -> Some("test"))
    db.getSettingKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map("content" -> List("content2", "key"))) shouldBe Map("content" -> Some("value"))
168
169
170
171
172

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
173
  def testSettingsForSamples(): Unit = {
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    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)

190
191
    db.getSettingsForSamples(0, 0.left, Some(0.left), keyValues = Map()) shouldBe Map(0 -> Map())
    db.getSettingsForSamples(0, 0.left, Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map(0 -> Map("content" -> Some("test")))
192
193
194
195
196

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
197
  def testSettingsForLibraries(): Unit = {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    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)

215
216
    db.getSettingsForLibraries(0, 0.left, Some(0.left), keyValues = Map()) shouldBe Map((0, 0) -> Map())
    db.getSettingsForLibraries(0, 0.left, Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map((0, 0) -> Map("content" -> Some("test")))
217
218
219
220

    db.close()
  }

221
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
222
  def testStats(): Unit = {
223
224
225
226
227
    val dbFile = File.createTempFile("summary.", ".db")
    dbFile.deleteOnExit()
    val db = SummaryDb.openSqliteSummary(dbFile)
    db.createTables()

228
229
230
231
232
233
    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)

234
235
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 0

236
    Await.result(db.createOrUpdateStat(runId, pipelineId, None, None, None, """{"content": "test" }"""), Duration.Inf)
237
    Await.result(db.getStat(runId, pipelineId.left, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test"))
238
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 1
239
    Await.result(db.createOrUpdateStat(runId, pipelineId, None, None, None, """{"content": "test2" }"""), Duration.Inf)
240
    Await.result(db.getStat(runId, pipelineId.left, None, None, None), Duration.Inf) shouldBe Some(Map("content" -> "test2"))
241
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 1
242
243
244

    // Test join queries
    Await.result(db.createOrUpdateStat(runId, pipelineId, Some(moduleId), Some(sampleId), Some(libraryId), """{"content": "test3" }"""), Duration.Inf)
245
    Await.result(db.getStat(runId, "test_pipeline".right, Some("test_module".right), Some("test_sample".right), Some("test_library".right)), Duration.Inf) shouldBe Some(Map("content" -> "test3"))
246
    Await.result(db.getStatsSize(), Duration.Inf) shouldBe 2
247

Peter van 't Hof's avatar
Peter van 't Hof committed
248
249
250
    db.close()
  }

251
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
252
  def testStatKeys(): Unit = {
253
254
255
256
257
258
    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
259
      """
260
261
262
263
264
265
266
        |{
        |"content": "test",
        |"content2": {
        |  "key": "value"
        |}
        | }""".stripMargin), Duration.Inf)

267
268
269
    db.getStatKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map()) shouldBe Map()
    db.getStatKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map("content" -> Some("test"))
    db.getStatKeys(0, 0.left, Some(0.left), Some(0.left), Some(0.left), keyValues = Map("content" -> List("content2", "key"))) shouldBe Map("content" -> Some("value"))
270
271
272
273
274

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
275
  def testStatsForSamples(): Unit = {
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    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)

292
293
    db.getStatsForSamples(0, 0.left, Some(0.left), keyValues = Map()) shouldBe Map(0 -> Map())
    db.getStatsForSamples(0, 0.left, Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map(0 -> Map("content" -> Some("test")))
294
295
296
297
298

    db.close()
  }

  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
299
  def testStatsForLibraries(): Unit = {
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    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)

317
318
    db.getStatsForLibraries(0, 0.left, Some(0.left), keyValues = Map()) shouldBe Map((0, 0) -> Map())
    db.getStatsForLibraries(0, 0.left, Some(0.left), keyValues = Map("content" -> List("content"))) shouldBe Map((0, 0) -> Map("content" -> Some("test")))
319
320
321
322

    db.close()
  }

323
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
324
  def testFiles(): Unit = {
325
326
327
328
329
330
331
332
333
334
335
    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
336
    Await.result(db.createOrUpdateFile(runId, pipelineId, None, None, None, "key", "path", "md5", link = false, 1), Duration.Inf)
337
    Await.result(db.getFile(runId, pipelineId.left, None, None, None, "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
338
    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
339
    Await.result(db.createOrUpdateFile(runId, pipelineId, None, None, None, "key", "path2", "md5", link = false, 1), Duration.Inf)
340
    Await.result(db.getFile(runId, pipelineId.left, None, None, None, "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
341
    Await.result(db.getFiles(), Duration.Inf) shouldBe Seq(Schema.File(0, 0, None, None, None, "key", "path2", "md5", link = false, 1))
342
343

    // Test join queries
Peter van 't Hof's avatar
Peter van 't Hof committed
344
    Await.result(db.createOrUpdateFile(runId, pipelineId, Some(moduleId), Some(sampleId), Some(libraryId), "key", "path3", "md5", link = false, 1), Duration.Inf)
345
    Await.result(db.getFile(runId, "test_pipeline".right, Some("test_module".right), Some("test_sample".right), Some("test_library".right), "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
346
    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))
347
348
349
350

    db.close()
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
351
  @Test
Peter van 't Hof's avatar
Peter van 't Hof committed
352
  def testExecutable(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
353
354
355
356
357
358
359
360
361
    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()
362
363
364
  }

}