|
| 1 | +package cn.vika.client.api; |
| 2 | + |
| 3 | +import cn.vika.client.api.http.ApiCredential; |
| 4 | +import cn.vika.client.api.model.CreateFieldRequest; |
| 5 | +import cn.vika.client.api.model.CreateFieldResponse; |
| 6 | +import cn.vika.client.api.model.builder.CreateFieldRequestBuilder; |
| 7 | +import cn.vika.client.api.model.field.FieldType; |
| 8 | +import cn.vika.client.api.model.field.property.FormulaFieldProperty; |
| 9 | +import cn.vika.client.api.model.field.property.SingleTextFieldProperty; |
| 10 | +import cn.vika.client.api.model.field.property.option.Format; |
| 11 | +import cn.vika.client.api.model.field.property.option.FormatType; |
| 12 | +import cn.vika.client.api.model.field.property.option.NumberFormat; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import static cn.vika.client.api.ConstantKey.TEST_API_KEY; |
| 17 | +import static cn.vika.client.api.ConstantKey.TEST_DATASHEET_ID; |
| 18 | +import static cn.vika.client.api.ConstantKey.TEST_HOST_URL; |
| 19 | +import static cn.vika.client.api.ConstantKey.TEST_SPACE_ID; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author tao |
| 24 | + */ |
| 25 | +public class FieldAddOperationTest { |
| 26 | + |
| 27 | + private final String SPACE_ID = TEST_SPACE_ID.get(); |
| 28 | + |
| 29 | + private final String DATASHEET_ID = TEST_DATASHEET_ID.get(); |
| 30 | + |
| 31 | + private final String HOST_URL = TEST_HOST_URL.get(); |
| 32 | + |
| 33 | + private final String API_KEY = TEST_API_KEY.get(); |
| 34 | + |
| 35 | + private final VikaApiClient vikaApiClient = new VikaApiClient(HOST_URL, new ApiCredential(API_KEY)); |
| 36 | + |
| 37 | + private String cacheFieldId; |
| 38 | + |
| 39 | + @Test |
| 40 | + void testCreateField() { |
| 41 | + FieldApi fieldApi = vikaApiClient.getFieldApi(); |
| 42 | + SingleTextFieldProperty singleTextFieldProperty = new SingleTextFieldProperty(); |
| 43 | + singleTextFieldProperty.setDefaultValue("hello"); |
| 44 | + CreateFieldRequest<SingleTextFieldProperty> request = CreateFieldRequestBuilder |
| 45 | + .create() |
| 46 | + .ofType(FieldType.SingleText) |
| 47 | + .withName("java test") |
| 48 | + .withProperty(singleTextFieldProperty) |
| 49 | + .build(); |
| 50 | + CreateFieldResponse response = fieldApi.addField(SPACE_ID, DATASHEET_ID, request); |
| 51 | + assertThat(response.getName()).isEqualTo("java test"); |
| 52 | + cacheFieldId = response.getId(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testCreateFormalField() { |
| 57 | + FieldApi fieldApi = vikaApiClient.getFieldApi(); |
| 58 | + FormulaFieldProperty formulaFieldProperty = new FormulaFieldProperty(); |
| 59 | + formulaFieldProperty.setExpression("1 / 3"); |
| 60 | + Format<NumberFormat> numberFormatFormat = new Format<>(); |
| 61 | + numberFormatFormat.setType(FormatType.Number); |
| 62 | + NumberFormat numberFormat = new NumberFormat(); |
| 63 | + numberFormat.setPrecision(3); |
| 64 | + numberFormatFormat.setFormat(numberFormat); |
| 65 | + formulaFieldProperty.setFormat(numberFormatFormat); |
| 66 | + CreateFieldRequest<FormulaFieldProperty> request = CreateFieldRequestBuilder |
| 67 | + .create() |
| 68 | + .ofType(FieldType.Formula) |
| 69 | + .withName("java test") |
| 70 | + .withProperty(formulaFieldProperty) |
| 71 | + .build(); |
| 72 | + CreateFieldResponse response = fieldApi.addField(SPACE_ID, DATASHEET_ID, request); |
| 73 | + assertThat(response.getName()).isEqualTo("java test"); |
| 74 | + cacheFieldId = response.getId(); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterEach |
| 78 | + void tearDown() { |
| 79 | + FieldApi fieldApi = vikaApiClient.getFieldApi(); |
| 80 | + fieldApi.deleteField(SPACE_ID, DATASHEET_ID, cacheFieldId); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments