-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checkpoint for SqliteFileSystem implementation
Work being done for #120
- Loading branch information
Showing
10 changed files
with
331 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Acoustics.Test.SqliteFileSystem | ||
{ | ||
using System.Diagnostics; | ||
using System.IO; | ||
using global::SqliteFileSystem; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TestHelpers; | ||
using Zio.FileSystems; | ||
using Zio.FileSystems.Additional; | ||
|
||
[TestClass] | ||
public class AdapterTests | ||
{ | ||
|
||
protected DirectoryInfo outputDirectory; | ||
private FileInfo testFile; | ||
private System.Random random; | ||
private byte[] sampleBlob; | ||
|
||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
this.testFile = PathHelper.GetTempFile("sqlite3"); | ||
this.outputDirectory = this.testFile.Directory; | ||
this.random = Random.GetRandom(); | ||
this.sampleBlob = new byte[1024]; | ||
|
||
this.random.NextBytes(this.sampleBlob); | ||
} | ||
|
||
[TestCleanup] | ||
public void Cleanup() | ||
{ | ||
Debug.WriteLine("Deleting output directory:" + this.outputDirectory.FullName); | ||
|
||
PathHelper.DeleteTempDir(this.outputDirectory); | ||
} | ||
|
||
[TestMethod] | ||
public void AdapterCanStreamBlobs() | ||
{ | ||
// create a new empty database - mainly doing this to get a schema | ||
using (var fs = new SqliteFileSystem(this.testFile.FullName, SqliteOpenMode.ReadWriteCreate)) | ||
{ | ||
} | ||
|
||
var connectionString = $"Data source='{this.testFile.FullName}';Mode={SqliteOpenMode.ReadWrite}"; | ||
using (var connection = new SqliteConnection(connectionString)) | ||
{ | ||
connection.Open(); | ||
|
||
// add a blob we can read | ||
|
||
using (var command = new SqliteCommand("INSERT INTO files VALUES ('/test.blob', @blob)")) | ||
{ | ||
var parameter = new SqliteParameter("blob", SqliteType.Blob) { Value = this.sampleBlob }; | ||
command.Parameters.Add(parameter); | ||
|
||
command.ExecuteNonQuery(); | ||
} | ||
|
||
|
||
// verify we can stream the response back | ||
byte[] result; | ||
using (var readStream = Adapter.ExecuteNonQueryStream(connection, "SELECT blob FROM files LIMIT 1")) | ||
{ | ||
// make sure we can read, get length, can't write | ||
Assert.AreEqual(1024, readStream.Length); | ||
Assert.IsTrue(readStream.CanRead); | ||
Assert.IsFalse(readStream.CanWrite); | ||
Assert.IsTrue(readStream.CanSeek); | ||
|
||
// read the result in tiny chunks | ||
result = new byte[readStream.Length]; | ||
var buffer = new byte[128]; | ||
int offset = 0; | ||
while (readStream.Read(buffer, offset, 128) > 0) | ||
{ | ||
buffer.CopyTo(result, offset); | ||
offset += 128; | ||
} | ||
} | ||
|
||
// finally make sure the blob was read back accurately | ||
CollectionAssert.AreEqual(this.sampleBlob, result); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Alexandre Mutel. All rights reserved. | ||
// This file is licensed under the BSD-Clause 2 license. | ||
// See the license.txt file in the project root for more information. | ||
|
||
using System.IO; | ||
|
||
namespace SqliteFileSystem | ||
{ | ||
/// <summary> | ||
/// Lifted from https://raw.githubusercontent.com/xoofx/zio/78b66d29c857b450e495c31b38f7ed4021ebec8e/src/Zio/FileSystemExceptionHelper.cs | ||
/// so excpetions are formatted in a similar manner. | ||
/// </summary> | ||
internal static class FileSystemExceptionHelper | ||
{ | ||
public static FileNotFoundException NewFileNotFoundException(UPath path) | ||
{ | ||
return new FileNotFoundException($"Could not find file `{path}`."); | ||
} | ||
|
||
public static DirectoryNotFoundException NewDirectoryNotFoundException(UPath path) | ||
{ | ||
return new DirectoryNotFoundException($"Could not find a part of the path `{path}`."); | ||
} | ||
|
||
public static IOException NewDestinationDirectoryExistException(UPath path) | ||
{ | ||
return new IOException($"The destination path `{path}` is an existing directory"); | ||
} | ||
|
||
public static IOException NewDestinationFileExistException(UPath path) | ||
{ | ||
return new IOException($"The destination path `{path}` is an existing file"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// Note: the attributes normally in this file are instead stored in the .csproj file for .NET core projects | ||
|
||
[assembly: InternalsVisibleTo("Acoustics.Test")] |
Oops, something went wrong.