From 1b1162a195c4d2c5bc537df35e71e87c6b08d2c8 Mon Sep 17 00:00:00 2001 From: singingbush Date: Wed, 31 Aug 2022 16:41:33 +0100 Subject: [PATCH] #89 fix tests on x86 --- source/ddbc/pods.d | 10 +++++++ test/ddbctest/main.d | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/source/ddbc/pods.d b/source/ddbc/pods.d index ee2c120..4c2d7e4 100644 --- a/source/ddbc/pods.d +++ b/source/ddbc/pods.d @@ -1076,6 +1076,16 @@ bool insert(T)(Statement stmt, ref T o) if (__traits(isPOD, T)) { sharedLog.tracef("The ID is of type '%s' and can be a '%s'", insertId.type().toString(), typeof(o.id).stringof); } o.id = insertId.get!(typeof(o.id)); // potentially could use coerce instead of get + } else if(is(typeof(o.id) == uint) || is(typeof(o.id) == int)) { + // This isn't generally an issue but on Windows (x86) using a size_t will result in a uint + static if(__traits(compiles, (){ import std.experimental.logger; } )) { + import std.experimental.logger ; sharedLog; + sharedLog.warningf("The ID is of type '%s', converting to type '%s' could cause data errors", insertId.type().toString(), typeof(o.id).stringof); + } else { + import std.stdio : writeln; + writeln("The ID is of type '" ~ insertId.type().toString() ~ "', converting to type '" ~ typeof(o.id).stringof ~ "' could cause data errors"); + } + o.id = to!(typeof(o.id))(insertId.get!ulong); // alternative syntax: o.id = cast(typeof(o.id))insertId.get!ulong; } else { static if(__traits(compiles, (){ import std.experimental.logger; } )) { import std.experimental.logger ; sharedLog; diff --git a/test/ddbctest/main.d b/test/ddbctest/main.d index d89fc94..18c064c 100644 --- a/test/ddbctest/main.d +++ b/test/ddbctest/main.d @@ -275,6 +275,74 @@ class SQLitePodTest : DdbcTestFixture { assertEquals(u.updated, result.updated); } + @Test // Test for: https://github.com/buggins/ddbc/issues/89 + public void testInsertingPodWithIdInt() { + Statement stmt = conn.createStatement(); + scope(exit) stmt.close(); + + // A POD with an int for an id + struct User { + int id; + string name; + int flags; + Date dob; + DateTime created; + SysTime updated; + } + + User u; + u.id = 0; + u.name = "Test 89"; + u.flags = 5; + + assertEquals(0, u.id, "default value is 0"); + bool inserted = stmt.insert!User(u); + assertTrue(inserted, "Should be able to perform INSERT with pod"); + assertEquals(1, u.id, "Should auto generate an ID"); + + immutable User result = stmt.get!User(u.id); + assertEquals(u.id, result.id); + assertEquals(u.name, result.name); + assertEquals(u.flags, result.flags); + assertEquals(u.dob, result.dob); + assertEquals(u.created, result.created); + assertEquals(u.updated, result.updated); + } + + @Test // Test for: https://github.com/buggins/ddbc/issues/89 + public void testInsertingPodWithIdUint() { + Statement stmt = conn.createStatement(); + scope(exit) stmt.close(); + + // A POD with an uint for an id + struct User { + uint id; + string name; + int flags; + Date dob; + DateTime created; + SysTime updated; + } + + User u; + u.id = 0; + u.name = "Test 89"; + u.flags = 5; + + assertEquals(0, u.id, "default value is 0"); + bool inserted = stmt.insert!User(u); + assertTrue(inserted, "Should be able to perform INSERT with pod"); + assertEquals(1, u.id, "Should auto generate an ID"); + + immutable User result = stmt.get!User(u.id); + assertEquals(u.id, result.id); + assertEquals(u.name, result.name); + assertEquals(u.flags, result.flags); + assertEquals(u.dob, result.dob); + assertEquals(u.created, result.created); + assertEquals(u.updated, result.updated); + } + @Test // Test for: https://github.com/buggins/ddbc/issues/89 public void testInsertingPodWithIdLong() { Statement stmt = conn.createStatement();