Skip to content

Commit

Permalink
[Add] 新增专门用于测试的模块
Browse files Browse the repository at this point in the history
  • Loading branch information
CCweixiao committed Aug 4, 2023
1 parent e66d8fa commit 773e20c
Show file tree
Hide file tree
Showing 14 changed files with 1,582 additions and 1,147 deletions.
1,115 changes: 978 additions & 137 deletions README-EN.md

Large diffs are not rendered by default.

1,127 changes: 144 additions & 983 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions hydraql-console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<packaging>jar</packaging>
<artifactId>hydraql-console_${hydraql.hbase.adapter.version}</artifactId>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.jline</groupId>
Expand Down
35 changes: 35 additions & 0 deletions hydraql-tests/hydraql-template-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hydraql-tests</artifactId>
<groupId>com.hydraql</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hydraql-template-test</artifactId>
<packaging>jar</packaging>

<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>


<dependencies>
<dependency>
<groupId>com.hydraql</groupId>
<artifactId>hydraql-tests-common</artifactId>
</dependency>
<dependency>
<groupId>com.hydraql</groupId>
<artifactId>hydraql-template_2.2</artifactId>
</dependency>
</dependencies>

<build>
<finalName>hydraql-template-test</finalName>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.hydraql.template.model;

import com.hydraql.common.annotations.HBaseColumn;
import com.hydraql.common.annotations.HBaseRowKey;
import com.hydraql.common.annotations.HBaseTable;

import java.util.List;

