Skip to content

Commit

Permalink
Initial cut as making postgres testing work
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydtabb committed Dec 28, 2021
1 parent 12694b9 commit b8649f0
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 54 deletions.
5 changes: 5 additions & 0 deletions cloudbuild/build-test/build-test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env sh
set -euxo pipefail


nix-shell --pure --command "$(cat <<NIXCMD
export PGHOST=localhost
cd postgres_test
./postgres_init.sh
cd ..
yarn install
yarn build
yarn test
Expand Down
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
with import <nixpkgs> {}; stdenv.mkDerivation { name = "malloy"; buildInputs = [ nodejs-16_x yarn jdk8 google-cloud-sdk ]; }
with import <nixpkgs> {}; stdenv.mkDerivation { name = "malloy"; buildInputs = [ nodejs-16_x yarn jdk8 google-cloud-sdk postgresql]; }
114 changes: 61 additions & 53 deletions packages/malloy-db-test/src/expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import * as malloy from "@malloy-lang/malloy";
import { getRuntimes } from "./runtimes";

// const runtimes = getRuntimes(["bigquery"]);
// const runtimes = getRuntimes(["postgres"]);
const runtimes = getRuntimes();
const runtimes = getRuntimes(["postgres", "bigquery"]);
// const runtimes = getRuntimes();

const expressionModelText = `
export define aircraft_models is (explore 'malloytest.aircraft_models'
Expand Down Expand Up @@ -252,10 +252,12 @@ expressionModels.forEach((expressionModel, databaseName) => {
expect(result.data.path(0, "boeing_max_model").value).toBe("YL-15");
});

it(`model: dates - ${databaseName}`, async () => {
const result = await expressionModel
.loadQuery(
`
(databaseName === "postgres" ? it.skip : it)(
`model: dates - ${databaseName}`,
async () => {
const result = await expressionModel
.loadQuery(
`
explore 'malloytest.alltypes' | reduce
t_date,
t_date.\`month\`,
Expand All @@ -273,41 +275,44 @@ expressionModels.forEach((expressionModel, databaseName) => {
t_timestamp.day_of_year,
`
)
.run();
expect(result.data.path(0, "t_date").value).toEqual(new Date("2020-03-02"));
expect(result.data.path(0, "t_date_month").value).toEqual(
new Date("2020-03-01")
);
expect(result.data.path(0, "t_date_year").value).toEqual(
new Date("2020-01-01")
);
expect(result.data.path(0, "t_date_day_of_year").value).toEqual(62);
expect(result.data.path(0, "t_date_day_of_month").value).toEqual(2);
expect(result.data.path(0, "t_timestamp").value).toEqual(
new Date("2020-03-02T12:35:56.000Z")
);
expect(result.data.path(0, "t_timestamp_second").value).toEqual(
new Date("2020-03-02T12:35:56.000Z")
);
expect(result.data.path(0, "t_timestamp_minute").value).toEqual(
new Date("2020-03-02T12:35:00.000Z")
);
expect(result.data.path(0, "t_timestamp_hour").value).toEqual(
new Date("2020-03-02T12:00:00.000Z")
);
expect(result.data.path(0, "t_timestamp_date").value).toEqual(
new Date("2020-03-02")
);
expect(result.data.path(0, "t_timestamp_month").value).toEqual(
new Date("2020-03-01")
);
expect(result.data.path(0, "t_timestamp_year").value).toEqual(
new Date("2020-01-01")
);
expect(result.data.path(0, "t_timestamp_day_of_year").value).toEqual(62);
expect(result.data.path(0, "t_timestamp_day_of_month").value).toEqual(2);
});
)
.run();
expect(result.data.path(0, "t_date").value).toEqual(
new Date("2020-03-02")
);
expect(result.data.path(0, "t_date_month").value).toEqual(
new Date("2020-03-01")
);
expect(result.data.path(0, "t_date_year").value).toEqual(
new Date("2020-01-01")
);
expect(result.data.path(0, "t_date_day_of_year").value).toEqual(62);
expect(result.data.path(0, "t_date_day_of_month").value).toEqual(2);
expect(result.data.path(0, "t_timestamp").value).toEqual(
new Date("2020-03-02T12:35:56.000Z")
);
expect(result.data.path(0, "t_timestamp_second").value).toEqual(
new Date("2020-03-02T12:35:56.000Z")
);
expect(result.data.path(0, "t_timestamp_minute").value).toEqual(
new Date("2020-03-02T12:35:00.000Z")
);
expect(result.data.path(0, "t_timestamp_hour").value).toEqual(
new Date("2020-03-02T12:00:00.000Z")
);
expect(result.data.path(0, "t_timestamp_date").value).toEqual(
new Date("2020-03-02")
);
expect(result.data.path(0, "t_timestamp_month").value).toEqual(
new Date("2020-03-01")
);
expect(result.data.path(0, "t_timestamp_year").value).toEqual(
new Date("2020-01-01")
);
expect(result.data.path(0, "t_timestamp_day_of_year").value).toEqual(62);
expect(result.data.path(0, "t_timestamp_day_of_month").value).toEqual(2);
}
);

