TM1 Testcontainers · Walkthrough 1 of 1
Test your code against a real TM1
You write tests that run against a real TM1 server in Docker. The server starts once for the test run, and the tests skip, rather than fail, where Docker or TM1 isn't there.
Before you start
- Docker, running on the computer that runs the tests.
- Your own licensed TM1 Server. No IBM software comes with this library. The quickest source is IBM's download,
tm1server-*-linuxi38664h_bundle.tar.gz.
Steps
Add it to your tests.
Needs Java 17 or later.
Gradle (Kotlin)
testImplementation("io.skipcheck:tm1-testcontainers:0.1.1")Maven
<dependency> <groupId>io.skipcheck</groupId> <artifactId>tm1-testcontainers</artifactId> <version>0.1.1</version> <scope>test</scope> </dependency>Needs Python 3.11 or later.
Terminal
pip install skipcheck-tm1-testcontainers==0.1.1Needs .NET 10.
Terminal
dotnet add package Skipcheck.Tm1.Testcontainers --version 0.1.1Needs Node 22.22 or later.
Terminal
npm install --save-dev @skipcheck/tm1-testcontainers@0.1.1- Tell it where TM1 is. Set
TM1_BUNDLE_DIRto the folder that holds IBM's download, then run your tests as usual. The first start installs TM1 from it into a Docker volume, which takes a few minutes; later starts are fast. Tm1Source lists the other sources, such as an installed TM1 (TM1_HOME_DIR) or a private image. Write a test. Ask for the server, and connect to it with its address and login:
@RequiresTm1 static class HelloTm1Test { @Test void connects(Tm1ServerContainer server) { Tm1Client tm1 = Tm1Client.builder() .baseUrl(server.getRestUrl()) .basicAuth(server.getUsername(), server.getPassword()) .build(); try { assertNotNull(tm1.session().version().block(Duration.ofSeconds(30))); } finally { tm1.close().block(Duration.ofSeconds(30)); } } }# The tm1_server fixture is the shared TM1 server; without one configured, the test is skipped @pytest.mark.requires_tm1 def test_connects(tm1_server: Tm1ServerContainer) -> None: builder = Tm1Client.builder().base_url(tm1_server.rest_url).basic_auth(tm1_server.username, tm1_server.password) with builder.build() as tm1: assert tm1.session.version() is not Nonepublic sealed class HelloTm1Tests(Tm1ServerFixture server) : IClassFixture<Tm1ServerFixture> { [RequiresTm1Fact] public async Task Connects() { await using var tm1 = server.Client(); Assert.NotNull(await tm1.Session.VersionAsync()); } }import { Tm1Client } from "@skipcheck/tm1-client"; import { requiresTm1 } from "@skipcheck/tm1-testcontainers/vitest"; import { expect } from "vitest"; // Skipped when there's no Docker or no TM1 source; "tm1" is the shared server requiresTm1("connects", async ({ tm1 }) => { await using client = Tm1Client.builder().baseUrl(tm1.getRestUrl()).basicAuth(tm1.getUsername(), tm1.getPassword()).build(); expect(await client.session.version()).toBeDefined(); });Make what a test needs, and clean up. Every test in the run shares the server, so give your objects names no other test uses, and delete them afterwards:
@RequiresTm1 static class RegionDimensionTest { private static final Duration WAIT = Duration.ofSeconds(30); private static Tm1Client tm1; // One client per class, on the shared server @BeforeAll static void connect(Tm1ServerContainer server) { tm1 = Tm1Client.builder() .baseUrl(server.getRestUrl()) .basicAuth(server.getUsername(), server.getPassword()) .build(); } @AfterAll static void close() { tm1.close().block(WAIT); } // The server is shared, so use a unique name and delete it afterwards private final String region = "Region-" + UUID.randomUUID(); @AfterEach void deleteRegion() { tm1.dimensions().delete(region).onErrorComplete().block(WAIT); } @Test void createsTheRegions() { Dimension created = tm1.dimensions() .create(DimensionPatch.builder() .name(region) .hierarchies(List.of(HierarchyPatch.builder() .name(region) .elements(List.of(ElementPatch.builder().name("North").type(ElementType.NUMERIC).build(), ElementPatch.builder().name("South").type(ElementType.NUMERIC).build())) .build())) .build()) .block(WAIT); assertEquals(region, created.name()); } }# Every test in the module needs TM1, and is skipped without one pytestmark = pytest.mark.requires_tm1 # One client per module, on the shared server @pytest.fixture(scope="module") def tm1(tm1_server: Tm1ServerContainer) -> Iterator[Tm1Client]: with Tm1Client.builder().base_url(tm1_server.rest_url).basic_auth(tm1_server.username, tm1_server.password).build() as client: yield client # The server is shared, so use a unique name and delete it afterwards @pytest.fixture def region(tm1: Tm1Client) -> Iterator[str]: name = f"Region-{uuid.uuid4()}" yield name try: tm1.dimensions.delete(name) except Tm1Error: pass def test_creates_the_regions(tm1: Tm1Client, region: str) -> None: north = ElementPatch.builder().name("North").type(ElementType.NUMERIC).build() south = ElementPatch.builder().name("South").type(ElementType.NUMERIC).build() hierarchy = HierarchyPatch.builder().name(region).elements([north, south]).build() created = tm1.dimensions.create(DimensionPatch.builder().name(region).hierarchies([hierarchy]).build()) assert created.name == region// Every class with this fixture gets the same server, started once for the run public sealed class RegionDimensionTests(Tm1ServerFixture server) : IClassFixture<Tm1ServerFixture> { [RequiresTm1Fact] public async Task CreatesTheRegions() { await using var tm1 = server.Client(); // The server is shared, so use a unique name and delete it afterwards var region = "Region-" + Guid.NewGuid(); try { var created = await tm1.Dimensions.CreateAsync( DimensionPatch .Builder() .Name(region) .Hierarchies([ HierarchyPatch .Builder() .Name(region) .Elements([ ElementPatch.Builder().Name("North").Type(ElementType.Numeric).Build(), ElementPatch.Builder().Name("South").Type(ElementType.Numeric).Build(), ]) .Build(), ]) .Build() ); Assert.Equal(region, created.Name); } finally { try { await tm1.Dimensions.DeleteAsync(region); } catch (Tm1NotFoundException) { // Never created } } } }describe("the Region dimension", async () => { // Setup for the whole file needs the server too, so skip it as the tests are skipped const skipped = (await RequiresTm1.skipReason()) !== undefined; let tm1: Tm1Client; // One client per file, on the shared server beforeAll(async () => { if (skipped) return; const server = await Tm1ServerContainer.shared(); tm1 = Tm1Client.builder().baseUrl(server.getRestUrl()).basicAuth(server.getUsername(), server.getPassword()).build(); }); afterAll(async () => { await tm1?.close(); }); // The server is shared, so use a unique name and delete it afterwards const region = "Region-" + randomUUID(); afterEach(async () => { await tm1?.dimensions.delete(region).catch(() => undefined); }); requiresTm1("creates the regions", async () => { const elements = ["North", "South"].map((name) => ElementPatch.builder().name(name).type(ElementType.Numeric).build()); const hierarchy = HierarchyPatch.builder().name(region).elements(elements).build(); const created = await tm1.dimensions.create(DimensionPatch.builder().name(region).hierarchies([hierarchy]).build()); expect(created.Name).toBe(region); }); });Test against sample data. For a test that reads a model, start a server of its own with IBM's SData sample. It's stopped when the block ends:
Tm1Source source = Tm1Source.discover().orElseThrow(); // SData's login is admin / apple, and the server must be this version try (var server = new Tm1ServerContainer(source).withSeed(Tm1Seed.SDATA) .withExpectedVersion(Tm1Version.parse("11.8.03500.6"))) { server.start(); Tm1Client tm1 = Tm1Client.builder() .baseUrl(server.getRestUrl()) .basicAuth(server.getUsername(), server.getPassword()) .build(); try { // SData's cubes, such as SalesCube return tm1.cubes() .modelCubes(ODataQuery.none().select(Cube.NAME)) .map(Cube::name) .collectList() .block(WAIT); } finally { tm1.close().block(WAIT); // before the container stops } }source = Tm1Source.discover() if source is None: raise RuntimeError("Set TM1_HOME_DIR or TM1_BUNDLE_DIR") # SData's login is admin / apple, and the server must be this version container = Tm1ServerContainer(source).with_seed(Tm1Seed.SDATA).with_expected_version(Tm1Version.parse("11.8.03500.6")) # Entering the block starts the container, and leaving it stops it with container as server: # The client's block is the inner one, so its session closes before the container stops with Tm1Client.builder().base_url(server.rest_url).basic_auth(server.username, server.password).build() as tm1: # SData's cubes, such as SalesCube return [cube.name for cube in tm1.cubes.model_cubes(ODataQuery.none().select(Cube.NAME))]var source = Tm1Source.Discover() ?? throw new InvalidOperationException("Set TM1_HOME_DIR or TM1_BUNDLE_DIR"); // SData's login is admin / apple, and the server must be this version await using var server = new Tm1ServerContainer(source).WithSeed(Tm1Seed.Sdata).WithExpectedVersion(Tm1Version.Parse("11.8.03500.6")); await server.StartAsync(); // Declared after the server, so the client closes before the container stops await using var tm1 = Tm1Client.Builder().BaseUrl(server.RestUrl).BasicAuth(server.Username, server.Password).Build(); // SData's cubes, such as SalesCube return await tm1.Cubes.ModelCubesAsync(ODataQuery.None().Select(Cube.Fields.Name)).Select(cube => cube.Name!).ToListAsync();const source = Tm1Source.discover(); if (!source) throw new Error("Set TM1_HOME_DIR or TM1_BUNDLE_DIR"); // SData's login is admin / apple, and the server must be this version await using server = new Tm1ServerContainer(source).withSeed(Tm1Seed.SDATA).withExpectedVersion(Tm1Version.parse("11.8.03500.6")); await server.start(); // Declared after the server, so it's closed before the container stops await using tm1 = Tm1Client.builder().baseUrl(server.getRestUrl()).basicAuth(server.getUsername(), server.getPassword()).build(); // SData's cubes, such as SalesCube const cubes = await tm1.cubes.modelCubes(ODataQuery.none().select(Cube.Fields.Name)).toArray(); return cubes.map((cube) => cube.Name!);
Next
- Skip a test on servers older than a feature: RequiresTm1.
- Start TM1 with your own database: Tm1ServerContainer.
- Test with TM1 Web, Workspace or Cognos security: whole environments.