/**
* @author leojie 2022/11/5 13:56
*/
@HBaseTable(namespaceName = "default", tableName = "test_table", defaultFamilyName = "f1")
public class CityModel {
@HBaseRowKey
private String cityId;
private String cityName;
private String cityAddress;

@HBaseColumn(familyName = "f2")
private Integer cityArea;
@HBaseColumn(familyName = "f2", columnName = "TOTAL_POPULATION", toUpperCase = true)
private Integer totalPopulation;
@HBaseColumn(familyName = "f2", columnName = "cityTagList")
private List<CityTag> cityTagList;

public String getCityId() {
return cityId;
}

public void setCityId(String cityId) {
this.cityId = cityId;
}

public String getCityName() {
return cityName;
}

public void setCityName(String cityName) {
this.cityName = cityName;
}

public String getCityAddress() {
return cityAddress;
}

public void setCityAddress(String cityAddress) {
this.cityAddress = cityAddress;
}

public Integer getCityArea() {
return cityArea;
}

public void setCityArea(Integer cityArea) {
this.cityArea = cityArea;
}

public Integer getTotalPopulation() {
return totalPopulation;
}

public void setTotalPopulation(Integer totalPopulation) {
this.totalPopulation = totalPopulation;
}

public List<CityTag> getCityTagList() {
return cityTagList;
}

public void setCityTagList(List<CityTag> cityTagList) {
this.cityTagList = cityTagList;
}

@Override
public String toString() {
return "CityModel{" +
"cityId='" + cityId + '\'' +
", cityName='" + cityName + '\'' +
", cityAddress='" + cityAddress + '\'' +
", cityArea=" + cityArea +
", totalPopulation=" + totalPopulation +
", cityTagList=" + cityTagList +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.hydraql.template.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author leojie 2023/8/4 23:49
*/
public class CityModelUtil {
public static CityModel createDefaultCityModel() {
return cityModel("a10001", "北京", "北京市", 1000000, 40000000,
Arrays.asList("首都", "旅游城市"));
}

public static List<CityModel> createDefaultCityModelList() {
List<CityModel> cityModels = new ArrayList<>(4);
cityModels.add(cityModel("a10001", "北京", "北京市", 1000000, 40000000,
Arrays.asList("首都", "旅游城市")));
cityModels.add(cityModel("a10002", "上海", "上海市", 1000000, 20000000,
Arrays.asList("魔都", "旅游城市")));
cityModels.add(cityModel("b20001", "广州", "广州市", 1000000, 40000000,
Arrays.asList("沿海城市", "旅游城市")));
cityModels.add(cityModel("b20002", "深圳", "深圳市", 1000000, 40000000,
Arrays.asList("沿海城市", "发达地区")));
return cityModels;
}

public static List<CityModel> createDefaultMultiVersionsCityModelList() {
List<CityModel> cityModels = new ArrayList<>(4);
cityModels.add(cityModel("a1000112", "北京", "北京市", 1000001, 40000001,
Arrays.asList("首都", "旅游城市")));
cityModels.add(cityModel("a1000112", "上海", "上海市", 1000002, 20000002,
Arrays.asList("魔都", "旅游城市")));
cityModels.add(cityModel("a1000112", "广州", "广州市", 1000003, 40000003,
null));
cityModels.add(cityModel("a1000112", "深圳", "深圳市", 1000004, 40000004,
Arrays.asList("沿海城市", "发达地区")));
return cityModels;
}

public static CityModel cityModel(String cityId, String cityName, String cityAddress, int cityArea, int totalPopulation,
List<String> tagNameList) {
CityModel cityModel = new CityModel();
cityModel.setCityId(cityId);
cityModel.setCityName(cityName);
cityModel.setCityAddress(cityAddress);
cityModel.setCityArea(cityArea);
cityModel.setTotalPopulation(totalPopulation);
if (tagNameList != null) {
cityModel.setCityTagList(tagNameList.stream().map(CityTag::new).collect(Collectors.toList()));
}
return cityModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.hydraql.template.model;

/**
* @author leojie 2022/11/5 13:59
*/
public class CityTag {
private String tagName;

public CityTag(String tagName) {
this.tagName = tagName;
}

public String getTagName() {
return tagName;
}

public void setTagName(String tagName) {
this.tagName = tagName;
}

@Override
public String toString() {
return "CityTag{" +
"tagName='" + tagName + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.hydraql.common.query.GetRowsParam;
import com.hydraql.common.query.IHBaseFilter;
import com.hydraql.common.query.ScanParams;
import com.hydraql.service.model.CityModel;
import com.hydraql.template.model.CityModel;
import com.hydraql.template.model.CityModelUtil;
import com.hydraql.tests.common.HydraQlBaseTestTemplate;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
Expand All @@ -20,49 +22,57 @@
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author leojie 2023/7/3 11:00
* @author leojie 2023/8/4 22:53
*/
public class TestHBaseTableTemplate extends BaseTestTemplate {
public class HBaseTableTestTemplateTest extends HydraQlBaseTestTemplate {
private HBaseTableTemplate tableTemplate;

@Before
public void setup() throws Exception {
startMiniCluster();
tableTemplate = HBaseTableTemplate.of(getConfiguration());
createTestTable();
}

@Test
public void testSaveMap() {
Map<String, Object> data = new HashMap<>(2);
data.put("info:name", "leo");
data.put("info:age", 18);
data.put("f1:name", "leo");
data.put("f1:age", 18);
tableTemplate.save(TEST_TABLE, "1001", data);
GetRowParam getRowParam = GetRowParam.of("1001").build();
HBaseRowData result = tableTemplate.getRow(TEST_TABLE, getRowParam);
Assert.assertEquals(2, result.getColDataContainer().size());
HBaseRowData rowData = tableTemplate.getRow(TEST_TABLE, GetRowParam.of("1001").build());
Assert.assertEquals(2, rowData.getColDataContainer().size());
}

@Test
public void testSave() {
CityModel cityModel = createDefaultCityModel();
tableTemplate.save(cityModel);
GetRowParam getRowParam = GetRowParam.of(cityModel.getCityId()).build();
Optional<CityModel> cityModelRes = tableTemplate.getRow(getRowParam, CityModel.class);
Assert.assertNotNull(cityModelRes);
public void testDelete() {
testSaveMap();
tableTemplate.delete(TEST_TABLE, "1001", F1, "name", "age");
HBaseRowData row = tableTemplate.getRow(TEST_TABLE, GetRowParam.of("1001").build());
Assert.assertTrue(row.getColDataContainer().isEmpty());
}

@Test
public void testSaveBatchMap() {
Map<String, Object> data1 = new HashMap<>();
data1.put("info:name", "leo1");
data1.put("info:age", 17);
data1.put("f1:name", "leo1");
data1.put("f1:age", 17);

Map<String, Object> data2 = new HashMap<>();
data2.put("info:name", "leo2");
data2.put("info:age", 18);
data2.put("f1:name", "leo2");
data2.put("f1:age", 18);

Map<String, Map<String, Object>> data = new HashMap<>(2);
data.put("1001", data1);
Expand All @@ -75,9 +85,28 @@ public void testSaveBatchMap() {
Assert.assertEquals(2, rowDataList.size());
}

@Test
public void testDeleteBatch() {
testSaveBatchMap();
tableTemplate.deleteBatch(TEST_TABLE, Arrays.asList("1001", "1002"));
GetRowsParam getRowsParam = GetRowsParam.of().appendRowKey("1001")
.appendRowKey("1002").build();
List<HBaseRowData> rowDataList = tableTemplate.getRows(TEST_TABLE, getRowsParam);
Assert.assertEquals(0, rowDataList.size());
}

@Test
public void testSave() {
CityModel cityModel = CityModelUtil.createDefaultCityModel();
tableTemplate.save(cityModel);
GetRowParam getRowParam = GetRowParam.of(cityModel.getCityId()).build();
Optional<CityModel> cityModelRes = tableTemplate.getRow(getRowParam, CityModel.class);
Assert.assertNotNull(cityModelRes);
}

@Test
public void testSaveBatch() {
List<CityModel> cityModelList = createDefaultCityModelList();
List<CityModel> cityModelList = CityModelUtil.createDefaultCityModelList();
tableTemplate.saveBatch(cityModelList);
List<String> rowKeys = cityModelList.stream().map(CityModel::getCityId)
.collect(Collectors.toList());
Expand All @@ -89,7 +118,8 @@ public void testSaveBatch() {

@Test
public void testGetByRowMapper() {
GetRowParam getRowParam = GetRowParam.of("1001").build();
testSave();
GetRowParam getRowParam = GetRowParam.of("a10001").build();
Map<String, String> data = tableTemplate.getRow(TEST_TABLE, getRowParam, new RowMapper<Map<String, String>>() {
@Override
public <R> Map<String, String> mapRow(R r, int rowNum) throws Exception {
Expand All @@ -100,17 +130,19 @@ public <R> Map<String, String> mapRow(R r, int rowNum) throws Exception {
Map<String, String> data = new HashMap<>(2);
for (Cell cell : result.listCells()) {
data.put(Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneValue(cell)));
}
return data;
}
}).orElse(new HashMap<>(0));
Assert.assertEquals(2, data.size());
Assert.assertEquals(5, data.size());
}


@Test
public void testScanWithCustomFilter() {
testSaveBatch();
ScanParams scanParams = ScanParams.of()
.filter(new IHBaseFilter<Filter>() {
@Override
Expand Down Expand Up @@ -139,10 +171,11 @@ public Filter customFilter() {
Assert.assertEquals(1, cityModels.size());
}


@Test
public void testGetRowToList() {
tableTemplate.delete(TEST_TABLE, "a1000112");
List<CityModel> defaultCityModelList = createDefaultMultiVersionsCityModelList();
List<CityModel> defaultCityModelList = CityModelUtil.createDefaultMultiVersionsCityModelList();
for (CityModel cityModel : defaultCityModelList) {
tableTemplate.save(cityModel);
}
Expand Down Expand Up @@ -215,8 +248,4 @@ public void testScanWithMultiVersions() {
Assert.assertEquals(1, hBaseRowDataWithMultiVersions.size());
}

@Test
public void testDelete() {
tableTemplate.delete("t1", "r1", "f", "version");
}
}
Loading

0 comments on commit 773e20c

Please sign in to comment.