272 lines
9.5 KiB
C#
272 lines
9.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using ln.bson.mapper;
|
|
using ln.bson.storage;
|
|
using Newtonsoft.Json;
|
|
using NUnit.Framework;
|
|
|
|
namespace ln.bson.tests
|
|
{
|
|
public class Tests
|
|
{
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
}
|
|
|
|
[Test]
|
|
public void Test_GetBytes()
|
|
{
|
|
BsonDocument bsonDocument = new BsonDocument();
|
|
bsonDocument["alpha"] = new BsonString("Hallo Welt");
|
|
bsonDocument["beta"] = new BsonString("");
|
|
bsonDocument["gamma"] = new BsonInt32(int.MaxValue);
|
|
bsonDocument["delta"] = new BsonDateTime(DateTime.Now);
|
|
BsonDocument embeddedDocument = new BsonDocument();
|
|
bsonDocument["zeta"] = embeddedDocument;
|
|
BsonArray bsonArray = new BsonArray();
|
|
bsonArray.Add(bsonDocument["alpha"]);
|
|
bsonArray.Add(bsonDocument["beta"]);
|
|
bsonArray.Add(bsonDocument["gamma"]);
|
|
bsonDocument["eta"] = bsonArray;
|
|
embeddedDocument["alpha"] = bsonArray;
|
|
|
|
bsonDocument["phi"] = new BsonGuid(Guid.NewGuid());
|
|
|
|
byte[] documentBytes = bsonDocument.GetBytes();
|
|
BsonDocument compareDocument = BsonReader.ReadDocument(documentBytes);
|
|
byte[] compareBytes = compareDocument.GetBytes();
|
|
|
|
Assert.AreEqual(documentBytes, compareBytes);
|
|
Assert.Pass();
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void Test_Mapper()
|
|
{
|
|
BsonMapping bsonMapping = BsonMapper.DefaultInstance.CreateMapping<MapperTestA>();
|
|
Assert.NotNull(bsonMapping);
|
|
|
|
MapperTestA testA = new MapperTestA(true) { text = "jetzt bin ich ein anderer text" };
|
|
|
|
BsonDocument mappedDocument = BsonMapper.DefaultInstance.Map(testA);
|
|
|
|
MapperTestA testA2 = BsonMapper.DefaultInstance.Unmap<MapperTestA>(mappedDocument);
|
|
|
|
FieldsAreEqual(testA, testA2);
|
|
|
|
Document document = new Document();
|
|
document.Keywords.Add("supplier", "123-4567-890-AAA");
|
|
document.Keywords.Add("DocType", "supplier-invoice");
|
|
//document.DocumentContents.Add("application/pdf", new DocumentContent(){ Authentic = true, Created = DateTime.Now });
|
|
|
|
BsonDocument bsonDocument = BsonMapper.DefaultInstance.Map(document);
|
|
Document unmappedDocument = BsonMapper.DefaultInstance.Unmap<Document>(bsonDocument);
|
|
|
|
FieldsAreEqual(document, unmappedDocument);
|
|
|
|
Assert.Pass();
|
|
}
|
|
|
|
[Test]
|
|
public void Test_FileStorage()
|
|
{
|
|
BsonDocument[] bsonDocuments = PrepareDocuments(256);
|
|
int n = 0;
|
|
|
|
File.Delete("test1.bson");
|
|
using (ByteArrayFileStorage byteArrayFileStorage = new ByteArrayFileStorage("test1.bson"))
|
|
{
|
|
foreach (BsonDocument bsonDocument in bsonDocuments)
|
|
Assert.IsTrue(byteArrayFileStorage.TrySaveDocumentBytes(bsonDocument.GetBytes(), out long documentOffset));
|
|
|
|
foreach (KeyValuePair<long,byte[]> storageData in byteArrayFileStorage)
|
|
{
|
|
Assert.AreEqual(bsonDocuments[n].GetBytes(), storageData.Value);
|
|
n++;
|
|
}
|
|
|
|
n = 0;
|
|
foreach (KeyValuePair<long,byte[]> storageData in byteArrayFileStorage.ToArray())
|
|
{
|
|
Assert.IsTrue(byteArrayFileStorage.Remove(storageData.Key));
|
|
|
|
n++;
|
|
if (n >= 64)
|
|
break;
|
|
}
|
|
|
|
n = 64;
|
|
foreach (KeyValuePair<long,byte[]> storageData in byteArrayFileStorage)
|
|
{
|
|
Assert.AreEqual(bsonDocuments[n].GetBytes(), storageData.Value);
|
|
n++;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
[Test]
|
|
public void Test_DocumentStorage()
|
|
{
|
|
BsonDocument[] bsonDocuments = PrepareDocuments(256);
|
|
int n = 0;
|
|
|
|
File.Delete("documents.bson");
|
|
using (BsonDocumentStorage storage = new BsonDocumentStorage(new BsonDocumentStorageConfiguration("documents.bson")))
|
|
{
|
|
foreach (BsonDocument bsonDocument in bsonDocuments)
|
|
Assert.IsTrue(storage.SaveDocument(BsonReader.ReadDocument(bsonDocument.GetBytes())));
|
|
|
|
foreach (BsonDocument bsonDocument in storage)
|
|
{
|
|
Assert.AreEqual(bsonDocuments[n], bsonDocument);
|
|
Assert.AreNotSame(bsonDocuments[n], bsonDocument);
|
|
n++;
|
|
}
|
|
|
|
n = 0;
|
|
foreach (BsonDocument bsonDocument in storage.ToArray())
|
|
{
|
|
Assert.IsTrue(storage.DeleteDocument(bsonDocument));
|
|
|
|
n++;
|
|
if (n >= 64)
|
|
break;
|
|
}
|
|
|
|
n = 64;
|
|
foreach (BsonDocument bsonDocument in storage)
|
|
{
|
|
Assert.AreEqual(bsonDocuments[n], bsonDocument);
|
|
Assert.AreNotSame(bsonDocuments[n], bsonDocument);
|
|
n++;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
[Test]
|
|
public void Test_FileMapper()
|
|
{
|
|
Document[] documents = new Document[64];
|
|
for (int n = 0; n < documents.Length; n++)
|
|
documents[n] = new Document()
|
|
{ UniqueId = Guid.NewGuid(), FolderId = Guid.NewGuid(), FileName = string.Format("{0}.txt", n) };
|
|
|
|
File.Delete("test.documents.bson");
|
|
using (BsonFileMapper<Document> fileMapper = new BsonFileMapper<Document>("test.documents.bson"))
|
|
{
|
|
foreach (var document in documents)
|
|
fileMapper.Save(document);
|
|
|
|
int n = 0;
|
|
foreach (Document document in fileMapper)
|
|
{
|
|
Assert.AreEqual(documents[n].UniqueId, document.UniqueId);
|
|
Assert.AreEqual(documents[n].FolderId, document.FolderId);
|
|
Assert.AreEqual(documents[n].FileName, document.FileName);
|
|
n++;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
*/
|
|
|
|
[Test]
|
|
public void Test_Indeces()
|
|
{
|
|
BsonDocument[] bsonDocuments = PrepareDocuments(256);
|
|
int n = 0;
|
|
|
|
File.Delete("documents.bson");
|
|
using (BsonDocumentStorage storage = new BsonDocumentStorage(
|
|
new BsonDocumentStorageConfiguration("documents.bson")
|
|
.Index("__NO__")
|
|
.SetLogTarget(Console.WriteLine)
|
|
))
|
|
{
|
|
foreach (BsonDocument bsonDocument in bsonDocuments)
|
|
Assert.IsTrue(storage.SaveDocument(BsonReader.ReadDocument(bsonDocument.GetBytes())));
|
|
}
|
|
|
|
using (BsonDocumentStorage storage = new BsonDocumentStorage(
|
|
new BsonDocumentStorageConfiguration("documents.bson")
|
|
.Index("__NO__")
|
|
.SetLogTarget(Console.WriteLine)
|
|
))
|
|
{
|
|
var e = storage.Query("__NO__", QueryOperator.LESS, 10);
|
|
|
|
Assert.AreEqual(
|
|
10,
|
|
e.Count()
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public BsonDocument[] PrepareDocuments(int count)
|
|
{
|
|
BsonDocument[] documents = new BsonDocument[count];
|
|
for (int n = 0; n < documents.Length; n++)
|
|
{
|
|
documents[n] = new BsonDocument();
|
|
documents[n]["__NO__"] = new BsonInt32(n);
|
|
documents[n]["__UUID__"] = Guid.NewGuid();
|
|
}
|
|
|
|
return documents;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void FieldsAreEqual<T>(T a, T b)
|
|
{
|
|
foreach (FieldInfo fieldInfo in typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public |
|
|
BindingFlags.NonPublic))
|
|
{
|
|
if (fieldInfo.FieldType.IsAssignableTo(typeof(ICollection)))
|
|
CollectionAssert.AreEquivalent((ICollection)fieldInfo.GetValue(a), (ICollection)fieldInfo.GetValue(b));
|
|
else
|
|
Assert.AreEqual(fieldInfo.GetValue(a), fieldInfo.GetValue(b));
|
|
}
|
|
}
|
|
|
|
class MapperTestA
|
|
{
|
|
public string text = "ich bin ein text";
|
|
public int i32 = Int32.MinValue;
|
|
public long i64 = Int64.MaxValue;
|
|
public double d64 = Double.MaxValue;
|
|
|
|
public Dictionary<string, string> keywords = new Dictionary<string, string>();
|
|
|
|
public MapperTestA(){}
|
|
public MapperTestA(bool populateKeywords)
|
|
{
|
|
if (populateKeywords)
|
|
{
|
|
keywords.Add("keyword1", "some-value");
|
|
keywords.Add("keyword2", "another-value");
|
|
keywords.Add("keyword3", "some text that you would expect to be part of good test scenario");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |