Skip to content

Commit

Permalink
fix(jdbc): summary count should be a prefix on the namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Jan 16, 2025
1 parent d3efcdd commit 9a19c01
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import lombok.Getter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand Down Expand Up @@ -623,6 +624,57 @@ void findByExecutionNoRevision() {
}
}

@Test
void shouldCountForNullTenant() {
FlowWithSource toDelete = null;
try {
// Given
Flow flow = createTestFlowForNamespace("io.kestra.unittest");
toDelete = flowRepository.create(flow, "", flow);
// When
int count = flowRepository.count(null);

// Then
Assertions.assertTrue(count > 0);
} finally {
Optional.ofNullable(toDelete).ifPresent(flow -> {
flowRepository.delete(flow);
});
}
}

@Test
void shouldCountForNullTenantGivenNamespace() {
List<FlowWithSource> toDelete = new ArrayList<>();
try {
toDelete.add(flowRepository.create(createTestFlowForNamespace("io.kestra.unittest.sub"), "", createTestFlowForNamespace("io.kestra.unittest.sub")));
toDelete.add(flowRepository.create(createTestFlowForNamespace("io.kestra.unittest.shouldcountbynamespacefornulltenant"), "", createTestFlowForNamespace("io.kestra.unittest.shouldcountbynamespacefornulltenant")));
toDelete.add(flowRepository.create(createTestFlowForNamespace("com.kestra.unittest"), "", createTestFlowForNamespace("com.kestra.unittest")));

int count = flowRepository.countForNamespace(null, "io.kestra.unittest.shouldcountbynamespacefornulltenant");
assertThat(count, is(1));

count = flowRepository.countForNamespace(null, "io.kestra.unittest");
assertThat(count, is(2));
} finally {
for (FlowWithSource flow : toDelete) {
flowRepository.delete(flow);
}
}
}

private static Flow createTestFlowForNamespace(String namespace) {
return Flow.builder()
.id(IdUtils.create())
.namespace(namespace)
.tasks(List.of(Return.builder()
.id(IdUtils.create())
.type(Return.class.getName())
.build()
))
.build();
}

private void deleteFlow(Flow flow) {
Integer revision = flowRepository.lastRevision(flow.getTenantId(), flow.getNamespace(), flow.getId());
flowRepository.delete(flow.toBuilder().revision(revision).build().withSource(flow.generateSource()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,55 @@ void all() {
assertThat(find.size(), is(1));
assertThat(find.getFirst().getTriggerId(), is(searchedTrigger.getTriggerId()));
}

@Test
void shouldCountForNullTenant() {
// Given
triggerRepository.save(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("io.kestra.unittest")
.build()
);
// When
int count = triggerRepository.count(null);
// Then
assertThat(count, is(1));
}

@Test
void shouldCountForNullTenantGivenNamespace() {
// Given
triggerRepository.save(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("io.kestra.unittest.p2")
.build()
);

triggerRepository.save(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("io.kestra.unittest.shouldcountbynamespacefornulltenant")
.build()
);

triggerRepository.save(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("com.kestra.unittest")
.build()
);

// When
int count = triggerRepository.countForNamespace(null, "io.kestra.unittest.shouldcountbynamespacefornulltenant");
assertThat(count, is(1));

count = triggerRepository.countForNamespace(null, "io.kestra.unittest");
assertThat(count, is(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,10 @@ public Map<String, ExecutionCountStatistics> executionCountsGroupedByNamespace(
.and(START_DATE_FIELD.lessOrEqual(finalEndDate.toOffsetDateTime()));

if (namespace != null) {
selectCount = selectCount.and(NAMESPACE_FIELD.eq(namespace));
selectCount = selectCount.and(DSL.or(
NAMESPACE_FIELD.likeIgnoreCase(namespace + ".%"),
NAMESPACE_FIELD.eq(namespace)
));
}

Map<String, Result<Record3<String, String, Long>>> resultByNamespace = selectCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ public int countForNamespace(String tenantId, @Nullable String namespace) {
.selectCount()
.from(fromLastRevision(true))
.where(this.defaultFilter(tenantId))
.and(NAMESPACE_FIELD.eq(namespace))
.and(DSL.or(
NAMESPACE_FIELD.likeIgnoreCase(namespace + ".%"),
NAMESPACE_FIELD.eq(namespace)
))
.fetchOne(0, int.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public int countForNamespace(@Nullable String tenantId, @Nullable String namespa
.selectCount()
.from(this.jdbcRepository.getTable())
.where(this.defaultFilter(tenantId))
.and(NAMESPACE_FIELD.eq(namespace))
.and(DSL.or(
NAMESPACE_FIELD.likeIgnoreCase(namespace + ".%"),
NAMESPACE_FIELD.eq(namespace)
))
.fetchOne(0, int.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,20 @@
import io.kestra.core.models.flows.FlowWithException;
import io.kestra.core.models.flows.FlowWithSource;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.core.utils.IdUtils;
import io.kestra.jdbc.JdbcTestUtils;
import io.kestra.jdbc.JooqDSLContextWrapper;
import io.kestra.plugin.core.debug.Return;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.Sort;
import jakarta.inject.Inject;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import jakarta.inject.Inject;

import static io.kestra.jdbc.repository.AbstractJdbcRepository.field;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -92,55 +87,4 @@ protected void setup() {
jdbcTestUtils.drop();
jdbcTestUtils.migrate();
}

@Test
void shouldCountForNullTenant() {
FlowWithSource toDelete = null;
try {
// Given
Flow flow = createTestFlowForNamespace("io.kestra.unittest");
toDelete = flowRepository.create(flow, "", flow);
// When
int count = flowRepository.count(null);

// Then
Assertions.assertTrue(count > 0);
} finally {
Optional.ofNullable(toDelete).ifPresent(flow -> {
flowRepository.delete(flow);
});
}
}

@Test
void shouldCountForNullTenantGivenNamespace() {
List<FlowWithSource> toDelete = new ArrayList<>();
try {
// Given
toDelete.add(flowRepository.create(createTestFlowForNamespace("io.kestra.unittest"), "", createTestFlowForNamespace("io.kestra.unittest")));
toDelete.add(flowRepository.create(createTestFlowForNamespace("io.kestra.unittest.shouldcountbynamespacefornulltenant"), "", createTestFlowForNamespace("io.kestra.unittest.shouldcountbynamespacefornulltenant")));

// When
int count = flowRepository.countForNamespace(null, "io.kestra.unittest.shouldcountbynamespacefornulltenant");

// Then
Assertions.assertEquals(1, count);
} finally {
for (FlowWithSource flow : toDelete) {
flowRepository.delete(flow);
}
}
}

private static Flow createTestFlowForNamespace(String namespace) {
return Flow.builder()
.id(IdUtils.create())
.namespace(namespace)
.tasks(List.of(Return.builder()
.id(IdUtils.create())
.type(Return.class.getName())
.build()
))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,4 @@ protected void init() {
jdbcTestUtils.drop();
jdbcTestUtils.migrate();
}

@Test
void shouldCountForNullTenant() {
// Given
repository.create(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("io.kestra.unittest")
.build()
);
// When
int count = repository.count(null);
// Then
Assertions.assertEquals(1, count);
}

@Test
void shouldCountForNullTenantGivenNamespace() {
// Given
repository.create(Trigger
.builder()
.triggerId(IdUtils.create())
.flowId(IdUtils.create())
.namespace("io.kestra.unittest")
.namespace("io.kestra.unittest.shouldcountbynamespacefornulltenant")
.build()
);
// When
int count = repository.countForNamespace(null, "io.kestra.unittest.shouldcountbynamespacefornulltenant");
// Then
Assertions.assertEquals(1, count);

}
}

0 comments on commit 9a19c01

Please sign in to comment.