Fix #7104: Add support for get resource permission by FQN (#7270)

This commit is contained in:
Sriharsha Chintalapani 2022-09-06 12:30:52 -07:00 committed by GitHub
parent e097e18a9b
commit d29a230726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 512 deletions

View File

@ -64,7 +64,6 @@ import org.openmetadata.catalog.resources.databases.TableResource;
import org.openmetadata.catalog.tests.ColumnTest; import org.openmetadata.catalog.tests.ColumnTest;
import org.openmetadata.catalog.tests.CustomMetric; import org.openmetadata.catalog.tests.CustomMetric;
import org.openmetadata.catalog.tests.TableTest; import org.openmetadata.catalog.tests.TableTest;
import org.openmetadata.catalog.tests.type.TestCaseResult;
import org.openmetadata.catalog.type.Column; import org.openmetadata.catalog.type.Column;
import org.openmetadata.catalog.type.ColumnJoin; import org.openmetadata.catalog.type.ColumnJoin;
import org.openmetadata.catalog.type.ColumnProfile; import org.openmetadata.catalog.type.ColumnProfile;
@ -397,152 +396,6 @@ public class TableRepository extends EntityRepository<Table> {
return table.withTableQueries(getQueries(table)); return table.withTableQueries(getQueries(table));
} }
@Transaction
public Table addTableTest(UUID tableId, TableTest tableTest) throws IOException {
// Validate the request content
Table table = dao.findEntityById(tableId);
// if ID is not passed we treat it as a new test case being added
List<TableTest> storedTableTests = getTableTests(table);
// we will override any test case name passed by user/client with tableName + testType
// our assumption is there is only one instance of a test type as of now.
tableTest.setName(table.getName() + "." + tableTest.getTestCase().getTableTestType().toString());
Map<String, TableTest> storedMapTableTests = new HashMap<>();
if (storedTableTests != null) {
for (TableTest t : storedTableTests) {
storedMapTableTests.put(t.getName(), t);
}
}
// existing test, use the previous UUID
if (storedMapTableTests.containsKey(tableTest.getName())) {
TableTest prevTableTest = storedMapTableTests.get(tableTest.getName());
tableTest.setId(prevTableTest.getId());
// process test result
if (!nullOrEmpty(tableTest.getResults())) {
List<TestCaseResult> prevTestCaseResults = prevTableTest.getResults();
prevTestCaseResults.addAll(tableTest.getResults());
tableTest.setResults(prevTestCaseResults);
}
}
storedMapTableTests.put(tableTest.getName(), tableTest);
List<TableTest> updatedTests = new ArrayList<>(storedMapTableTests.values());
daoCollection
.entityExtensionDAO()
.insert(tableId.toString(), "table.tableTests", "tableTest", JsonUtils.pojoToJson(updatedTests));
setFields(table, Fields.EMPTY_FIELDS);
// return the only test instead of querying all tests and results
return table.withTableTests(List.of(tableTest));
}
@Transaction
public Table deleteTableTest(UUID tableId, String tableTestType) throws IOException {
// Validate the request content
Table table = dao.findEntityById(tableId);
// if ID is not passed we treat it as a new test case being added
List<TableTest> storedTableTests = getTableTests(table);
// we will override any test case name passed by user/client with tableName + testType
// our assumption is there is only one instance of a test type as of now.
String tableTestName = table.getName() + "." + tableTestType;
Map<String, TableTest> storedMapTableTests = new HashMap<>();
if (storedTableTests != null) {
for (TableTest t : storedTableTests) {
storedMapTableTests.put(t.getName(), t);
}
}
if (!storedMapTableTests.containsKey(tableTestName)) {
throw new EntityNotFoundException(String.format("Failed to find %s for %s", tableTestName, table.getName()));
}
TableTest deleteTableTest = storedMapTableTests.get(tableTestName);
storedMapTableTests.remove(tableTestName);
List<TableTest> updatedTests = new ArrayList<>(storedMapTableTests.values());
daoCollection
.entityExtensionDAO()
.insert(tableId.toString(), "table.tableTests", "tableTest", JsonUtils.pojoToJson(updatedTests));
return table.withTableTests(List.of(deleteTableTest));
}
@Transaction
public Table addColumnTest(UUID tableId, ColumnTest columnTest) throws IOException {
// Validate the request content
Table table = dao.findEntityById(tableId);
String columnName = columnTest.getColumnName();
validateColumn(table, columnName);
// we will override any test case name passed by user/client with columnName + testType
// our assumption is there is only one instance of a test type as of now.
columnTest.setName(columnName + "." + columnTest.getTestCase().getColumnTestType().toString());
List<ColumnTest> storedColumnTests = getColumnTests(table, columnName);
Map<String, ColumnTest> storedMapColumnTests = new HashMap<>();
if (storedColumnTests != null) {
for (ColumnTest ct : storedColumnTests) {
storedMapColumnTests.put(ct.getName(), ct);
}
}
// existingTest use the previous UUID
if (storedMapColumnTests.containsKey(columnTest.getName())) {
ColumnTest prevColumnTest = storedMapColumnTests.get(columnTest.getName());
columnTest.setId(prevColumnTest.getId());
// process test results
if (!nullOrEmpty(columnTest.getResults())) {
List<TestCaseResult> prevTestCaseResults = prevColumnTest.getResults();
prevTestCaseResults.addAll(columnTest.getResults());
columnTest.setResults(prevTestCaseResults);
}
}
storedMapColumnTests.put(columnTest.getName(), columnTest);
List<ColumnTest> updatedTests = new ArrayList<>(storedMapColumnTests.values());
String extension = TABLE_COLUMN_EXTENSION + columnName + ".tests";
daoCollection
.entityExtensionDAO()
.insert(table.getId().toString(), extension, "columnTest", JsonUtils.pojoToJson(updatedTests));
setFields(table, Fields.EMPTY_FIELDS);
// return the newly created/updated column test only
for (Column column : table.getColumns()) {
if (column.getName().equals(columnName)) {
column.setColumnTests(List.of(columnTest));
}
}
return table;
}
@Transaction
public Table deleteColumnTest(UUID tableId, String columnName, String columnTestType) throws IOException {
// Validate the request content
Table table = dao.findEntityById(tableId);
validateColumn(table, columnName);
// we will override any test case name passed by user/client with columnName + testType
// our assumption is there is only one instance of a test type as of now.
String columnTestName = columnName + "." + columnTestType;
List<ColumnTest> storedColumnTests = getColumnTests(table, columnName);
Map<String, ColumnTest> storedMapColumnTests = new HashMap<>();
if (storedColumnTests != null) {
for (ColumnTest ct : storedColumnTests) {
storedMapColumnTests.put(ct.getName(), ct);
}
}
if (!storedMapColumnTests.containsKey(columnTestName)) {
throw new EntityNotFoundException(String.format("Failed to find %s for %s", columnTestName, table.getName()));
}
ColumnTest deleteColumnTest = storedMapColumnTests.get(columnTestName);
storedMapColumnTests.remove(columnTestName);
List<ColumnTest> updatedTests = new ArrayList<>(storedMapColumnTests.values());
String extension = TABLE_COLUMN_EXTENSION + columnName + ".tests";
daoCollection
.entityExtensionDAO()
.insert(table.getId().toString(), extension, "columnTest", JsonUtils.pojoToJson(updatedTests));
// return the newly created/updated column test only
for (Column column : table.getColumns()) {
if (column.getName().equals(columnName)) {
column.setColumnTests(List.of(deleteColumnTest));
}
}
return table;
}
@Transaction @Transaction
public Table addCustomMetric(UUID tableId, CustomMetric customMetric) throws IOException { public Table addCustomMetric(UUID tableId, CustomMetric customMetric) throws IOException {
// Validate the request content // Validate the request content

View File

@ -811,81 +811,6 @@ public class TableResource extends EntityResource<Table, TableRepository> {
return addHref(uriInfo, table); return addHref(uriInfo, table);
} }
@PUT
@Path("/{id}/tableTest")
@Operation(
operationId = "addTableTest",
summary = "Add table test cases",
tags = "tables",
description = "Add test cases to the table.",
responses = {
@ApiResponse(
responseCode = "200",
description = "OK",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Table.class)))
})
public Table addTableTest(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the table", schema = @Schema(type = "UUID")) @PathParam("id") UUID id,
@Valid CreateTableTest createTableTest)
throws IOException {
authorizer.authorizeAdmin(securityContext, true);
TableTest tableTest = getTableTest(securityContext, createTableTest);
Table table = dao.addTableTest(id, tableTest);
return addHref(uriInfo, table);
}
@DELETE
@Path("/{id}/tableTest/{tableTestType}")
@Operation(
operationId = "deleteTableTest",
summary = "delete table test case",
tags = "tables",
description = "Delete test case from the table.",
responses = {
@ApiResponse(
responseCode = "200",
description = "OK",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Table.class)))
})
public Table deleteTableTest(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the table", schema = @Schema(type = "UUID")) @PathParam("id") UUID id,
@Parameter(description = "Table Test Type", schema = @Schema(type = "string")) @PathParam("tableTestType")
String tableTestType)
throws IOException {
authorizer.authorizeAdmin(securityContext, true);
Table table = dao.deleteTableTest(id, tableTestType);
return addHref(uriInfo, table);
}
@PUT
@Path("/{id}/columnTest")
@Operation(
operationId = "addColumnTest",
summary = "Add column test cases",
tags = "tables",
description = "Add column test cases to the table.",
responses = {
@ApiResponse(
responseCode = "200",
description = "OK",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Table.class)))
})
public Table addColumnTest(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the table", schema = @Schema(type = "string")) @PathParam("id") String id,
@Valid CreateColumnTest createColumnTest)
throws IOException {
authorizer.authorizeAdmin(securityContext, true);
ColumnTest columnTest = getColumnTest(securityContext, createColumnTest);
Table table = dao.addColumnTest(UUID.fromString(id), columnTest);
return addHref(uriInfo, table);
}
@PUT @PUT
@Path("/{id}/customMetric") @Path("/{id}/customMetric")
@Operation( @Operation(
@ -911,33 +836,6 @@ public class TableResource extends EntityResource<Table, TableRepository> {
return addHref(uriInfo, table); return addHref(uriInfo, table);
} }
@DELETE
@Path("/{id}/columnTest/{columnName}/{columnTestType}")
@Operation(
operationId = "deleteColumnTest",
summary = "delete column test case",
tags = "tables",
description = "Delete column test case from the table.",
responses = {
@ApiResponse(
responseCode = "200",
description = "OK",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Table.class)))
})
public Table deleteColumnTest(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the table", schema = @Schema(type = "UUID")) @PathParam("id") UUID id,
@Parameter(description = "column of the table", schema = @Schema(type = "string")) @PathParam("columnName")
String columnName,
@Parameter(description = "column Test Type", schema = @Schema(type = "string")) @PathParam("columnTestType")
String columnTestType)
throws IOException {
authorizer.authorizeAdmin(securityContext, true);
Table table = dao.deleteColumnTest(id, columnName, columnTestType);
return addHref(uriInfo, table);
}
@DELETE @DELETE
@Path("/{id}/customMetric/{columnName}/{customMetricName}") @Path("/{id}/customMetric/{columnName}/{customMetricName}")
@Operation( @Operation(

View File

@ -140,6 +140,39 @@ public class PermissionsResource {
return authorizer.getPermission(securityContext, user, resourceContext); return authorizer.getPermission(securityContext, user, resourceContext);
} }
@GET
@Path("/{resource}/name/{name}")
@Operation(
operationId = "getResourcePermissionByName",
summary = "Get permissions for a given entity name for a logged in user",
tags = "permission",
responses = {
@ApiResponse(
responseCode = "200",
description = "Permissions for logged in user",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = ResourcePermissionList.class)))
})
public ResourcePermission getPermission(
@Context SecurityContext securityContext,
@Parameter(
description =
"Permission for user specified in this query param. If not specified, the user is "
+ "defaulted to the logged in user",
schema = @Schema(type = "string", example = "john"))
@QueryParam("user")
String user,
@Parameter(description = "Resource type", schema = @Schema(type = "String")) @PathParam("resource")
String resource,
@Parameter(description = "Entity Name", schema = @Schema(type = "String")) @PathParam("name") String name) {
EntityRepository<EntityInterface> entityRepository = Entity.getEntityRepository(resource);
ResourceContext resourceContext =
ResourceContext.builder().resource(resource).name(name).entityRepository(entityRepository).build();
return authorizer.getPermission(securityContext, user, resourceContext);
}
static class ResourcePermissionList extends ResultList<ResourcePermission> { static class ResourcePermissionList extends ResultList<ResourcePermission> {
@SuppressWarnings("unused") @SuppressWarnings("unused")
public ResourcePermissionList() {} public ResourcePermissionList() {}

View File

@ -117,7 +117,6 @@ import org.openmetadata.catalog.tests.table.TableColumnCountToEqual;
import org.openmetadata.catalog.tests.table.TableRowCountToBeBetween; import org.openmetadata.catalog.tests.table.TableRowCountToBeBetween;
import org.openmetadata.catalog.tests.table.TableRowCountToEqual; import org.openmetadata.catalog.tests.table.TableRowCountToEqual;
import org.openmetadata.catalog.tests.type.TestCaseResult; import org.openmetadata.catalog.tests.type.TestCaseResult;
import org.openmetadata.catalog.tests.type.TestCaseStatus;
import org.openmetadata.catalog.type.ChangeDescription; import org.openmetadata.catalog.type.ChangeDescription;
import org.openmetadata.catalog.type.Column; import org.openmetadata.catalog.type.Column;
import org.openmetadata.catalog.type.ColumnConstraint; import org.openmetadata.catalog.type.ColumnConstraint;
@ -1391,136 +1390,6 @@ public class TableResourceTest extends EntityResourceTest<Table, CreateTable> {
verifyCustomMetrics(table, c1, List.of(createMetric2)); verifyCustomMetrics(table, c1, List.of(createMetric2));
} }
@Test
void createUpdateDelete_tableColumnTests_200(TestInfo test) throws IOException {
Table table = createAndCheckEntity(createRequest(test), ADMIN_AUTH_HEADERS);
TableRowCountToEqual tableRowCountToEqual = new TableRowCountToEqual().withValue(100);
TableTestCase tableTestCase =
new TableTestCase()
.withTableTestType(TableTestCase.TableTestType.TABLE_ROW_COUNT_TO_EQUAL)
.withConfig(tableRowCountToEqual);
CreateTableTest createTableTest = new CreateTableTest().withTestCase(tableTestCase);
Table putResponse = putTableTest(table.getId(), createTableTest, ADMIN_AUTH_HEADERS);
verifyTableTest(putResponse.getName(), putResponse.getTableTests(), List.of(createTableTest));
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyTableTest(table.getName(), table.getTableTests(), List.of(createTableTest));
// Add result to tableTest
TestCaseResult testCaseResult1 =
new TestCaseResult()
.withResult("Rows equal to 100")
.withTestCaseStatus(TestCaseStatus.Success)
.withSampleData("Rows == 100")
.withTimestamp(100L);
createTableTest.setResult(testCaseResult1);
putResponse = putTableTest(table.getId(), createTableTest, ADMIN_AUTH_HEADERS);
verifyTableTest(putResponse.getName(), putResponse.getTableTests(), List.of(createTableTest));
TestCaseResult testCaseResult2 =
new TestCaseResult()
.withResult("Rows equal to 100")
.withTestCaseStatus(TestCaseStatus.Success)
.withSampleData("Rows == 100")
.withTimestamp(100L);
createTableTest.setResult(testCaseResult2);
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyTableTest(table.getName(), table.getTableTests(), List.of(createTableTest));
TableRowCountToBeBetween tableRowCountToBeBetween =
new TableRowCountToBeBetween().withMinValue(100).withMaxValue(1000);
TableTestCase tableTestCase1 =
new TableTestCase()
.withTableTestType(TableTestCase.TableTestType.TABLE_ROW_COUNT_TO_BE_BETWEEN)
.withConfig(tableRowCountToBeBetween);
CreateTableTest createTableTest1 = new CreateTableTest().withTestCase(tableTestCase1);
putResponse = putTableTest(table.getId(), createTableTest1, ADMIN_AUTH_HEADERS);
// returns the current test thats updated or created
verifyTableTest(putResponse.getName(), putResponse.getTableTests(), List.of(createTableTest1));
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyTableTest(table.getName(), table.getTableTests(), List.of(createTableTest, createTableTest1));
// update the test case
tableRowCountToBeBetween = new TableRowCountToBeBetween().withMaxValue(10).withMaxValue(100);
tableTestCase1.withConfig(tableRowCountToBeBetween);
putResponse = putTableTest(table.getId(), createTableTest1, ADMIN_AUTH_HEADERS);
// returns the current test thats updated or created
verifyTableTest(putResponse.getName(), putResponse.getTableTests(), List.of(createTableTest1));
Column c1 = table.getColumns().get(0);
ColumnValueLengthsToBeBetween columnValueLengthsToBeBetween =
new ColumnValueLengthsToBeBetween().withMaxLength(100).withMinLength(10);
ColumnTestCase columnTestCase =
new ColumnTestCase()
.withColumnTestType(ColumnTestCase.ColumnTestType.COLUMN_VALUE_LENGTHS_TO_BE_BETWEEN)
.withConfig(columnValueLengthsToBeBetween);
CreateColumnTest createColumnTest =
new CreateColumnTest().withColumnName(c1.getName()).withTestCase(columnTestCase);
putResponse = putColumnTest(table.getId(), createColumnTest, ADMIN_AUTH_HEADERS);
verifyColumnTest(putResponse, c1, List.of(createColumnTest));
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyTableTest(table.getName(), table.getTableTests(), List.of(createTableTest, createTableTest1));
verifyColumnTest(table, c1, List.of(createColumnTest));
// Add result to columnTest
TestCaseResult colTestCaseResult =
new TestCaseResult()
.withResult("min is > 100 and max < 1000")
.withTestCaseStatus(TestCaseStatus.Success)
.withSampleData("minValue is 100 and maxValue is 1000")
.withTimestamp(100L);
createColumnTest.setResult(colTestCaseResult);
putResponse = putColumnTest(table.getId(), createColumnTest, ADMIN_AUTH_HEADERS);
verifyColumnTest(putResponse, c1, List.of(createColumnTest));
ColumnValuesMissingCountToBeEqual columnValuesMissingCountToBeEqual =
new ColumnValuesMissingCountToBeEqual().withMissingCountValue(10);
ColumnTestCase columnTestCase1 =
new ColumnTestCase()
.withColumnTestType(ColumnTestCase.ColumnTestType.COLUMN_VALUES_MISSING_COUNT_TO_BE_EQUAL)
.withConfig(columnValuesMissingCountToBeEqual);
CreateColumnTest createColumnTest1 =
new CreateColumnTest().withColumnName(c1.getName()).withTestCase(columnTestCase1);
putResponse = putColumnTest(table.getId(), createColumnTest1, ADMIN_AUTH_HEADERS);
verifyColumnTest(putResponse, c1, List.of(createColumnTest1));
// update the test config
columnValuesMissingCountToBeEqual = new ColumnValuesMissingCountToBeEqual().withMissingCountValue(100);
columnTestCase1 =
new ColumnTestCase()
.withColumnTestType(ColumnTestCase.ColumnTestType.COLUMN_VALUES_MISSING_COUNT_TO_BE_EQUAL)
.withConfig(columnValuesMissingCountToBeEqual);
createColumnTest1 = new CreateColumnTest().withColumnName(c1.getName()).withTestCase(columnTestCase1);
putResponse = putColumnTest(table.getId(), createColumnTest1, ADMIN_AUTH_HEADERS);
verifyColumnTest(putResponse, c1, List.of(createColumnTest1));
// Add result to columnTest
TestCaseResult colTestCaseResult1 =
new TestCaseResult()
.withResult("min is > 100 and max < 1000")
.withTestCaseStatus(TestCaseStatus.Success)
.withSampleData("minValue is 100 and maxValue is 1000")
.withTimestamp(100L);
createColumnTest.setResult(colTestCaseResult1);
putResponse = putColumnTest(table.getId(), createColumnTest, ADMIN_AUTH_HEADERS);
createColumnTest.setResult(colTestCaseResult1);
verifyColumnTest(putResponse, c1, List.of(createColumnTest));
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyColumnTest(table, c1, List.of(createColumnTest, createColumnTest1));
// delete the table test case
deleteTableTest(table.getId(), createTableTest1.getTestCase().getTableTestType().toString(), ADMIN_AUTH_HEADERS);
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyTableTest(table.getName(), table.getTableTests(), List.of(createTableTest));
// delete column test case
deleteColumnTest(table.getId(), c1.getName(), columnTestCase1.getColumnTestType().toString(), ADMIN_AUTH_HEADERS);
table = getEntity(table.getId(), "tests", ADMIN_AUTH_HEADERS);
verifyColumnTest(table, c1, List.of(createColumnTest));
}
@Test @Test
void get_deletedTableWithDeleteLocation(TestInfo test) throws IOException { void get_deletedTableWithDeleteLocation(TestInfo test) throws IOException {
CreateTable create = createRequest(getEntityName(test), "description", "displayName", USER1_REF); CreateTable create = createRequest(getEntityName(test), "description", "displayName", USER1_REF);

View File

@ -170,6 +170,10 @@ class PermissionsResourceTest extends CatalogApplicationTest {
ResourcePermission actualPermission = getPermission(Entity.TABLE, table1.getId(), null, authHeaders); ResourcePermission actualPermission = getPermission(Entity.TABLE, table1.getId(), null, authHeaders);
assertAllOperationsAllowed(actualPermission); assertAllOperationsAllowed(actualPermission);
// get permissions by resource entity name
actualPermission = getPermissionByName(Entity.TABLE, table1.getFullyQualifiedName(), null, authHeaders);
assertAllOperationsAllowed(actualPermission);
// Admin getting permissions for a specific resource on for Data consumer // Admin getting permissions for a specific resource on for Data consumer
actualPermission = getPermission(Entity.TABLE, table1.getId(), DATA_CONSUMER_USER_NAME, ADMIN_AUTH_HEADERS); actualPermission = getPermission(Entity.TABLE, table1.getId(), DATA_CONSUMER_USER_NAME, ADMIN_AUTH_HEADERS);
assertAllOperationsAllowed(actualPermission); assertAllOperationsAllowed(actualPermission);
@ -300,4 +304,11 @@ class PermissionsResourceTest extends CatalogApplicationTest {
target = user != null ? target.queryParam("user", user) : target; target = user != null ? target.queryParam("user", user) : target;
return TestUtils.get(target, ResourcePermission.class, authHeaders); return TestUtils.get(target, ResourcePermission.class, authHeaders);
} }
public ResourcePermission getPermissionByName(
String resource, String name, String user, Map<String, String> authHeaders) throws HttpResponseException {
WebTarget target = getResource("permissions/" + resource + "/name/" + name);
target = user != null ? target.queryParam("user", user) : target;
return TestUtils.get(target, ResourcePermission.class, authHeaders);
}
} }

View File

@ -31,8 +31,6 @@ from metadata.generated.schema.api.services.createDatabaseService import (
CreateDatabaseServiceRequest, CreateDatabaseServiceRequest,
) )
from metadata.generated.schema.api.teams.createUser import CreateUserRequest from metadata.generated.schema.api.teams.createUser import CreateUserRequest
from metadata.generated.schema.api.tests.createColumnTest import CreateColumnTestRequest
from metadata.generated.schema.api.tests.createTableTest import CreateTableTestRequest
from metadata.generated.schema.entity.data.table import ( from metadata.generated.schema.entity.data.table import (
Column, Column,
ColumnJoins, ColumnJoins,
@ -56,18 +54,8 @@ from metadata.generated.schema.entity.services.databaseService import (
DatabaseService, DatabaseService,
DatabaseServiceType, DatabaseServiceType,
) )
from metadata.generated.schema.tests.basic import TestCaseResult, TestCaseStatus
from metadata.generated.schema.tests.column.columnValuesToBeBetween import (
ColumnValuesToBeBetween,
)
from metadata.generated.schema.tests.columnTest import ColumnTestCase, ColumnTestType
from metadata.generated.schema.tests.table.tableRowCountToEqual import (
TableRowCountToEqual,
)
from metadata.generated.schema.tests.tableTest import TableTestCase, TableTestType
from metadata.generated.schema.type.entityReference import EntityReference from metadata.generated.schema.type.entityReference import EntityReference
from metadata.generated.schema.type.usageRequest import UsageRequest from metadata.generated.schema.type.usageRequest import UsageRequest
from metadata.ingestion.ometa.client import APIError
from metadata.ingestion.ometa.ometa_api import OpenMetadata from metadata.ingestion.ometa.ometa_api import OpenMetadata
@ -458,126 +446,6 @@ class OMetaTableTest(TestCase):
assert res.id == entity_ref.id assert res.id == entity_ref.id
def test_add_table_tests(self):
"""
Add tableTests to table instance
"""
table = self.metadata.create_or_update(data=self.create)
table_test = CreateTableTestRequest(
description="Testing something",
testCase=TableTestCase(
config=TableRowCountToEqual(value=100),
tableTestType=TableTestType.tableRowCountToEqual,
),
)
table_with_test = self.metadata.add_table_test(
table=table, table_test=table_test
)
assert len(table_with_test.tableTests) == 1
assert table_with_test.tableTests[0].testCase == table_test.testCase
test_case_result = TestCaseResult(
result="some result",
timestamp=datetime.now().timestamp(),
testCaseStatus=TestCaseStatus.Success,
)
table_test_with_res = CreateTableTestRequest(
description="Testing something",
testCase=TableTestCase(
config=TableRowCountToEqual(value=100),
tableTestType=TableTestType.tableRowCountToEqual,
),
result=test_case_result,
)
table_with_test_and_res = self.metadata.add_table_test(
table=table, table_test=table_test_with_res
)
assert len(table_with_test_and_res.tableTests[0].results) == 1
assert (
table_with_test_and_res.tableTests[0].results[0].testCaseStatus
== TestCaseStatus.Success
)
def test_add_column_tests(self):
"""
Add columnTests to table instance
"""
table = self.metadata.create_or_update(data=self.create)
col_test = CreateColumnTestRequest(
columnName="id",
testCase=ColumnTestCase(
config=ColumnValuesToBeBetween(minValue=1, maxValue=3),
columnTestType=ColumnTestType.columnValuesToBeBetween,
),
)
updated_table = self.metadata.add_column_test(table=table, col_test=col_test)
id_test = next(
iter([col for col in updated_table.columns if col.name.__root__ == "id"]),
None,
)
assert len(id_test.columnTests) == 1
assert id_test.columnTests[0].testCase == col_test.testCase
# Column needs to exist in the table!
with pytest.raises(APIError):
ko_test = CreateColumnTestRequest(
columnName="random_column",
testCase=ColumnTestCase(
config=ColumnValuesToBeBetween(minValue=1, maxValue=3),
columnTestType=ColumnTestType.columnValuesToBeBetween,
),
)
self.metadata.add_column_test(table=table, col_test=ko_test)
col_test_res = TestCaseResult(
result="some result",
timestamp=datetime.now().timestamp(),
testCaseStatus=TestCaseStatus.Success,
)
col_test_with_res = CreateColumnTestRequest(
columnName="id",
testCase=ColumnTestCase(
config=ColumnValuesToBeBetween(minValue=1, maxValue=3),
columnTestType=ColumnTestType.columnValuesToBeBetween,
),
result=col_test_res,
)
table_with_test_and_res = self.metadata.add_column_test(
table=table, col_test=col_test_with_res
)
id_test_res = next(
iter(
[
col
for col in table_with_test_and_res.columns
if col.name.__root__ == "id"
]
),
None,
)
assert len(id_test_res.columnTests[0].results) == 1
assert (
id_test_res.columnTests[0].results[0].testCaseStatus
== TestCaseStatus.Success
)
def test_update_profile_sample(self): def test_update_profile_sample(self):
""" """
We can safely update the profile sample % We can safely update the profile sample %