Skip to content

Commit

Permalink
#89 fix tests on x86
Browse files Browse the repository at this point in the history
  • Loading branch information
SingingBush committed Aug 31, 2022
1 parent 2b06ef6 commit 1b1162a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions source/ddbc/pods.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
68 changes: 68 additions & 0 deletions test/ddbctest/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1b1162a

Please sign in to comment.