Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,27 @@ package org.apache.texera.web

import org.apache.texera.dao.MockTexeraDB
import org.apache.texera.dao.jooq.generated.Tables._
import org.apache.texera.dao.jooq.generated.enums.{UserRoleEnum, WorkflowComputingUnitTypeEnum}
import org.apache.texera.dao.jooq.generated.enums.{
PrivilegeEnum,
UserRoleEnum,
WorkflowComputingUnitTypeEnum
}
import org.apache.texera.dao.jooq.generated.tables.daos.{
UserDao,
WorkflowComputingUnitDao,
WorkflowDao,
WorkflowExecutionsDao,
WorkflowOfUserDao,
WorkflowUserAccessDao,
WorkflowVersionDao
}
import org.apache.texera.dao.jooq.generated.tables.pojos.{
User,
Workflow,
WorkflowComputingUnit,
WorkflowExecutions,
WorkflowOfUser,
WorkflowUserAccess,
WorkflowVersion
}

Expand Down Expand Up @@ -83,6 +91,13 @@ trait ExecutionDatabaseTestSupport { this: MockTexeraDB =>
workflow
}

protected def insertWorkflowOfUser(uid: Int, wid: Int): Unit =
new WorkflowOfUserDao(getDSLContext.configuration()).insert(new WorkflowOfUser(uid, wid))

protected def insertWorkflowUserAccess(uid: Int, wid: Int, privilege: PrivilegeEnum): Unit =
new WorkflowUserAccessDao(getDSLContext.configuration())
.insert(new WorkflowUserAccess(uid, wid, privilege))

