forked from apache/arrow-adbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csharp/test): implement DuckDb test fixture (apache#1781)
Adds support for running tests against DuckDb and implements a simple smoke test.
- Loading branch information
1 parent
26bddc5
commit fa8c249
Showing
3 changed files
with
154 additions
and
0 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,72 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using Apache.Arrow.Adbc.C; | ||
|
||
namespace Apache.Arrow.Adbc.Tests | ||
{ | ||
public class DuckDbFixture : IDisposable | ||
{ | ||
readonly string _dataDirectory; | ||
AdbcDriver _driver; | ||
|
||
public DuckDbFixture() | ||
{ | ||
_dataDirectory = Path.Combine(Path.GetTempPath(), "AdbcTest_DuckDb", Guid.NewGuid().ToString("D")); | ||
Directory.CreateDirectory(_dataDirectory); | ||
|
||
string root = Directory.GetCurrentDirectory(); | ||
string file; | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
file = Path.Combine(root, "libduckdb.so"); | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
file = Path.Combine(root, "duckdb.dll"); | ||
else | ||
file = Path.Combine(root, "libduckdb.dylib"); | ||
|
||
_driver = CAdbcDriverImporter.Load(file, "duckdb_adbc_init"); | ||
} | ||
|
||
public AdbcDatabase OpenDatabase(string name) | ||
{ | ||
if (_driver == null) throw new ObjectDisposedException("DuckDbFixture"); | ||
|
||
return _driver.Open(new Dictionary<string, string> { { "path", Path.Combine(_dataDirectory, name) } }); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_driver != null) | ||
{ | ||
_driver.Dispose(); | ||
_driver = null; | ||
|
||
try | ||
{ | ||
Directory.Delete(_dataDirectory, true); | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
csharp/test/Apache.Arrow.Adbc.Tests/ImportedDuckDbTests.cs
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,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using Apache.Arrow.Types; | ||
using Xunit; | ||
|
||
namespace Apache.Arrow.Adbc.Tests | ||
{ | ||
public class ImportedDuckDbTests : IClassFixture<DuckDbFixture> | ||
{ | ||
readonly DuckDbFixture _duckDb; | ||
|
||
public ImportedDuckDbTests(DuckDbFixture duckDb) | ||
{ | ||
_duckDb = duckDb; | ||
} | ||
|
||
[Fact] | ||
public void SimpleEndToEndTest() | ||
{ | ||
using var database = _duckDb.OpenDatabase("test.db"); | ||
using var connection = database.Connect(null); | ||
using var statement = connection.CreateStatement(); | ||
|
||
statement.SqlQuery = "CREATE TABLE integers(foo INTEGER, bar INTEGER);"; | ||
statement.ExecuteUpdate(); | ||
|
||
statement.SqlQuery = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, 8);"; | ||
statement.ExecuteUpdate(); | ||
|
||
statement.SqlQuery = "SELECT * from integers"; | ||
var results = statement.ExecuteQuery(); | ||
|
||
using var stream = results.Stream; | ||
|
||
var schema = stream.Schema; | ||
Assert.Equal(2, schema.FieldsList.Count); | ||
Assert.Equal(ArrowTypeId.Int32, schema.FieldsList[0].DataType.TypeId); | ||
Assert.Equal(ArrowTypeId.Int32, schema.FieldsList[1].DataType.TypeId); | ||
|
||
var firstBatch = stream.ReadNextRecordBatchAsync().Result; | ||
Assert.Equal(3, firstBatch.Length); | ||
Assert.Equal(3, (firstBatch.Column(0) as Int32Array).Values[0]); | ||
Assert.Equal(5, (firstBatch.Column(0) as Int32Array).Values[1]); | ||
Assert.Equal(7, (firstBatch.Column(0) as Int32Array).Values[2]); | ||
|
||
var secondBatch = stream.ReadNextRecordBatchAsync().Result; | ||
Assert.Null(secondBatch); | ||
} | ||
} | ||
} |