Skip to content

Commit

Permalink
fix(cats): Fix gradle tests in cats (#4453)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert committed Mar 20, 2020
1 parent 384aa4b commit 07d27a6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
6 changes: 6 additions & 0 deletions cats/cats-sql/cats-sql.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ dependencies {
testImplementation "org.testcontainers:mysql"
testImplementation "mysql:mysql-connector-java"
}

test {
useJUnitPlatform {
includeEngines "junit-vintage", "junit-jupiter"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1431,10 +1431,10 @@ class SqlCache(
var relWhere: Condition = noCondition()

if (relationshipPrefixes.isNotEmpty() && !relationshipPrefixes.contains("ALL")) {
relWhere = field("rel_type").like(relationshipPrefixes[0])
relWhere = field("rel_type").like("${relationshipPrefixes[0]}%")

for (i in 1 until relationshipPrefixes.size) {
relWhere = relWhere.or(field("rel_type").like(relationshipPrefixes[i]))
relWhere = relWhere.or(field("rel_type").like("${relationshipPrefixes[i]}%"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.netflix.spinnaker.cats.sql.cache

import com.google.common.hash.Hashing
import com.netflix.spinnaker.config.SqlConstraints
import com.netflix.spinnaker.kork.annotations.VisibleForTesting

/**
* Provides utility methods for clouddriver's SQL naming conventions.
Expand Down Expand Up @@ -47,7 +48,8 @@ class SqlNames(
* It always keeps prefix with tableNamespace but can shorten name and suffix in that order.
* @return computed table name
*/
private fun checkTableName(prefix: String, name: String, suffix: String): String {
@VisibleForTesting
internal fun checkTableName(prefix: String, name: String, suffix: String): String {
var base = prefix
if (tableNamespace != null) {
base = "${prefix + tableNamespace}_"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.netflix.spinnaker.kork.sql.config.SqlRetryProperties
import com.netflix.spinnaker.kork.sql.test.SqlTestUtil
import com.zaxxer.hikari.HikariDataSource
import org.jooq.DSLContext
import org.jooq.impl.DSL
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Unroll
Expand Down Expand Up @@ -86,32 +87,15 @@ class SqlCacheSpec extends WriteableCacheSpec {
def where = ((SqlCache) cache).getRelWhere(relPrefixes, queryPrefix)

then:
where == expected
where.toString() == expected

where:
filter || queryPrefix || expected
RelationshipCacheFilter.none() || "meowdy=partner" || "meowdy=partner"
null || "meowdy=partner" || "meowdy=partner"
RelationshipCacheFilter.include("instances", "images") || null || "(rel_type LIKE 'instances%' OR rel_type LIKE 'images%')"
RelationshipCacheFilter.include("images") || "meowdy=partner" || "meowdy=partner AND (rel_type LIKE 'images%')"
null || null || "1=1"
}

@Unroll
def 'max length of table name is checked'() {
when:
def realName = ((SqlCache) cache).checkTableName("cats_v1_", name, suffix)

then:
realName == expected

where:
name || expected || suffix
"foo" || "cats_v1_test_foo" || ""
"abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghijabcdeaa7d0fee7e891a66" || ""
"abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghija9246690b33571ecc_rel" || "_rel"
"abcdefghij" * 10 || "cats_v1_test_abcdefghijabcdefghijabcdefghijabcdefe546a736182e553" || "suffix"*10

filter || queryPrefix || expected
RelationshipCacheFilter.none() || DSL.field("meowdy").eq("partner") || "meowdy = 'partner'"
null || DSL.field("meowdy").eq("partner") || "meowdy = 'partner'"
RelationshipCacheFilter.include("instances", "images") || null || "(\n rel_type like 'instances%'\n or rel_type like 'images%'\n)"
RelationshipCacheFilter.include("images") || DSL.field("meowdy").eq("partner") || "(\n meowdy = 'partner'\n and rel_type like 'images%'\n)"
null || null || "1 = 1"
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Netflix, Inc.
*
* Licensed 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 com.netflix.spinnaker.cats.sql.cache

import dev.minutest.junit.JUnit5Minutests
import dev.minutest.rootContext
import strikt.api.expectThat
import strikt.assertions.isEqualTo

class SqlNamesTest : JUnit5Minutests {

fun tests() = rootContext<SqlNames> {
fixture {
SqlNames()
}

listOf(
TableName("hello", "", "cats_v1_hello"),
TableName("hello", "world", "cats_v1_helloworld"),
TableName("abcdefghij".repeat(10), "", "cats_v1_abcdefghijabcdefghijabcdefghijabcdefghijaa7d0fee7e891a66"),
TableName("abcdefghij".repeat(10), "_rel", "cats_v1_abcdefghijabcdefghijabcdefghijabcdef9246690b33571ecc_rel"),
TableName("abcdefghij".repeat(10), "suffix".repeat(10), "cats_v1_abcdefghijabcdefghijabcdefghijabcdefghijfe546a736182e553")
).forEach { table ->
test("max length of table name is checked: $table") {
expectThat(checkTableName("cats_v1_", table.name, table.suffix))
.isEqualTo(table.expected)
}
}
}

private inner class TableName(
val name: String,
val suffix: String,
val expected: String
)
}

0 comments on commit 07d27a6

Please sign in to comment.