Quellcode durchsuchen

给MessageField.cc GetFeild函数增加了unit test

tanghai vor 14 Jahren
Ursprung
Commit
6b6be41152

+ 2 - 0
Cpp/Platform/Orm/CMakeLists.txt

@@ -19,9 +19,11 @@ ADD_LIBRARY(Orm ${OrmSrc})
 SET_PROPERTY(TARGET Orm PROPERTY FOLDER "Platform")
 
 ADD_EXECUTABLE(SelectTest SelectTest.cc)
+ADD_EXECUTABLE(MessageFieldTest MessageFieldTest.cc)
 
 SET(Tests
 	SelectTest
+	MessageFieldTest
 )
 
 FOREACH(Test ${Tests})

+ 3 - 4
Cpp/Platform/Orm/DbResult.cc

@@ -41,11 +41,10 @@ void DbResult::All(std::vector<ProtobufMessagePtr>& messages)
 
 void DbResult::One(ProtobufMessagePtr message)
 {
-	if (!resultSet->next())
+	if (resultSet->next())
 	{
-		return;
+		FillMessage(message);
 	}
-	FillMessage(message);
 }
 
 std::size_t DbResult::Count()
@@ -53,4 +52,4 @@ std::size_t DbResult::Count()
 	return resultSet->rowsCount();
 }
 
-} // namespace Egametang
+} // namespace Egametang

+ 1 - 1
Cpp/Platform/Orm/DbResultTest.cc

@@ -1,7 +1,7 @@
 // Copyright: All Rights Reserved
 // Author: egametang@gmail.com (tanghai)
 
-#include "DbResult.h"
+#include "Orm/DbResult.h"
 
 namespace Egametang {
 

+ 1 - 1
Cpp/Platform/Orm/ExprTest.cc

@@ -1,7 +1,7 @@
 // Copyright: All Rights Reserved
 // Author: egametang@gmail.com (tanghai)
 
-#include "Expression.h"
+#include "Orm/Expr.h"
 
 namespace Egametang {
 

+ 5 - 6
Cpp/Platform/Orm/MessageField.cc

@@ -82,7 +82,7 @@ std::string MessageField::GetRepeatedField()
 		{
 			for (int i = 0; i < reflection->FieldSize(message, field); ++i)
 			{
-				int64 value = reflection->GetInt64(message, field);
+				int64 value = reflection->GetUInt32(message, field);
 				valueStr += boost::lexical_cast<std::string>(value) + "\t";
 			}
 			break;
@@ -91,7 +91,7 @@ std::string MessageField::GetRepeatedField()
 		{
 			for (int i = 0; i < reflection->FieldSize(message, field); ++i)
 			{
-				int64 value = reflection->GetInt64(message, field);
+				int64 value = reflection->GetUInt64(message, field);
 				valueStr += boost::lexical_cast<std::string>(value) + "\t";
 			}
 			break;
@@ -101,7 +101,6 @@ std::string MessageField::GetRepeatedField()
 			valueStr += "'";
 			for (int i = 0; i < reflection->FieldSize(message, field); ++i)
 			{
-				int64 value = reflection->GetInt64(message, field);
 				valueStr += reflection->GetString(message, field) + "\t";
 			}
 			valueStr += "'";
@@ -112,7 +111,6 @@ std::string MessageField::GetRepeatedField()
 			valueStr += "'";
 			for (int i = 0; i < reflection->FieldSize(message, field); ++i)
 			{
-				int64 value = reflection->GetInt64(message, field);
 				valueStr += reflection->GetString(message, field) + "\t";
 			}
 			valueStr += "'";
@@ -142,6 +140,7 @@ std::string MessageField::GetOptionalField()
 	const google::protobuf::Reflection* reflection = message.GetReflection();
 	google::protobuf::FieldDescriptor::Type type = field->type();
 	std::string valueStr;
+	VLOG(2) << "FieldDescriptor::Type: " << type;
 	switch (type)
 	{
 		case google::protobuf::FieldDescriptor::TYPE_BOOL:
@@ -192,8 +191,8 @@ std::string MessageField::GetOptionalField()
 		}
 		case google::protobuf::FieldDescriptor::TYPE_MESSAGE:
 		{
-			const google::protobuf::Message& message = reflection->GetMessage(message, field);
-			valueStr = message.ShortDebugString();
+			const google::protobuf::Message& msg = reflection->GetMessage(message, field);
+			valueStr = msg.ShortDebugString();
 			break;
 		}
 		default:

+ 167 - 1
Cpp/Platform/Orm/MessageFieldTest.cc

@@ -1,8 +1,174 @@
 // Copyright 2011 Netease Inc. All Rights Reserved.
 // Author: tanghai@corp.netease.com (tanghai)
 
-#include "MessageField.h"
+#include <gtest/gtest.h>
+#include <glog/logging.h>
+#include <gflags/gflags.h>
+#include <google/protobuf/descriptor.h>
+#include "Orm/MessageField.h"
+#include "Orm/Person.pb.h"
 
 namespace Egametang {
 
+class MessageFieldTest: public testing::Test
+{
+};
+
+TEST_F(MessageFieldTest, GetField_FieldIsInt32)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(1);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_age(10);
+	str = messageField.GetField();
+	ASSERT_EQ("10", str);
+
+	person.set_age(-10);
+	str = messageField.GetField();
+	ASSERT_EQ("-10", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsUInt32)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(2);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_number(10);
+	str = messageField.GetField();
+	ASSERT_EQ("10", str);
+
+	person.set_number(-10);
+	str = messageField.GetField();
+	ASSERT_EQ("4294967286", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsUInt64)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(3);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_time(33333333333333LL);
+	str = messageField.GetField();
+	ASSERT_EQ("33333333333333", str);
+
+	person.set_time(-33333333333333LL);
+	str = messageField.GetField();
+	ASSERT_EQ("18446710740376218283", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsInt64)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(0);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_guid(33333333333333LL);
+	str = messageField.GetField();
+	ASSERT_EQ("33333333333333", str);
+
+	person.set_guid(-33333333333333LL);
+	str = messageField.GetField();
+	ASSERT_EQ("-33333333333333", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsString)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(4);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_name("tanghai");
+	str = messageField.GetField();
+	ASSERT_EQ("'tanghai'", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsDouble)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(5);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_height(1.78);
+	str = messageField.GetField();
+	ASSERT_EQ("1.78", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsBytes)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(6);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_comment("tanghai is a good student!");
+	str = messageField.GetField();
+	ASSERT_EQ("'tanghai is a good student!'", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsBool)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(7);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.set_marry(false);
+	str = messageField.GetField();
+	ASSERT_EQ("0", str);
+
+	person.set_marry(true);
+	str = messageField.GetField();
+	ASSERT_EQ("1", str);
+}
+
+TEST_F(MessageFieldTest, GetField_FieldIsMessage)
+{
+	Person person;
+	const google::protobuf::Descriptor* descriptor = person.GetDescriptor();
+	const google::protobuf::FieldDescriptor* field = descriptor->field(8);
+
+	std::string str;
+	MessageField messageField(person, field);
+
+	person.mutable_item()->set_id(123);
+	person.mutable_item()->set_name("pen");
+	str = messageField.GetField();
+	ASSERT_EQ("id: 123 name: \"pen\"", str);
+}
+
 } // namespace Egametang
+
+
+int main(int argc, char* argv[])
+{
+	testing::InitGoogleTest(&argc, argv);
+	google::InitGoogleLogging(argv[0]);
+	google::ParseCommandLineFlags(&argc, &argv, true);
+	return RUN_ALL_TESTS();
+}
+

+ 14 - 3
Cpp/Platform/Orm/Person.proto

@@ -1,8 +1,19 @@
 package Egametang;
 
-message Person
-{
+message Item {
 	optional int32 id = 1;
 	optional string name = 2;
-	optional int32 age = 3;
+}
+
+message Person
+{
+	optional int64 guid = 1;
+	optional int32 age = 2;
+	optional uint32 number = 3;
+	optional uint64 time = 4;
+	optional string name = 5;
+	optional double height = 6;
+	optional bytes comment = 7;
+	optional bool marry = 8;
+	optional Item item = 9;
 }

+ 36 - 18
Cpp/Platform/Orm/SelectTest.cc

@@ -8,47 +8,65 @@ namespace Egametang {
 
 class RpcServerTest: public testing::Test
 {
-public:
-
 };
 
-TEST_F(RpcServerTest, ToString)
+TEST_F(RpcServerTest, SelectFrom)
 {
 	std::string expectedSql;
 	expectedSql = "select * from Egametang.Person";
 	Select<Person> selectQuery1(Column("*"));
 	EXPECT_EQ(expectedSql, selectQuery1.ToString());
+}
 
-	Select<Person> selectQuery2(Column("*"));
+TEST_F(RpcServerTest, SelectWhere)
+{
+	std::string expectedSql;
+	Select<Person> selectQuery(Column("*"));
 	expectedSql = "select * from Egametang.Person where age > 10";
-	selectQuery2.Where(Column("age") > 10);
-	EXPECT_EQ(expectedSql, selectQuery2.ToString());
+	selectQuery.Where(Column("age") > 10);
+	EXPECT_EQ(expectedSql, selectQuery.ToString());
+}
 
-	Select<Person> selectQuery3(Column("*"));
+TEST_F(RpcServerTest, SelectDistinct)
+{
+	std::string expectedSql;
+	Select<Person> selectQuery(Column("*"));
 	expectedSql = "select * distinct from Egametang.Person where age > 10";
-	selectQuery3.Distinct().Where(Column("age") > 10);
-	EXPECT_EQ(expectedSql, selectQuery3.ToString());
+	selectQuery.Distinct().Where(Column("age") > 10);
+	EXPECT_EQ(expectedSql, selectQuery.ToString());
+}
 
-	Select<Person> selectQuery4(Column("age, name"));
+TEST_F(RpcServerTest, SelectTwoColumn)
+{
+	std::string expectedSql;
+	Select<Person> selectQuery(Column("age, name"));
 	expectedSql = "select age, name distinct from Egametang.Person where age > 10";
-	selectQuery4.Distinct().Where(Column("age") > 10);
-	EXPECT_EQ(expectedSql, selectQuery4.ToString());
+	selectQuery.Distinct().Where(Column("age") > 10);
+	EXPECT_EQ(expectedSql, selectQuery.ToString());
+}
 
-	Select<Person> selectQuery5(Column("age, name"));
+TEST_F(RpcServerTest, LimitOffset)
+{
+	std::string expectedSql;
+	Select<Person> selectQuery(Column("age, name"));
 	expectedSql = "select age, name distinct from Egametang.Person where age > 10 limit 1 offset 10";
-	selectQuery5.Distinct().Where(Column("age") > 10).Limit(1).Offset(10);
-	EXPECT_EQ(expectedSql, selectQuery5.ToString());
+	selectQuery.Distinct().Where(Column("age") > 10).Limit(1).Offset(10);
+	EXPECT_EQ(expectedSql, selectQuery.ToString());
+}
 
-	Select<Person> selectQuery6(Column("age, name"));
+TEST_F(RpcServerTest, GroupByHaving)
+{
+	std::string expectedSql;
+	Select<Person> selectQuery(Column("age, name"));
 	expectedSql =
 			"select age, name distinct from Egametang.Person"
 			" group by age having age > 10 limit 1 offset 10";
-	selectQuery6.Distinct()
+	selectQuery.Distinct()
 			.GroupBy(Column("age"))
 			.Having(Column("age") > 10)
 			.Limit(1)
 			.Offset(10);
-	EXPECT_EQ(expectedSql, selectQuery6.ToString());
+	EXPECT_EQ(expectedSql, selectQuery.ToString());
 }
 
 } // namespace Egametang