Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controller): e2e test for remove/recover trash and fix bug #1571

Merged
merged 8 commits into from
Dec 2, 2022
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
74 changes: 74 additions & 0 deletions scripts/apitest/pytest/test_trash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest
import requests

from common import httputil as hu


class TestTrash:

def test_login(self, host, port):
hu.login(host, port)

def test_recover_model(self, host, port):
recover_trash(host, port, 'model')

def test_recover_dataset(self, host, port):
recover_trash(host, port, 'dataset')

def test_recover_runtime(self, host, port):
recover_trash(host, port, 'runtime')


def recover_trash(host, port, bundle_type):
res = requests.get(url=hu.url(host, port) + '/project'
'/1'
'/' + bundle_type,
headers=hu.header())
response = res.json()
if len(response['data']['list']) == 0:
print('Test Trash recover model cancel.')
return

obj_name = response['data']['list'][0]['name']
res = requests.delete(url=hu.url(host, port) + '/project'
'/1'
'/' + bundle_type +
'/' + obj_name,
headers=hu.header())
assert res.status_code == 200

res = requests.get(url=hu.url(host, port) + '/project'
'/1'
'/trash',
headers=hu.header())
response = res.json()
assert res.status_code == 200
assert response['data']['list'] is not None
assert len(response['data']['list']) > 0
trash_id = None
print(response['data']['list'])
for trash in response['data']['list']:
if trash['name'] == obj_name:
trash_id = trash['id']
break
assert trash_id is not None
res = requests.put(url=hu.url(host, port) + '/project'
'/1'
'/trash'
'/' + trash_id,
headers=hu.header())
assert res.status_code == 200

res = requests.get(url=hu.url(host, port) + '/project'
'/1'
'/' + bundle_type +
'/' + obj_name,
headers=hu.header())
response = res.json()
assert res.status_code == 200
assert response['code'] == 'success'
assert response['data']['name'] == obj_name