it.skip("defines in model", async () => {
// const result1 = await model.makeQuery(`
Expand Down Expand Up @@ -491,7 +496,7 @@ aircraft_count
expect(result.data.path(0, "b_count").value).toBe(355);
});

it(`group by explore - simple group by`, async () => {
it(`group by explore - simple group by - ${databaseName}`, async () => {
const result = await expressionModel
.loadQuery(
`
Expand All @@ -505,7 +510,7 @@ aircraft_count
expect(result.data.path(0, "aircraft_models_id").value).toBe("7102802");
});

it(`group by explore - pipeline`, async () => {
it(`group by explore - pipeline - ${databaseName}`, async () => {
const result = await expressionModel
.loadQuery(
`
Expand All @@ -522,10 +527,12 @@ aircraft_count
expect(result.data.path(0, "manufacturer").value).toBe("CESSNA");
});

it(`group by explore - pipeline 2 levels`, async () => {
const result = await expressionModel
.loadQuery(
`
(databaseName === "postgres" ? it.skip : it)(
`group by explore - pipeline 2 levels - ${databaseName}`,
async () => {
const result = await expressionModel
.loadQuery(
`
define f is (explore 'malloytest.flights'
a is join (explore 'malloytest.aircraft' primary key tail_num
state_facts is join (explore 'malloytest.state_facts' primary key state ) on state
Expand All @@ -539,10 +546,11 @@ aircraft_count
state_facts.popular_name
flight_count is flight_count.sum()
`
)
.run();
// console.log(result.data.toObject());
expect(result.data.path(0, "flight_count").value).toBe(199726);
expect(result.data.path(0, "popular_name").value).toBe("Isabella");
});
)
.run();
// console.log(result.data.toObject());
expect(result.data.path(0, "flight_count").value).toBe(199726);
expect(result.data.path(0, "popular_name").value).toBe("Isabella");
}
);
});
1 change: 1 addition & 0 deletions postgres_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.tmp
12 changes: 12 additions & 0 deletions postgres_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Setup for Postgres Test Data

Assumes that postgres has been installed via nix (installs but doesn't configure).

ADD to environment: `export PGHOST=localhost`

**postgres_init.sh** - builds a database as the current user in .tmp/data/malloytestdb. Starts server runing on localhost:5432
copies the test data in `malloytest-postgres.sql.gz` into the database.

**postgres_start.sh** - starts the postgres server, once it has been installed (use after a reboot, for example)

**postgres_stop.sh** - stops the postgres server
Binary file added postgres_test/malloytest-postgres.sql.gz
Binary file not shown.
7 changes: 7 additions & 0 deletions postgres_test/postgres_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /bin/bash
export PGHOST=localhost
mkdir .tmp
initdb -d .tmp/data/malloytestdb --no-locale --encoding=UTF8
pg_ctl -D .tmp/data/malloytestdb -l .tmp/logfile -o "--unix_socket_directories='$PWD/.tmp'" start
createdb $USER
gunzip -c malloytest-postgres.sql.gz | psql
2 changes: 2 additions & 0 deletions postgres_test/postgres_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/bash
pg_ctl -D .tmp/malloytestdb -l logfile -o "--unix_socket_directories='$PWD'" start
2 changes: 2 additions & 0 deletions postgres_test/postgres_stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
pg_ctl -D .tmp/malloytestdb stop

0 comments on commit b8649f0

Please sign in to comment.