protected def insertWorkflowVersion(
wid: Int,
vidOpt: Option[Int] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.apache.texera.web.resource

import org.apache.texera.amber.util.JSONUtils
import org.apache.texera.dao.MockTexeraDB
import org.apache.texera.dao.DatabaseFlatSpec
import org.apache.texera.dao.jooq.generated.Tables.{USER, WORKFLOW, WORKFLOW_USER_ACCESS}
import org.apache.texera.dao.jooq.generated.enums.{PrivilegeEnum, UserRoleEnum}
import org.apache.texera.dao.jooq.generated.tables.daos.{
Expand All @@ -32,9 +32,6 @@ import org.apache.texera.dao.jooq.generated.tables.pojos.{User, Workflow, Workfl
import org.apache.texera.web.model.collab.request._
import org.apache.texera.web.resource.CollaborationResource._
import org.scalamock.scalatest.MockFactory
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import java.sql.Timestamp
import java.util.concurrent.{Future => JFuture}
Expand All @@ -59,13 +56,7 @@ import scala.collection.mutable.ArrayBuffer
// throws where TryLockRequest copes. Case (2) predates this spec and is pinned
// by "AcquireLockRequest should rethrow when the holder slot holds the null
// sentinel" as current behaviour, not as desired behaviour.
class CollaborationResourceSpec
extends AnyFlatSpec
with Matchers
with MockFactory
with BeforeAndAfterAll
with BeforeAndAfterEach
with MockTexeraDB {
class CollaborationResourceSpec extends DatabaseFlatSpec(cleanupAfterEach = true) with MockFactory {

private var resource: CollaborationResource = _

Expand All @@ -79,31 +70,18 @@ class CollaborationResourceSpec
// session: with one uid the privilege predicate cannot be observed at all.
private val readOnlyUid = 8202

override protected def beforeAll(): Unit = {
initializeDBAndReplaceDSLContext()
}

override protected def afterAll(): Unit = {
closeConnectionPool()
}

// The five maps live on the companion object, i.e. they are JVM-wide mutable
// state shared by every test in the suite. Clearing them here is what keeps
// this spec order-independent.
override def beforeEach(): Unit = {
override protected def onBeforeEach(): Unit = {
sessionIdSessionMap.clear()
sessionIdWIdMap.clear()
sessionIdUIdMap.clear()
wIdSessionIdsMap.clear()
wIdLockHolderSessionIdMap.clear()
cleanupAccess()
resource = new CollaborationResource()
}

override def afterEach(): Unit = {
cleanupAccess()
}

// Seeds user accessUid + workflow accessWid + a workflow_user_access row with
// the given privilege, so WorkflowAccess.getPrivilege(accessWid,
// accessUid) returns it.
Expand Down Expand Up @@ -135,7 +113,7 @@ class CollaborationResourceSpec
)
}

private def cleanupAccess(): Unit = {
override protected def cleanup(): Unit = {
getDSLContext
.deleteFrom(WORKFLOW_USER_ACCESS)
.where(WORKFLOW_USER_ACCESS.WID.eq(accessWid))
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

package org.apache.texera.web.resource.dashboard

import org.apache.texera.dao.MockTexeraDB
import org.apache.texera.dao.DatabaseFlatSpec
import org.apache.texera.web.resource.dashboard.VersionedSearchQuerySupport.{
assertProjection,
params,
renderSearch
}
import org.apache.texera.web.resource.dashboard.DashboardResource.SearchQueryParams
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.jdk.CollectionConverters._

Expand All @@ -38,50 +40,21 @@ import scala.jdk.CollectionConverters._
* `FulltextSearchQueryUtils` builds before its pgroonga/tsvector branch — an assertion on either
* arm would depend on which suites ran earlier in the JVM.
*/
class ModelSearchQueryBuilderSpec
extends AnyFlatSpec
with Matchers
with BeforeAndAfterAll
with MockTexeraDB {
class ModelSearchQueryBuilderSpec extends DatabaseFlatSpec(cleanupBeforeEach = false) {

private val callerUid: Integer = Integer.valueOf(9201)

override protected def beforeAll(): Unit = {
initializeDBAndReplaceDSLContext()
}

override protected def afterAll(): Unit = {
shutdownDB()
}

private def params(keywords: String*): SearchQueryParams =
SearchQueryParams(keywords = keywords.toList.asJava)

private def sqlFor(
uid: Integer,
includePublic: Boolean,
p: SearchQueryParams = params()
): String =
getDSLContext
.renderInlined(ModelSearchQueryBuilder.constructQuery(uid, p, includePublic))
.toLowerCase
.replace("\"", "")
renderSearch(getDSLContext, ModelSearchQueryBuilder, uid, p, includePublic)

"the projection" should "carry every model column under the alias its schema slot names" in {
val sql = sqlFor(callerUid, includePublic = true)

sql should include("'model' as resourcetype")
sql should include("model.name as resourcename")
sql should include("model.description as resourcedescription")
sql should include("model.creation_time as resourcecreationtime")
sql should include("model.owner_uid as resourceownerid")
sql should include("model.repository_name as repository_name")
// Shared with the other versioned resources, hence the resource-neutral aliases.
sql should include("model.mid as versioned_resource_id")
sql should include("model.is_public as is_versioned_resource_public")
sql should include("model.is_downloadable as is_versioned_resource_downloadable")
sql should include("model_user_access.privilege as user_versioned_resource_access")
sql should include("model.cover_image as versioned_resource_cover_image")
assertProjection(sql, "model", "mid")
}

it should "carry framework and format, which no other resource projects" in {
Expand All @@ -92,7 +65,7 @@ class ModelSearchQueryBuilderSpec
}

it should "leave the workflow and dataset-only slots null" in {
// The union requires all builders to project the same 23 columns in the same order.
// The union requires all builders to project the same columns in the same order.
val sql = sqlFor(callerUid, includePublic = true)

sql should include("cast(null as int) as wid")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.texera.web.resource.dashboard

import org.apache.texera.dao.MockTexeraDB
import org.apache.texera.dao.jooq.generated.tables.daos._
import org.apache.texera.dao.jooq.generated.tables.pojos._
import org.apache.texera.web.ExecutionDatabaseTestSupport
import org.jooq.DSLContext

import java.sql.Timestamp
import java.util.UUID

private[dashboard] trait DashboardWorkflowFixtureSupport extends ExecutionDatabaseTestSupport {
self: MockTexeraDB =>
protected lazy val workflowDao = new WorkflowDao(getDSLContext.configuration())

protected def uniqueSuffix: String = UUID.randomUUID().toString.substring(0, 8)
protected def now: Timestamp = new Timestamp(System.currentTimeMillis())

protected def fixtureUser(uid: Int, name: String): User = {
val user = new User
user.setUid(uid)
user.setName(name)
user.setEmail(s"$name@test.com")
user
}

protected def fixtureWorkflow(
wid: Int,
name: String,
description: String,
isPublic: java.lang.Boolean
): Workflow = {
val workflow = new Workflow
workflow.setWid(wid)
workflow.setName(name)
workflow.setContent("{}")
workflow.setDescription(description)
workflow.setIsPublic(isPublic)
workflow.setCreationTime(now)
workflow.setLastModifiedTime(now)
workflow
}
}

private[dashboard] trait UserDashboardFixtureSupport extends DashboardWorkflowFixtureSupport {
Comment thread
aglinxinyuan marked this conversation as resolved.
Resolved
self: MockTexeraDB =>
protected lazy val userDao = new UserDao(getDSLContext.configuration())
private lazy val datasetDao = new DatasetDao(getDSLContext.configuration())

protected def seedDataset(uid: Int, namePrefix: String): Dataset = {
val dataset = new Dataset
dataset.setOwnerUid(uid)
dataset.setName(namePrefix + uniqueSuffix)
dataset.setRepositoryName("repo-" + uniqueSuffix)
dataset.setIsPublic(false)
dataset.setIsDownloadable(true)
dataset.setDescription("")
dataset.setCreationTime(now)
datasetDao.insert(dataset)
dataset
}
}

/** Creates the accounts used by warehouse endpoint and workflow selection tests. */
private[web] object WarehouseAccountFixture {
def seed(context: DSLContext, name: String, avatar: Option[String] = None): User = {
val user = new User
user.setName(name)
user.setEmail(s"user_${UUID.randomUUID()}@example.com")
avatar.foreach(user.setAvatar)
new UserDao(context.configuration()).insert(user)
user
}
}
Loading
Loading