Skip to content

Commit

Permalink
Add CI for raw temporal types
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanruth committed Oct 17, 2024
1 parent 15f568a commit e8bcf7b
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 32 deletions.
1 change: 1 addition & 0 deletions checklicense.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def find_files_to_check():
('release.toml', ''),
('LICENSE', ''),
('TODO.org', ''),
('tests/ci/*.sql', '--')
]
file_prefixes = dict()
unknown = []
Expand Down
30 changes: 12 additions & 18 deletions src/convert/raw_temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ use num::Zero;

use crate::{cursor::replies::ResultSet, CursorResult};

use super::{
conversion_error,
raw_decimal::RawDecimal,
FromMonet,
};
use super::{conversion_error, raw_decimal::RawDecimal, FromMonet};

/// Representation of a DATE value from MonetDB
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -132,6 +128,10 @@ impl RawTime {
};
Ok(time)
}

pub fn microseconds(&self) -> u32 {
self.microseconds + 1_000_000 * self.seconds as u32
}
}

#[test]
Expand Down Expand Up @@ -290,21 +290,12 @@ impl RawTz {
#[test]
fn test_parse_tz() {
let mut s: &[u8] = b"+00:00xyz";
assert_eq!(
RawTz::parse(&mut s),
Ok(RawTz { seconds_east: 0 })
);
assert_eq!(RawTz::parse(&mut s), Ok(RawTz { seconds_east: 0 }));
assert_eq!(s, b"xyz");
s = b"-00:00";
assert_eq!(
RawTz::parse(&mut s),
Ok(RawTz { seconds_east: 0 })
);
assert_eq!(RawTz::parse(&mut s), Ok(RawTz { seconds_east: 0 }));
s = b"+01:00";
assert_eq!(
RawTz::parse(&mut s),
Ok(RawTz { seconds_east: 3600 })
);
assert_eq!(RawTz::parse(&mut s), Ok(RawTz { seconds_east: 3600 }));
s = b"+07:30";
assert_eq!(
RawTz::parse(&mut s),
Expand Down Expand Up @@ -470,7 +461,10 @@ where
*data = &data[n..];
Ok(v)
}
_ => Err(conversion_error::<T>("invalid integer")),
_ => Err(conversion_error::<T>(format_args!(
"invalid integer {:?}",
BStr::new(data)
))),
}
}

Expand Down
34 changes: 23 additions & 11 deletions tests/ci/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//
// Copyright 2024 MonetDB Foundation

use anyhow::{bail, Result as AResult};
use anyhow::{bail, Context, Result as AResult};

use monetdb::{ConnectResult, Connection, Cursor, Parameters};
use monetdb::{parms::Parm, ConnectResult, Connection, Cursor, Parameters};
use std::{
env::{self, VarError},
mem,
Expand All @@ -22,12 +22,12 @@ const DEFAULT_PASSWORD: &str = "monetdb";

/// This static either holds a mutex-protected Server Context or
/// the error message we got when we tried to create one.
static SERVER: LazyLock<Result<Mutex<Server>, String>> = LazyLock::new(initialize_server);
static SERVER: LazyLock<AResult<Mutex<Server>>> = LazyLock::new(find_and_initialize_server);

/// Get an exclusive handle on the server context, initializing if not already there.
pub fn get_server() -> MutexGuard<'static, Server> {
match &*SERVER {
Err(e) => panic!("{e}"),
Err(e) => panic!("{e:#}"),
Ok(srv) => match srv.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
Expand Down Expand Up @@ -69,19 +69,31 @@ pub fn with_shared_cursor(f: impl FnOnce(&mut Cursor) -> AResult<()>) -> AResult
})
}

fn initialize_server() -> Result<Mutex<Server>, String> {
fn find_and_initialize_server() -> AResult<Mutex<Server>> {
match parms_from_env(SERVER_URL_ENV_VAR, Some(DEFAULT_SERVER_URL)) {
Ok(parms) => {
let mut conn = Connection::new(parms.clone())?;
initialize_server(&mut conn).context("Could not initialize test database")?;
let server = Server {
parms,
shared: None,
shared: Some(conn),
};
Ok(Mutex::new(server))
}
Err(e) => Err(format!("{SERVER_URL_ENV_VAR}: {e}")),
Err(e) => bail!("{SERVER_URL_ENV_VAR}: {e}"),
}
}

const SQL: &str = include_str!("schema.sql");

fn initialize_server(conn: &mut Connection) -> AResult<()> {
let mut cursor = conn.cursor();
cursor.execute(SQL)?;
cursor.close()?;
Ok(())
}

/// Extract connection parameters from an environment variable
fn parms_from_env(env_var: &str, default_url: Option<&str>) -> AResult<Parameters> {
let url = match env::var(env_var) {
Ok(u) => u,
Expand All @@ -99,11 +111,11 @@ fn parms_from_env(env_var: &str, default_url: Option<&str>) -> AResult<Parameter
.with_user(DEFAULT_USER)?
.with_password(DEFAULT_PASSWORD)?;
parms.apply_url(&url)?;
parms.validate()?;

let test_parms = parms.clone().with_connect_timeout(2)?;
let conn = Connection::new(test_parms)?;
conn.close();
if parms.is_default(Parm::ConnectTimeout) {
parms.set_connect_timeout(2)?;
}

parms.validate()?;
Ok(parms)
}
29 changes: 29 additions & 0 deletions tests/ci/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- SPDX-License-Identifier: MPL-2.0
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--
-- Copyright 2024 MonetDB Foundation

START TRANSACTION;

DROP TABLE IF EXISTS temporal;
CREATE TABLE temporal(
i INT,
tsz TIMESTAMPTZ
);

-- The interval 87_654 seconds was chosen to get a nice variation
-- in seconds, minutes, hours, days, etc.
-- 1000 * 1000 goes back to about year -748.
INSERT INTO temporal
SELECT
value AS i,
NOW - value * value * INTERVAL '87654' SECOND
FROM
sys.generate_series(0, 1000)
;
-- SELECT * FROM temporal;

COMMIT;
Loading

0 comments on commit e8bcf7b

Please sign in to comment.