#![cfg(feature = "sqlite")]
extern crate rusqlite;
extern crate trust_dns_client;
extern crate trust_dns_proto;
extern crate trust_dns_server;
use std::net::*;
use std::str::FromStr;
use rusqlite::*;
use trust_dns_client::rr::*;
use trust_dns_server::store::sqlite::persistence::CURRENT_VERSION;
use trust_dns_server::store::sqlite::Journal;
#[test]
fn test_new_journal() {
let conn = Connection::open_in_memory().expect("could not create in memory DB");
assert_eq!(
Journal::new(conn).expect("new Journal").schema_version(),
-1
);
}
#[test]
fn test_init_journal() {
let conn = Connection::open_in_memory().expect("could not create in memory DB");
let mut journal = Journal::new(conn).unwrap();
let version = journal.schema_up().unwrap();
assert_eq!(version, CURRENT_VERSION);
assert_eq!(
Journal::select_schema_version(&*journal.conn()).unwrap(),
CURRENT_VERSION
);
}
fn create_test_journal() -> (Record, Journal) {
let www = Name::from_str("www.example.com").unwrap();
let mut record = Record::new();
record.set_name(www);
record.set_rr_type(RecordType::A);
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.0.1").unwrap()));
let conn = Connection::open_in_memory().expect("could not create in memory DB");
let mut journal = Journal::new(conn).unwrap();
journal.schema_up().unwrap();
journal.insert_record(0, &record).unwrap();
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.1.1").unwrap()));
journal.insert_record(0, &record).unwrap();
(record, journal)
}
#[test]
fn test_insert_and_select_record() {
let (mut record, journal) = create_test_journal();
let (row_id, journal_record) = journal
.select_record(0)
.expect("persistence error")
.expect("none");
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.0.1").unwrap()));
assert_eq!(journal_record, record);
let (row_id, journal_record) = journal
.select_record(row_id + 1)
.expect("persistence error")
.expect("none");
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.1.1").unwrap()));
assert_eq!(journal_record, record);
let option_none = journal
.select_record(row_id + 1)
.expect("persistence error");
assert!(option_none.is_none());
}
#[test]
fn test_iterator() {
let (mut record, journal) = create_test_journal();
let mut iter = journal.iter();
assert_eq!(
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.0.1").unwrap())),
&iter.next().unwrap()
);
assert_eq!(
record.set_rdata(RData::A(Ipv4Addr::from_str("127.0.1.1").unwrap())),
&iter.next().unwrap()
);
assert_eq!(None, iter.next());
}