Skip to content

Getting Started

Aryeh Citron edited this page May 12, 2026 · 2 revisions

Getting Started

Installation

dotnet add package InMemoryEmulator.BigQuery

Requirements:

  • .NET 8.0 or later
  • Google.Cloud.BigQuery.V2 SDK 3.x or later

Quick Start

Direct Instantiation

The simplest way to use the emulator — create an in-memory instance directly:

using InMemoryEmulator.BigQuery;
using Google.Cloud.BigQuery.V2;
using Google.Apis.Bigquery.v2.Data;

// Create an in-memory BigQuery with a dataset and table
using var bq = InMemoryBigQuery.Create("test-project", "my_dataset", ds =>
{
    ds.AddTable("users", new TableSchema
    {
        Fields = new List<TableFieldSchema>
        {
            new() { Name = "id", Type = "INTEGER", Mode = "REQUIRED" },
            new() { Name = "name", Type = "STRING", Mode = "NULLABLE" },
            new() { Name = "score", Type = "FLOAT", Mode = "NULLABLE" },
        }
    });
});

// Use the real BigQueryClient
var client = bq.Client;

// Insert rows
await client.InsertRowsAsync("my_dataset", "users", new[]
{
    new BigQueryInsertRow { ["id"] = 1, ["name"] = "Alice", ["score"] = 95.5 },
    new BigQueryInsertRow { ["id"] = 2, ["name"] = "Bob", ["score"] = 87.3 },
});

// Query with parameters
var results = await client.ExecuteQueryAsync(
    "SELECT * FROM my_dataset.users WHERE score > @threshold",
    new[] { new BigQueryParameter("threshold", BigQueryDbType.Float64, 90.0) });

foreach (var row in results)
{
    Console.WriteLine($"{row["name"]}: {row["score"]}");
}
// Output: Alice: 95.5

DI Integration (WebApplicationFactory)

For ASP.NET Core integration tests:

public class MyApiTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly WebApplicationFactory<Program> _factory;

    public MyApiTests(WebApplicationFactory<Program> factory)
    {
        _factory = factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureTestServices(services =>
            {
                services.UseInMemoryBigQuery(options =>
                {
                    options.ProjectId = "test-project";
                    options.AddDataset("my_dataset", ds =>
                    {
                        ds.AddTable("users", schema);
                    });
                });
            });
        });
    }

    [Fact]
    public async Task GetUsers_ReturnsData()
    {
        var client = _factory.CreateClient();
        var response = await client.GetAsync("/api/users");
        response.EnsureSuccessStatusCode();
    }
}

Seeding Test Data

Use the builder callback to pre-create tables, then insert data via the SDK:

using var bq = InMemoryBigQuery.Create("test-project", "my_dataset", ds =>
{
    ds.AddTable("users", schema);
});

// Seed rows using the SDK
await bq.Client.InsertRowsAsync("my_dataset", "users", testRows);

Or use the InMemoryBigQueryBuilder for complex setups:

using var bq = InMemoryBigQuery.Builder()
    .WithProjectId("test-project")
    .AddDataset("dataset1", ds => ds.AddTable("table1", schema1))
    .AddDataset("dataset2", ds => ds.AddTable("table2", schema2))
    .Build();

Clone this wiki locally