if __name__ == '__main__':
pytest.main(['test_trash.py'])
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public String findByNameSql(@Param("name") String name,
SELECT(COLUMNS);
FROM("dataset_info");
WHERE("dataset_name = #{name}");
WHERE("is_deleted = 0");
if (Objects.nonNull(projectId)) {
WHERE("project_id = #{projectId}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@Data
@SuperBuilder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public String findByNameSql(@Param("name") String name,
SELECT(COLUMNS);
FROM("model_info");
WHERE("model_name = #{name}");
WHERE("is_deleted = 0");
if (Objects.nonNull(projectId)) {
WHERE("project_id = #{projectId}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@Data
@SuperBuilder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String listSql(@Param("projectId") Long projectId,
WHERE("project_id = #{projectId}");
}
if (StrUtil.isNotEmpty(namePrefix)) {
WHERE("model_name like concat(#{namePrefix}, '%')");
WHERE("runtime_name like concat(#{namePrefix}, '%')");
}
if (StrUtil.isNotEmpty(order)) {
ORDER_BY(order);
Expand All @@ -98,6 +98,7 @@ public String findByNameSql(@Param("name") String name,
SELECT(COLUMNS);
FROM("runtime_info");
WHERE("runtime_name = #{name}");
WHERE("is_deleted = 0");
if (Objects.nonNull(projectId)) {
WHERE("project_id = #{projectId}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@Data
@SuperBuilder
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,26 @@ public void testFindDeletedBundleById() {

@Test
public void testRevertTo() {
Long bundleId = 4L;
Long versionId = 3L;
Long maxOrder = 5L;
given(versionMapper.selectVersionOrderForUpdate(same(versionId)))
.willReturn(2L);

given(versionMapper.selectMaxVersionOrderOfDatasetForUpdate(same(bundleId)))
.willReturn(maxOrder);

given(versionMapper.updateVersionOrder(same(versionId), same(maxOrder)))
.willReturn(1);

var order = manager.selectVersionOrderForUpdate(bundleId, versionId);
assertThat(order, is(2L));

var max = manager.selectMaxVersionOrderOfBundleForUpdate(bundleId);
assertThat(max, is(maxOrder));

var res = manager.updateVersionOrder(versionId, max);
assertThat(res, is(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2022 Starwhale, Inc. All Rights Reserved.
*
* 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 ai.starwhale.mlops.domain.dataset.mapper;

import ai.starwhale.mlops.domain.MySqlContainerHolder;
import ai.starwhale.mlops.domain.dataset.po.DatasetEntity;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;

@MybatisTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class DatasetMapperTest extends MySqlContainerHolder {

@Autowired
private DatasetMapper datasetMapper;

@Test
public void testInsertAndFind() {
DatasetEntity entity = DatasetEntity.builder()
.datasetName("model1")
.ownerId(1L)
.projectId(2L)
.build();
datasetMapper.insert(entity);
entity.setIsDeleted(0);

Assertions.assertEquals(entity, datasetMapper.find(entity.getId()));
Assertions.assertEquals(entity, datasetMapper.findByName(entity.getDatasetName(),
entity.getProjectId(), false));
Assertions.assertEquals(entity, datasetMapper.findByName(entity.getDatasetName(),
entity.getProjectId(), true));
}

@Test
public void testList() {
DatasetEntity dataset1 = DatasetEntity.builder()
.datasetName("dataset1")
.ownerId(1L)
.projectId(1L)
.isDeleted(0)
.build();
DatasetEntity dataset2 = DatasetEntity.builder()
.datasetName("dataset2")
.ownerId(2L)
.projectId(2L)
.isDeleted(0)
.build();
DatasetEntity dataset3 = DatasetEntity.builder()
.datasetName("dataset3")
.ownerId(2L)
.projectId(1L)
.isDeleted(0)
.build();

datasetMapper.insert(dataset1);
datasetMapper.insert(dataset2);
datasetMapper.insert(dataset3);

var list = datasetMapper.list(1L, null, null);
Assertions.assertIterableEquals(List.of(dataset3, dataset1), list);

list = datasetMapper.list(2L, null, null);
Assertions.assertIterableEquals(List.of(dataset2), list);

list = datasetMapper.list(null, "dataset", null);
Assertions.assertIterableEquals(List.of(dataset3, dataset2, dataset1), list);

list = datasetMapper.list(null, "dataset1", null);
Assertions.assertIterableEquals(List.of(dataset1), list);
}

@Test
public void testRemoveAndRecover() {
DatasetEntity dataset1 = DatasetEntity.builder()
.datasetName("dataset1")
.ownerId(1L)
.projectId(1L)
.isDeleted(0)
.build();
datasetMapper.insert(dataset1);

datasetMapper.remove(dataset1.getId());
Assertions.assertEquals(1, datasetMapper.find(dataset1.getId()).getIsDeleted());
Assertions.assertNull(datasetMapper.findByName(dataset1.getDatasetName(), dataset1.getProjectId(), false));
Assertions.assertEquals(dataset1.getDatasetName(),
datasetMapper.findDeleted(dataset1.getId()).getDatasetName());

datasetMapper.recover(dataset1.getId());
Assertions.assertEquals(0, datasetMapper.find(dataset1.getId()).getIsDeleted());
Assertions.assertEquals(dataset1,
datasetMapper.findByName(dataset1.getDatasetName(), dataset1.getProjectId(), false));
Assertions.assertNull(datasetMapper.findDeleted(dataset1.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,26 @@ public void testFindDeletedBundleById() {

@Test
public void testRevertTo() {
Long bundleId = 3L;
Long versionId = 1L;
Long maxOrder = 4L;
given(versionMapper.selectVersionOrderForUpdate(same(versionId)))
.willReturn(2L);

given(versionMapper.selectMaxVersionOrderOfModelForUpdate(same(bundleId)))
.willReturn(maxOrder);

given(versionMapper.updateVersionOrder(same(versionId), same(maxOrder)))
.willReturn(1);

var order = manager.selectVersionOrderForUpdate(bundleId, versionId);
assertThat(order, is(2L));

var max = manager.selectMaxVersionOrderOfBundleForUpdate(bundleId);
assertThat(max, is(maxOrder));

var res = manager.updateVersionOrder(versionId, max);
assertThat(res, is(1));
}

@Test
Expand All @@ -210,4 +229,6 @@ public void testRemove() {
res = manager.remove(2L);
assertThat(res, is(false));
}


}
Loading