Skip to content

Commit

Permalink
Added more Insert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fstokke committed Jun 29, 2011
1 parent e325180 commit 21a1d93
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/InsertTest.cs
Expand Up @@ -26,12 +26,29 @@ public override string ToString ()
}

}


public class TestObj2
{
[PrimaryKey]
public int Id { get; set; }
public String Text { get; set; }

public override string ToString()
{
return string.Format("[TestObj: Id={0}, Text={1}]", Id, Text);
}

}


public class TestDb : SQLiteConnection
{
public TestDb(String path)
: base(path)
{
CreateTable<TestObj>();
CreateTable<TestObj2>();
}

}
Expand Down Expand Up @@ -68,6 +85,89 @@ public void InsertALot()
var numCount = db.CreateCommand("select count(*) from TestObj").ExecuteScalar<int>();

Assert.AreEqual(numCount, n, "Num counted must = num objects");

db.Close();
}

[Test]
public void InsertTwoTimes()
{
var db = new TestDb(Path.GetTempFileName());
var obj1 = new TestObj() { Text = "GLaDOS loves testing!" };
var obj2 = new TestObj() { Text = "Keep testing, just keep testing" };


var numIn1 = db.Insert(obj1);
var numIn2 = db.Insert(obj2);
Assert.AreEqual(1, numIn1);
Assert.AreEqual(1, numIn2);

var result = db.Query<TestObj>("select * from TestObj").ToList();
Assert.AreEqual(2, result.Count);
Assert.AreEqual(obj1.Text, result[0].Text);
Assert.AreEqual(obj2.Text, result[1].Text);

db.Close();
}

[Test]
public void InsertIntoTwoTables()
{
var db = new TestDb(Path.GetTempFileName());
var obj1 = new TestObj() { Text = "GLaDOS loves testing!" };
var obj2 = new TestObj2() { Text = "Keep testing, just keep testing" };

var numIn1 = db.Insert(obj1);
Assert.AreEqual(1, numIn1);
var numIn2 = db.Insert(obj2);
Assert.AreEqual(1, numIn2);

var result1 = db.Query<TestObj>("select * from TestObj").ToList();
Assert.AreEqual(numIn1, result1.Count);
Assert.AreEqual(obj1.Text, result1.First().Text);

var result2 = db.Query<TestObj>("select * from TestObj2").ToList();
Assert.AreEqual(numIn2, result2.Count);

db.Close();
}

[Test]
public void InsertWithExtra()
{
var db = new TestDb(Path.GetTempFileName());
var obj1 = new TestObj2() { Id=1, Text = "GLaDOS loves testing!" };
var obj2 = new TestObj2() { Id=1, Text = "Keep testing, just keep testing" };
var obj3 = new TestObj2() { Id=1, Text = "Done testing" };

db.Insert(obj1);


try {
db.Insert(obj2);
Assert.Fail("Expected unique constraint violation");
}
catch (SQLiteException) {
}
db.Insert(obj2, "OR REPLACE");


try {
db.Insert(obj3);
Assert.Fail("Expected unique constraint violation");
}
catch (SQLiteException) {
}
db.Insert(obj3, "OR IGNORE");

var result = db.Query<TestObj>("select * from TestObj2").ToList();
Assert.AreEqual(1, result.Count);
Assert.AreEqual(obj2.Text, result.First().Text);


db.Close();
}


}
}

0 comments on commit 21a1d93

Please sign in to comment.