Skip to content

Commit

Permalink
1 调整 部分 测试类
Browse files Browse the repository at this point in the history
2  修复了一些 collation  模块  #75
  • Loading branch information
HbnKing committed Dec 1, 2023
1 parent f78fba4 commit 4aef5db
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* query itself specifies the same collation.
*/

@Deprecated
public class Collation {

private static final Collation SIMPLE = of("simple");
Expand All @@ -75,6 +76,7 @@ public class Collation {
private Optional<Boolean> normalization = Optional.empty();
private Optional<String> version = Optional.empty();

@Deprecated
private Collation(CollationLocale locale) {

Precondition.notNull(locale, "ICULocale must not be null!");
Expand All @@ -97,6 +99,7 @@ public static Collation simple() {
* @param locale must not be {@literal null}.
* @return new instance of {@link Collation}.
*/

public static Collation of(Locale locale) {

Precondition.notNull(locale, "Locale must not be null!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ public void setMeta(Meta meta) {
*
* @param collation can be {@literal null}.
* @return this.
*
*/
@Deprecated
public Query collation(Collation collation ) {

this.collation = Optional.ofNullable(collation.toMongoCollation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ protected < T > FindIterable< T > doFind( Query query, @Nullable Class< T > enti
findIterable = findIterable.limit(query.getLimit());
}

if(query.getCollation().isPresent()){
findIterable = findIterable.collation(query.getCollation().get());
}

if (ObjectUtil.isNotEmpty(query.getMeta())) {
Meta meta = query.getMeta();
if (ObjectUtil.isNotEmpty(meta.getComment())) {
Expand Down Expand Up @@ -1275,7 +1279,7 @@ public < T > long count( Query query, String collectionName ) {
public < T > MongoCollection< Document > createCollection( Class< T > entityClass ) {
notNull(entityClass, "EntityClass must not be null!");

return createCollection(entityClass, getMongoCollectionOptions(entityClass));
return createCollection(entityClass, scanEntityCollectionOptions(entityClass));
}

private GridFSUploadOptions computeUploadOptionsFor( String contentType, Document metadata ) {
Expand Down Expand Up @@ -1312,22 +1316,22 @@ public < T > MongoCollection< Document > createCollection( Class< T > entityClas

CreateCollectionOptions options = collectionOptions != null ? collectionOptions : new CreateCollectionOptions();

Document document = convertToDocument(options);
//Document document = convertToDocument(options);

return doCreateCollection(this.mapper.getEntityModel(entityClass).getCollectionName(), document);
return doCreateCollection(this.mapper.getEntityModel(entityClass).getCollectionName(), collectionOptions);
}

@Override
public MongoCollection< Document > createCollection( String collectionName ) {
notNull(collectionName, "CollectionName must not be null!");

return doCreateCollection(collectionName, new Document());
return doCreateCollection(collectionName, new CreateCollectionOptions());
}

@Override
public MongoCollection< Document > createCollection( String collectionName, CreateCollectionOptions collectionOptions ) {
notNull(collectionName, "collectionName must not be null");
return doCreateCollection(collectionName, convertToDocument(collectionOptions));
return doCreateCollection(collectionName, collectionOptions);
}

@Override
Expand Down Expand Up @@ -1375,7 +1379,7 @@ public < T > void dropCollection( Class< T > entityClass ) {
dropCollection(this.mapper.getEntityModel(entityClass).getCollectionName());
}

// @Override
@Deprecated
public MongoCollection< Document > createCollection( String collectionName, CollectionOptions collectionOptions ) {

notNull(collectionName, "CollectionName must not be null!");
Expand Down Expand Up @@ -1498,6 +1502,27 @@ protected Document convertToDocument( CreateCollectionOptions collectionOptions
return document;
}


protected MongoCollection< Document > doCreateCollection( String collectionName, CreateCollectionOptions createcollectionOptions ) {
lock.lock();
MongoCollection< Document > coll ;
try{
this.database.createCollection(collectionName, createcollectionOptions);

coll = database.getCollection(collectionName, Document.class);
//如果配置文件中开启了自动创建索引,则在表创建成功后创建索引
if (this.mapper.isAutoIndexCreation()) {
Class< Object > entity = this.mapper.getClassFromCollection(collectionName);
ensureIndexes(entity, coll.getNamespace().getCollectionName());
}

} finally {
lock.unlock();
}

return coll;
}

protected MongoCollection< Document > doCreateCollection( String collectionName, Document collectionOptions ) {
lock.lock();
try {
Expand All @@ -1515,7 +1540,11 @@ protected MongoCollection< Document > doCreateCollection( String collectionName,

//如果选项里面有关于collation方面的操作,
if (collectionOptions.containsKey("collation")) {
co.collation(com.whaleal.mars.core.query.Collation.from((Document.parse(collectionOptions.get("collation").toString()))).toMongoCollation());
String collation = collectionOptions.get("collation").toString();
System.out.println(collation);
Document parse = Document.parse(collation);
System.out.println(parse);
co.collation(com.whaleal.mars.core.query.Collation.from((parse)).toMongoCollation());
}

if (collectionOptions.containsKey("validator")) {
Expand Down Expand Up @@ -1672,7 +1701,7 @@ private CollectionOptions getCollectionOptions( Class< ? > entity ) {
}

//todo 此方法是额外写的方法 与getCollectionOptions一致 是用来解决报错 后续可能不需要
private CreateCollectionOptions getMongoCollectionOptions( Class< ? > entity ) {
private CreateCollectionOptions scanEntityCollectionOptions( Class< ? > entity ) {

EntityModel entityModel = this.mapper.getEntityModel(entity);
notNull(entity, "EntityClass must not be null!");
Expand Down
4 changes: 2 additions & 2 deletions mars-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<scope>test</scope>

</dependency>
<dependency>
<!-- <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0</version>
Expand All @@ -82,7 +82,7 @@
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependency>-->


<dependency>
Expand Down
15 changes: 15 additions & 0 deletions mars-test/src/main/java/com/whaleal/mars/bean/NumberBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.whaleal.mars.codecs.pojo.annotations.CappedAt;
import com.whaleal.mars.codecs.pojo.annotations.Collation;
import com.whaleal.mars.codecs.pojo.annotations.Entity;
import com.whaleal.mars.core.index.IndexDirection;
import com.whaleal.mars.core.index.annotation.Field;
import com.whaleal.mars.core.index.annotation.Index;
import com.whaleal.mars.core.index.annotation.IndexOptions;
import com.whaleal.mars.core.index.annotation.Indexes;

/**
* @author lyz
Expand All @@ -12,6 +17,16 @@
@Entity
@Collation(locale = "zh")
@CappedAt(count = 1000,value = 1024 * 1024)
@Indexes({@Index(fields = @Field(value = "stocks",type = IndexDirection.ASC)),
@Index(fields = {@Field(value = "name",type = IndexDirection.ASC),
@Field(value = "price",type = IndexDirection.DESC)}
,options = @IndexOptions(collation = @com.whaleal.mars.core.index.annotation.Collation(locale = "zh",numericOrdering = true)))})
public class NumberBean {

public int stocks ;

public String name ;

public double price ;

}
58 changes: 48 additions & 10 deletions mars-test/src/test/java/com/whaleal/mars/base/CollationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.whaleal.mars.base;

import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Collation;
import com.mongodb.client.model.CreateCollectionOptions;
import com.whaleal.icefrog.core.util.StrUtil;
Expand All @@ -9,6 +10,7 @@
import com.whaleal.mars.core.Mars;
import com.whaleal.mars.core.aggregation.AggregationPipeline;
import com.whaleal.mars.core.aggregation.stages.Projection;
import com.whaleal.mars.core.index.Index;
import com.whaleal.mars.core.query.Query;
import com.whaleal.mars.core.query.Sort;
import com.whaleal.mars.session.option.CollectionOptions;
Expand All @@ -22,6 +24,7 @@

/**
* @author lyz
* @author wh
* @description
* @date 2022-07-13 15:27
**/
Expand All @@ -39,7 +42,7 @@ public void dropCollection() {
* 创建集合时手动指定字符集排序规则
*/
@Test
public void testForWhenCreate() {
public void CollationForCreation() {

mars.createCollection("student", CollectionOptions.just(Collation.builder().locale("zh").build()));
List< Document > list = CreateDataUtil.parseString("{\"name\" : \"张七\" }\n" +
Expand All @@ -49,10 +52,12 @@ public void testForWhenCreate() {
"{\"name\" : \"马六\" }");
mars.insert(list, "student");


AggregationPipeline< Document > pipeline = AggregationPipeline.create();
pipeline.sort(com.whaleal.mars.core.aggregation.stages.Sort.sort().ascending("name"));
pipeline.project(Projection.project().exclude("_id"));

// 查询结果应当是有序的 正确的
List< Document > list1 = mars.aggregate(pipeline, "student").toList();

List< Document > list2 = CreateDataUtil.parseString("{\"name\" : \"李四\" }\n" +
Expand All @@ -67,38 +72,58 @@ public void testForWhenCreate() {

Assert.assertEquals(list1, list2);




MongoIterable< Document > collectionInfos = mars.getDatabase().listCollections();

// Iterate through collection information
for (Document collectionInfo : collectionInfos) {

Document document = collectionInfo.get("options", Document.class);

Document collation = document.get("collation",Document.class);

System.out.println(collation);

Assert.assertEquals("zh",collation.get("locale"));

}
}

/**
* 创建普通集合,查询时才指定排序规则
*/
@Test
public void testForWhenQuery() {
mars.createCollection("student1");
public void CollationForQuery() {

String tableName = "student";

mars.createCollection(tableName);
List< Document > list = CreateDataUtil.parseString("{\"name\" : \"张七\" }\n" +
"{\"name\" : \"张三\" }\n" +
"{\"name\" : \"李四\" }\n" +
"{\"name\" : \"王五\" }\n" +
"{\"name\" : \"马六\" }");
mars.insert(list, "student1");
mars.insert(list, tableName);


//List< Document > list1 = mars.find(new Query().withProjection(new com.whaleal.mars.core.query.Projection().exclude("_id")).with(Sort.ascending("name")).collation(com.whaleal.mars.core.query.Collation.of("zh")), Document.class, "student1").toList();

List< Document > list1 = mars.find(new Query().withProjection(new com.whaleal.mars.core.query.Projection().exclude("_id")).with(Sort.ascending("name")), Document.class, "student1").toList();
List< Document > list1 = mars.find(new Query().withProjection(new com.whaleal.mars.core.query.Projection().exclude("_id")).with(Sort.ascending("name")).collation(Collation.builder().locale("zh").build()), Document.class, tableName).toList();
List< Document > list2 = CreateDataUtil.parseString("{\"name\" : \"李四\" }\n" +
"{\"name\" : \"马六\" }\n" +
"{\"name\" : \"王五\" }\n" +
"{\"name\" : \"张七\" }\n" +
"{\"name\" : \"张三\" }");
System.out.println(StrUtil.toString(list1));
System.out.println(StrUtil.toString(list2));
Assert.assertEquals(list1, list2);
Assert.assertEquals(list2, list1);
}

@Test
public void testForWithAnno() {
public void CollationForEntityAnno() {

mars.createCollection(NumberBean.class);
mars.ensureIndexes(NumberBean.class);
List< Document > list = CreateDataUtil.parseString("{\"name\" : \"张七\" }\n" +
"{\"name\" : \"张三\" }\n" +
"{\"name\" : \"李四\" }\n" +
Expand All @@ -119,7 +144,20 @@ public void testForWithAnno() {
"{\"name\" : \"张三\" }");

System.out.println(StrUtil.toString(list));
Assert.assertEquals(list1, list2);
System.out.println(StrUtil.toString(list1));
System.out.println(StrUtil.toString(list2));
Assert.assertEquals(list2, list1);


List< Index > numberBean = mars.getIndexes("numberBean");
for(Index index :numberBean){
String locale = index.getIndexOptions().getCollation().getLocale();

System.out.println(index.getIndexKeys());
System.out.println(locale);
Assert.assertEquals("zh",locale);
}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ public class MongoPropertiesTest {

@Before
public void init() {

Assert.assertNotNull(properties);

}


@Test
public void test01() {

Assert.assertNotNull(properties.getUri(), "uri is not null");
}


@Test
public void testProperties() {
public void test() {

Assert.assertNotNull(properties.getUri());
Assert.assertNotNull(properties.getUri(), "uri is not null");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bson.Document;
import org.junit.Test;


import java.io.*;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -40,7 +39,6 @@ private static List< Document > getThousandDocuments() throws IOException, Class

if (i <= 0) {
break;

}
}

Expand Down
13 changes: 4 additions & 9 deletions mars-test/src/test/java/com/whaleal/mars/util/ZipCodesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public class ZipCodesUtil {

public static List<Document> createDocument() throws IOException, ClassNotFoundException {
public static List<Document> createDocument(){

List<Document> doc = new ArrayList<>();
BufferedReader reader;
Expand All @@ -24,7 +24,6 @@ public static List<Document> createDocument() throws IOException, ClassNotFoundE
String line = reader.readLine();
while (line != null) {
doc.add(Document.parse(line));
// System.out.println(line);
line = reader.readLine();
}
reader.close();
Expand All @@ -37,12 +36,8 @@ public static List<Document> createDocument() throws IOException, ClassNotFoundE

@Test
public void test(){
try {
List<Document> document = createDocument();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}

List<Document> document = createDocument();

}
}

0 comments on commit 4aef5db

Please sign in to comment.