Add more tests for using Theme with the database
This commit is contained in:
parent
df066c611d
commit
ca1e072d9b
1 changed files with 34 additions and 10 deletions
44
src/theme.rs
44
src/theme.rs
|
@ -56,6 +56,9 @@ impl ToSql<Text, Sqlite> for Theme {
|
||||||
|
|
||||||
impl FromSql<Text, Sqlite> for Theme {
|
impl FromSql<Text, Sqlite> for Theme {
|
||||||
fn from_sql(value: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
fn from_sql(value: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
|
||||||
|
// See Diesel's documentation on how to implement FromSql for Sqlite,
|
||||||
|
// especially with regards to the unsafe conversion below.
|
||||||
|
// http://docs.diesel.rs/diesel/deserialize/trait.FromSql.html
|
||||||
let text_ptr = <*const str as FromSql<Text, Sqlite>>::from_sql(value)?;
|
let text_ptr = <*const str as FromSql<Text, Sqlite>>::from_sql(value)?;
|
||||||
let text = unsafe { &*text_ptr };
|
let text = unsafe { &*text_ptr };
|
||||||
text.parse().map_err(Into::into)
|
text.parse().map_err(Into::into)
|
||||||
|
@ -82,9 +85,15 @@ impl Display for CssClass {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use std::error::Error;
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::sql_query;
|
||||||
|
use diesel::sql_types::Text;
|
||||||
use serde_plain;
|
use serde_plain;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_serialize() {
|
fn basic_serialize() {
|
||||||
assert_eq!(serde_plain::to_string(&Theme::Red).unwrap(), "red");
|
assert_eq!(serde_plain::to_string(&Theme::Red).unwrap(), "red");
|
||||||
|
@ -129,21 +138,36 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_db_roundtrip() {
|
fn basic_db_roundtrip() -> Result<(), Box<Error>> {
|
||||||
use diesel::prelude::*;
|
let conn = SqliteConnection::establish(":memory:")?;
|
||||||
use diesel::sql_query;
|
|
||||||
use diesel::sql_types::Text;
|
|
||||||
|
|
||||||
let conn = SqliteConnection::establish(":memory:").unwrap();
|
|
||||||
|
|
||||||
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
struct Row { #[sql_type = "Text"] theme: Theme }
|
struct Row { #[sql_type = "Text"] theme: Theme }
|
||||||
|
|
||||||
let res: Vec<Row> = sql_query("SELECT ? as theme")
|
let res = sql_query("SELECT ? as theme")
|
||||||
.bind::<Text, _>(DeepPurple)
|
.bind::<Text, _>(DeepPurple)
|
||||||
.load(&conn)
|
.load::<Row>(&conn)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(&[ Row { theme: DeepPurple } ], res.as_slice());
|
assert_eq!(&[ Row { theme: DeepPurple } ], res.as_slice());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn db_invalid_value_gives_error() -> Result<(), Box<Error>> {
|
||||||
|
let conn = SqliteConnection::establish(":memory:")?;
|
||||||
|
|
||||||
|
#[derive(QueryableByName, PartialEq, Eq, Debug)]
|
||||||
|
struct Row { #[sql_type = "Text"] theme: Theme }
|
||||||
|
|
||||||
|
let res = sql_query("SELECT 'green' as theme")
|
||||||
|
.load::<Row>(&conn);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
|
||||||
|
let res = sql_query("SELECT 'blueish-yellow' as theme")
|
||||||
|
.load::<Row>(&conn);
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue