|
| 1 | +/* |
| 2 | + * Copyright 2025 RAW Labs S.A. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file licenses/BSL.txt. |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0, included in the file |
| 10 | + * licenses/APL.txt. |
| 11 | + */ |
| 12 | + |
| 13 | +package com.rawlabs.das.sqlite |
| 14 | + |
| 15 | +import scala.jdk.CollectionConverters._ |
| 16 | + |
| 17 | +import org.scalatest.BeforeAndAfterAll |
| 18 | +import org.scalatest.funsuite.AnyFunSuite |
| 19 | + |
| 20 | +import com.rawlabs.protocol.das.v1.tables.{Column, Row} |
| 21 | +import com.rawlabs.protocol.das.v1.types.{Value, ValueDouble, ValueInt, ValueString} |
| 22 | +import com.typesafe.scalalogging.StrictLogging |
| 23 | + |
| 24 | +class DASSqliteSimpleTest extends AnyFunSuite with BeforeAndAfterAll with StrictLogging { |
| 25 | + |
| 26 | + test("read mydb file") { |
| 27 | + val resourceUrl = getClass.getResource("/mydb") |
| 28 | + val file = new java.io.File(resourceUrl.toURI) |
| 29 | + val fullPath = file.getAbsolutePath |
| 30 | + |
| 31 | + val sdk = new DASSqlite(Map("database" -> fullPath)) |
| 32 | + val defs = sdk.tableDefinitions |
| 33 | + assert(defs.nonEmpty, "tableDefinitions should not be empty.") |
| 34 | + val names = defs.map(_.getTableId.getName) |
| 35 | + assert(names.contains("COMPANY"), "We expect 'COMPANY' table in the DB.") |
| 36 | + |
| 37 | + val columns = defs.find(_.getTableId.getName == "COMPANY").get.getColumnsList.asScala |
| 38 | + assert(columns.map(_.getName) == Seq("ID", "NAME", "AGE", "ADDRESS", "SALARY"), "Columns should match.") |
| 39 | + |
| 40 | + val rs = |
| 41 | + sdk.getTable("COMPANY").get.execute(Seq.empty, Seq("ID", "NAME", "AGE", "ADDRESS", "SALARY"), Seq.empty, None) |
| 42 | + val buf = scala.collection.mutable.ListBuffer[Row]() |
| 43 | + while (rs.hasNext) { |
| 44 | + buf += rs.next() |
| 45 | + } |
| 46 | + rs.close() |
| 47 | + |
| 48 | + assert( |
| 49 | + buf.toList == List( |
| 50 | + buildMyDbRow(1, "Paul", 32, "California", 20000.0), |
| 51 | + buildMyDbRow(2, "Allen", 25, "Texas", 15000.0), |
| 52 | + buildMyDbRow(3, "Teddy", 23, "Norway", 20000.0), |
| 53 | + buildMyDbRow(4, "Mark", 25, "Rich-Mond ", 65000.0), |
| 54 | + buildMyDbRow(5, "David", 27, "Texas", 85000.0), |
| 55 | + buildMyDbRow(6, "Kim", 22, "South-Hall", 45000.0))) |
| 56 | + |
| 57 | + sdk.close() |
| 58 | + } |
| 59 | + |
| 60 | + private def buildMyDbRow(id: Int, name: String, age: Int, address: String, salary: Double): Row = { |
| 61 | + val row = Row.newBuilder() |
| 62 | + row.addColumns(Column.newBuilder().setName("ID").setData(Value.newBuilder().setInt(ValueInt.newBuilder().setV(id)))) |
| 63 | + row.addColumns( |
| 64 | + Column.newBuilder().setName("NAME").setData(Value.newBuilder().setString(ValueString.newBuilder().setV(name)))) |
| 65 | + row.addColumns( |
| 66 | + Column.newBuilder().setName("AGE").setData(Value.newBuilder().setInt(ValueInt.newBuilder().setV(age)))) |
| 67 | + row.addColumns( |
| 68 | + Column |
| 69 | + .newBuilder() |
| 70 | + .setName("ADDRESS") |
| 71 | + .setData(Value.newBuilder().setString(ValueString.newBuilder().setV(address)))) |
| 72 | + row.addColumns( |
| 73 | + Column |
| 74 | + .newBuilder() |
| 75 | + .setName("SALARY") |
| 76 | + .setData(Value.newBuilder().setDouble(ValueDouble.newBuilder().setV(salary)))) |
| 77 | + row.build() |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments