DataArray, DataObject pool 적용. 버그 수정. AGV 움직임 튀는 거 수정 필요
This commit is contained in:
161
Assets/Scripts/UVC/Tests/Data/DataArrayTests.cs
Normal file
161
Assets/Scripts/UVC/Tests/Data/DataArrayTests.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
#nullable enable
|
||||
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UVC.Data;
|
||||
|
||||
namespace UVC.Tests.Data
|
||||
{
|
||||
[TestFixture]
|
||||
public class DataArrayTests
|
||||
{
|
||||
private DataArray _dataArray = new DataArray();
|
||||
private DataObject _item1 = new DataObject();
|
||||
private DataObject _item2 = new DataObject();
|
||||
private DataObject _item3 = new DataObject();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 모든 테스트 메서드를 실행하는 메서드입니다.
|
||||
/// </summary>
|
||||
public void TestAll()
|
||||
{
|
||||
Debug.Log("===== DataArray 테스트 시작 =====");
|
||||
|
||||
SetUp();
|
||||
RunTest(nameof(Add_AddsItemToList), Add_AddsItemToList);
|
||||
SetUp();
|
||||
RunTest(nameof(Remove_RemovesItemFromList), Remove_RemovesItemFromList);
|
||||
SetUp();
|
||||
RunTest(nameof(Clear_RemovesAllItems), Clear_RemovesAllItems);
|
||||
SetUp();
|
||||
RunTest(nameof(UpdateDifferent_TracksAddsDeletesAndModifies), UpdateDifferent_TracksAddsDeletesAndModifies);
|
||||
SetUp();
|
||||
RunTest(nameof(ClearTrackedChanges_ResetsAllChangeLists), ClearTrackedChanges_ResetsAllChangeLists);
|
||||
|
||||
Debug.Log("===== DataArray 테스트 완료 =====");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 단일 테스트 메서드를 실행하고 결과를 로그로 출력합니다.
|
||||
/// </summary>
|
||||
private void RunTest(string testName, Action testAction)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.Log($"테스트 시작: {testName}");
|
||||
testAction();
|
||||
Debug.Log($"테스트 성공: {testName}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"테스트 실패: {testName}\n{ex.Message}\n{ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
// 테스트에 사용할 DataObject 아이템들을 초기화합니다.
|
||||
// UpdateDifferent 메서드가 Id를 기준으로 동작하므로 Id를 설정합니다.
|
||||
_item1 = new DataObject { { "id", "1" }, { "value", "A" } };
|
||||
_item2 = new DataObject { { "id", "2" }, { "value", "B" } };
|
||||
_item3 = new DataObject { { "id", "3" }, { "value", "C" } };
|
||||
|
||||
// 각 테스트 전에 DataArray를 초기화합니다.
|
||||
_dataArray = new DataArray(new[] { _item1, _item2 });
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_AddsItemToList()
|
||||
{
|
||||
// Act
|
||||
_dataArray.Add(_item3);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(3, _dataArray.Count, "DataArray의 전체 개수는 3이어야 합니다.");
|
||||
Assert.Contains(_item3, _dataArray.ToList());
|
||||
Assert.AreEqual(0, _dataArray.AddedItems.Count, "Add 메서드는 더 이상 AddedItems를 직접 채우지 않아야 합니다.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_RemovesItemFromList()
|
||||
{
|
||||
// Act
|
||||
bool result = _dataArray.Remove(_item1);
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result, "항목 제거는 성공해야 합니다.");
|
||||
Assert.AreEqual(1, _dataArray.Count, "DataArray의 전체 개수는 1이어야 합니다.");
|
||||
Assert.IsFalse(_dataArray.Contains(_item1));
|
||||
Assert.AreEqual(0, _dataArray.RemovedItems.Count, "Remove 메서드는 더 이상 RemovedItems를 직접 채우지 않아야 합니다.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Clear_RemovesAllItems()
|
||||
{
|
||||
// Act
|
||||
_dataArray.Clear();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, _dataArray.Count, "DataArray는 비어있어야 합니다.");
|
||||
Assert.AreEqual(0, _dataArray.RemovedItems.Count, "Clear 메서드는 더 이상 RemovedItems를 직접 채우지 않아야 합니다.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateDifferent_TracksAddsDeletesAndModifies()
|
||||
{
|
||||
// Arrange
|
||||
// 기존 항목 수정, 새 항목 추가를 포함하는 다른 DataArray 생성
|
||||
var item2Modified = new DataObject { { "id", "2" }, { "value", "B_modified" } };
|
||||
var item4New = new DataObject { { "id", "4" }, { "value", "D" } };
|
||||
var otherArray = new DataArray(new[] { item2Modified, item4New });
|
||||
|
||||
// Act
|
||||
_dataArray.UpdateDifferent(otherArray);
|
||||
|
||||
// Assert
|
||||
// 제거된 항목(_item1) 확인
|
||||
Assert.AreEqual(1, _dataArray.RemovedItems.Count, "제거된 항목이 1개여야 합니다.");
|
||||
Assert.AreEqual("1", _dataArray.RemovedItems[0].Id);
|
||||
|
||||
// 추가된 항목(item4New) 확인
|
||||
Assert.AreEqual(1, _dataArray.AddedItems.Count, "추가된 항목이 1개여야 합니다.");
|
||||
Assert.AreEqual("4", _dataArray.AddedItems[0].Id);
|
||||
|
||||
// 수정된 항목(_item2) 확인
|
||||
Assert.AreEqual(1, _dataArray.ModifiedList.Count, "수정된 항목이 1개여야 합니다.");
|
||||
Assert.AreEqual("2", _dataArray.ModifiedList[0].Id);
|
||||
|
||||
// 최종 컬렉션 상태 확인
|
||||
Assert.AreEqual(2, _dataArray.Count, "최종 컬렉션의 크기는 2여야 합니다.");
|
||||
Assert.IsFalse(_dataArray.Any(i => i.Id == "1"), "제거된 항목이 컬렉션에 없어야 합니다.");
|
||||
Assert.IsTrue(_dataArray.Any(i => i.Id == "4"), "추가된 항목이 컬렉션에 있어야 합니다.");
|
||||
|
||||
var updatedItem = _dataArray.First(i => i.Id == "2");
|
||||
Assert.AreEqual("B_modified", updatedItem.GetString("value"), "수정된 항목의 값이 반영되어야 합니다.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClearTrackedChanges_ResetsAllChangeLists()
|
||||
{
|
||||
// Arrange
|
||||
// UpdateDifferent를 호출하여 변경 내역을 생성합니다.
|
||||
var otherArray = new DataArray(new[] { _item3 });
|
||||
_dataArray.UpdateDifferent(otherArray);
|
||||
Assert.Greater(_dataArray.UpdatedCount, 0, "테스트 준비 단계에서 변경 내역이 있어야 합니다.");
|
||||
|
||||
// Act
|
||||
_dataArray.ClearTrackedChanges();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, _dataArray.UpdatedCount, "UpdatedCount는 0이어야 합니다.");
|
||||
Assert.IsEmpty(_dataArray.AddedItems, "AddedItems 리스트는 비어있어야 합니다.");
|
||||
Assert.IsEmpty(_dataArray.RemovedItems, "RemovedItems 리스트는 비어있어야 합니다.");
|
||||
Assert.IsEmpty(_dataArray.ModifiedList, "ModifiedList 리스트는 비어있어야 합니다.");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UVC/Tests/Data/DataArrayTests.cs.meta
Normal file
2
Assets/Scripts/UVC/Tests/Data/DataArrayTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b5f8fdb249f43b438e16aa4e70c8adf
|
||||
263
Assets/Scripts/UVC/Tests/Data/DataObjectTests.cs
Normal file
263
Assets/Scripts/UVC/Tests/Data/DataObjectTests.cs
Normal file
@@ -0,0 +1,263 @@
|
||||
#nullable enable
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UVC.Data;
|
||||
|
||||
namespace UVC.Tests.Data
|
||||
{
|
||||
[TestFixture]
|
||||
public class DataObjectTests
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 모든 테스트 메서드를 실행하는 메서드입니다.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 이 메서드는 클래스의 모든 테스트 메서드를 순차적으로 호출하고
|
||||
/// 각 테스트의 성공 또는 실패 여부를 로그로 출력합니다.
|
||||
/// </remarks>
|
||||
public void TestAll()
|
||||
{
|
||||
|
||||
Debug.Log("===== DataMapper 테스트 시작 =====");
|
||||
|
||||
SetUp();
|
||||
RunTest(nameof(UpdateDifferent_WhenCalled_UpdatesAndTracksChanges), UpdateDifferent_WhenCalled_UpdatesAndTracksChanges);
|
||||
SetUp();
|
||||
RunTest(nameof(Indexer_WhenPropertyIsSet_TracksChange), Indexer_WhenPropertyIsSet_TracksChange);
|
||||
SetUp();
|
||||
RunTest(nameof(Indexer_WhenNewPropertyIsAdded_TracksAsChange), Indexer_WhenNewPropertyIsAdded_TracksAsChange);
|
||||
SetUp();
|
||||
RunTest(nameof(GetUpdatedObject_WhenCalledAfterChanges_ReturnsOnlyUpdatedProperties), GetUpdatedObject_WhenCalledAfterChanges_ReturnsOnlyUpdatedProperties);
|
||||
SetUp();
|
||||
RunTest(nameof(MarkAllAsUpdated_WhenCalled_MarksAllPropertiesAsChanged), MarkAllAsUpdated_WhenCalled_MarksAllPropertiesAsChanged);
|
||||
SetUp();
|
||||
RunTest(nameof(Remove_WhenCalled_RemovesPropertyAndChangeTracking), Remove_WhenCalled_RemovesPropertyAndChangeTracking);
|
||||
SetUp();
|
||||
RunTest(nameof(GetTypeMethods_ReturnCorrectTypes), GetTypeMethods_ReturnCorrectTypes);
|
||||
SetUp();
|
||||
RunTest(nameof(Constructor_FromJObject_CreatesCorrectObject), Constructor_FromJObject_CreatesCorrectObject);
|
||||
SetUp();
|
||||
RunTest(nameof(IdProperty_ReturnsCorrectId), IdProperty_ReturnsCorrectId);
|
||||
SetUp();
|
||||
RunTest(nameof(KeyOrder_IsPreservedByInsertion), KeyOrder_IsPreservedByInsertion);
|
||||
SetUp();
|
||||
RunTest(nameof(DataObjectPool_GetAndReturn_WorksCorrectly), DataObjectPool_GetAndReturn_WorksCorrectly);
|
||||
|
||||
|
||||
Debug.Log("===== DataMapper 테스트 완료 =====");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 단일 테스트 메서드를 실행하고 결과를 로그로 출력합니다.
|
||||
/// </summary>
|
||||
/// <param name="testName">테스트 메서드 이름</param>
|
||||
/// <param name="testAction">실행할 테스트 메서드</param>
|
||||
private void RunTest(string testName, Action testAction)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.Log($"테스트 시작: {testName}");
|
||||
testAction();
|
||||
Debug.Log($"테스트 성공: {testName}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"테스트 실패: {testName}\n{ex.Message}\n{ex.StackTrace}");
|
||||
}
|
||||
}
|
||||
|
||||
private DataObject _dataObject = new DataObject();
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
// 각 테스트가 실행되기 전에 DataObject를 초기화합니다.
|
||||
_dataObject = new DataObject
|
||||
{
|
||||
{ "id", 1 },
|
||||
{ "name", "Test" },
|
||||
{ "value", 100.5f },
|
||||
{ "isActive", true }
|
||||
};
|
||||
// 초기 상태에서는 변경된 속성이 없어야 합니다.
|
||||
_dataObject.ClearChangedProperties(); // 변경 목록 초기화
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateDifferent_WhenCalled_UpdatesAndTracksChanges()
|
||||
{
|
||||
// Arrange
|
||||
var otherDataObject = new DataObject
|
||||
{
|
||||
{ "id", 1 }, // 동일한 값
|
||||
{ "name", "Updated Test" }, // 변경된 값
|
||||
{ "newValue", "new" } // 새로 추가된 값
|
||||
};
|
||||
|
||||
// Act
|
||||
_dataObject.UpdateDifferent(otherDataObject);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("Updated Test", _dataObject["name"]);
|
||||
Assert.AreEqual("new", _dataObject["newValue"]);
|
||||
Assert.AreEqual(2, _dataObject.UpdatedCount);
|
||||
Assert.Contains("name", _dataObject.ChangedProperies.ToList());
|
||||
Assert.Contains("newValue", _dataObject.ChangedProperies.ToList());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Indexer_WhenPropertyIsSet_TracksChange()
|
||||
{
|
||||
// Act
|
||||
_dataObject["name"] = "New Name";
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(1, _dataObject.UpdatedCount);
|
||||
Assert.Contains("name", _dataObject.ChangedProperies.ToList());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Indexer_WhenNewPropertyIsAdded_TracksAsChange()
|
||||
{
|
||||
// Act
|
||||
_dataObject["newKey"] = "newValue";
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(1, _dataObject.UpdatedCount, "새로 추가된 속성은 변경으로 간주되어야 합니다.");
|
||||
Assert.IsTrue(_dataObject.ChangedProperies.Contains("newKey"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetUpdatedObject_WhenCalledAfterChanges_ReturnsOnlyUpdatedProperties()
|
||||
{
|
||||
// Arrange
|
||||
_dataObject["name"] = "Updated Name";
|
||||
_dataObject["value"] = 200.0f;
|
||||
|
||||
// Act
|
||||
var updatedObject = _dataObject.GetUpdatedObject() as DataObject;
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(updatedObject);
|
||||
Assert.AreEqual(2, updatedObject.Count);
|
||||
Assert.AreEqual("Updated Name", updatedObject["name"]);
|
||||
Assert.AreEqual(200.0f, updatedObject["value"]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MarkAllAsUpdated_WhenCalled_MarksAllPropertiesAsChanged()
|
||||
{
|
||||
// Act
|
||||
_dataObject.MarkAllAsUpdated();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(_dataObject.Count, _dataObject.UpdatedCount);
|
||||
CollectionAssert.AreEquivalent(_dataObject.Keys, _dataObject.ChangedProperies.ToList());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_WhenCalled_RemovesPropertyAndChangeTracking()
|
||||
{
|
||||
// Arrange
|
||||
_dataObject["name"] = "A new name to be removed";
|
||||
Assert.Contains("name", _dataObject.ChangedProperies.ToList());
|
||||
|
||||
// Act
|
||||
bool result = _dataObject.Remove("name");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
Assert.IsFalse(_dataObject.ContainsKey("name"));
|
||||
Assert.IsFalse(_dataObject.ChangedProperies.Contains("name"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetTypeMethods_ReturnCorrectTypes()
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(1, _dataObject.GetInt("id"));
|
||||
Assert.AreEqual("Test", _dataObject.GetString("name"));
|
||||
Assert.AreEqual(100.5f, _dataObject.GetFloat("value"));
|
||||
Assert.IsTrue(_dataObject.GetBool("isActive"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Constructor_FromJObject_CreatesCorrectObject()
|
||||
{
|
||||
// Arrange
|
||||
var jObject = new JObject
|
||||
{
|
||||
{ "id", 10 },
|
||||
{ "user", "jobject_user" }
|
||||
};
|
||||
|
||||
// Act
|
||||
var dataObject = new DataObject(jObject);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(10, dataObject.GetInt("id"));
|
||||
Assert.AreEqual("jobject_user", dataObject.GetString("user"));
|
||||
Assert.AreEqual(0, dataObject.UpdatedCount, "생성자 호출 시에는 변경 추적을 하지 않아야 합니다.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IdProperty_ReturnsCorrectId()
|
||||
{
|
||||
// Arrange
|
||||
_dataObject.IdKey = "id";
|
||||
|
||||
// Act & Assert
|
||||
Assert.AreEqual("1", _dataObject.Id);
|
||||
|
||||
// Arrange
|
||||
var noIdKeyObject = new DataObject { { "name", "Test" }, { "value", 100 } };
|
||||
|
||||
// Act & Assert
|
||||
// IdKey가 설정되지 않은 경우 첫 번째 항목의 값을 Id로 사용해야 합니다.
|
||||
Assert.AreEqual("Test", noIdKeyObject.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void KeyOrder_IsPreservedByInsertion()
|
||||
{
|
||||
// Arrange
|
||||
var orderedDataObject = new DataObject();
|
||||
|
||||
// Act
|
||||
orderedDataObject.Add("first", 1);
|
||||
orderedDataObject.Add("second", 2);
|
||||
orderedDataObject.Add("third", 3);
|
||||
|
||||
// Assert
|
||||
var keys = orderedDataObject.Keys.ToList();
|
||||
Assert.AreEqual("first", keys[0]);
|
||||
Assert.AreEqual("second", keys[1]);
|
||||
Assert.AreEqual("third", keys[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DataObjectPool_GetAndReturn_WorksCorrectly()
|
||||
{
|
||||
// Act
|
||||
var obj = DataObjectPool.Get();
|
||||
obj["test"] = "value";
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(obj);
|
||||
Assert.AreEqual(1, obj.Count);
|
||||
|
||||
// Act
|
||||
DataObjectPool.Return(obj);
|
||||
var retrievedObj = DataObjectPool.Get();
|
||||
|
||||
// Assert
|
||||
// 풀에서 다시 가져온 객체는 비어 있어야 합니다.
|
||||
Assert.AreEqual(0, retrievedObj.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UVC/Tests/Data/DataObjectTests.cs.meta
Normal file
2
Assets/Scripts/UVC/Tests/Data/DataObjectTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ac90569e2576f04fbdf6394a36db8d8
|
||||
@@ -8,7 +8,9 @@ namespace UVC.Tests
|
||||
{
|
||||
//new DataMapperTests().TestAll();
|
||||
//new HttpPipeLineTests().TestAll();
|
||||
new MQTTPipeLineTests().TestAll();
|
||||
//new MQTTPipeLineTests().TestAll();
|
||||
//new DataObjectTests().TestAll();
|
||||
new DataArrayTests().TestAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user