banshee r3628 - in trunk/banshee: . src/Libraries/Hyena/Hyena.Data.Sqlite tests tests/Hyena



Author: gburt
Date: Mon Mar 31 23:01:14 2008
New Revision: 3628
URL: http://svn.gnome.org/viewvc/banshee?rev=3628&view=rev

Log:
2008-03-31  Gabriel Burt  <gabriel burt gmail com>

	* src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs: Make sure
	to create the SQL with culturally invariant string formatting.  Fixes BGO
	#524871.

	* tests/Hyena/SqliteCommandTests.cs: Test HyenaSqliteCommand in many ways,
	including testing for the above bug.

	* tests/Makefile.am: Set to run in it_IT so we can check for cultural
	issues.


Added:
   trunk/banshee/tests/Hyena/SqliteCommandTests.cs
Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
   trunk/banshee/tests/Makefile.am

Modified: trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena.Data.Sqlite/HyenaSqliteCommand.cs	Mon Mar 31 23:01:14 2008
@@ -86,7 +86,7 @@
             execution_exception = null;
             result = null;
 
-            SqliteCommand sql_command = new SqliteCommand (CurrentSqlText ());
+            SqliteCommand sql_command = new SqliteCommand (CurrentSqlText);
             sql_command.Connection = connection;
             //Log.DebugFormat ("Executing {0}", sql_command.CommandText);
 
@@ -140,12 +140,20 @@
                 CreateParameters ();
             }
 
+            // Special case for if a single null values is the paramter array
+            if (parameter_count == 1 && param_values == null) {
+                current_values = new object [] { "NULL" };
+                command_formatted = null;
+                return this;
+            }
+
             if (param_values.Length != parameter_count) {
                 throw new ArgumentException (String.Format (
                     "Command has {0} parameters, but {1} values given.", parameter_count, param_values.Length
                 ));
             }
 
+            // Transform values as necessary - not needed for numerical types
             for (int i = 0; i < parameter_count; i++) {
                 if (param_values[i] is string) {
                     param_values[i] = String.Format ("'{0}'", (param_values[i] as string).Replace ("'", "''"));
@@ -161,17 +169,18 @@
             return this;
         }
 
-        private string CurrentSqlText ()
-        {
-            if (command_format == null) {
-                return command;
-            }
+        private string CurrentSqlText {
+            get {
+                if (command_format == null) {
+                    return command;
+                }
 
-            if (command_formatted == null) {
-                command_formatted = String.Format (command_format, current_values);
-            }
+                if (command_formatted == null) {
+                    command_formatted = String.Format (System.Globalization.CultureInfo.InvariantCulture, command_format, current_values);
+                }
 
-            return command_formatted;
+                return command_formatted;
+            }
         }
 
         private void CreateParameters ()

Added: trunk/banshee/tests/Hyena/SqliteCommandTests.cs
==============================================================================
--- (empty file)
+++ trunk/banshee/tests/Hyena/SqliteCommandTests.cs	Mon Mar 31 23:01:14 2008
@@ -0,0 +1,64 @@
+using System;
+using System.Reflection;
+using NUnit.Framework;
+using Hyena.Data.Sqlite;
+
+[TestFixture]
+public class SqliteCommandTests
+{
+    [Test]
+    public void TestIdentifiesParameters ()
+    {
+        HyenaSqliteCommand cmd = new HyenaSqliteCommand ("select foo from bar where baz = ?, bbz = ?, this = ?");
+
+        try {
+            cmd.ApplyValues ("a", 32);
+            Assert.Fail ("Should not have been able to pass 2 values to ApplyValues without exception");
+        } catch {}
+
+        try {
+            cmd.ApplyValues ("a", 32, "22");
+        } catch {
+            Assert.Fail ("Should have been able to pass 3 values to ApplyValues without exception");
+        }
+
+        Assert.AreEqual ("select foo from bar where baz = 'a', bbz = 32, this = '22'", GetGeneratedSql (cmd));
+    }
+
+    [Test]
+    public void TestConstructor ()
+    {
+        HyenaSqliteCommand cmd = new HyenaSqliteCommand ("select foo from bar where baz = ?, bbz = ?, this = ?", "a", 32, "22");
+        Assert.AreEqual ("select foo from bar where baz = 'a', bbz = 32, this = '22'", GetGeneratedSql (cmd));
+    }
+
+    [Test]
+    public void TestCultureInvariant ()
+    {
+        HyenaSqliteCommand cmd = new HyenaSqliteCommand ("select foo from bar where baz = ?", 32.2);
+        Assert.AreEqual ("select foo from bar where baz = 32.2", GetGeneratedSql (cmd));
+    }
+
+    [Test]
+    public void TestParameterSerialization ()
+    {
+        HyenaSqliteCommand cmd = new HyenaSqliteCommand ("select foo from bar where baz = ?");
+
+        Assert.AreEqual ("select foo from bar where baz = NULL", GetGeneratedSql (cmd.ApplyValues (null)));
+        Assert.AreEqual ("select foo from bar where baz = 'It''s complicated, \"but\" ''''why not''''?'", GetGeneratedSql (cmd.ApplyValues ("It's complicated, \"but\" ''why not''?")));
+        Assert.AreEqual ("select foo from bar where baz = 0", GetGeneratedSql (cmd.ApplyValues (new DateTime (1970, 1, 1).ToLocalTime ())));
+        Assert.AreEqual ("select foo from bar where baz = 931309200", GetGeneratedSql (cmd.ApplyValues (new DateTime (1999, 7, 7).ToLocalTime ())));
+        Assert.AreEqual ("select foo from bar where baz = 555.55", GetGeneratedSql (cmd.ApplyValues (555.55f)));
+        Assert.AreEqual ("select foo from bar where baz = 555.55", GetGeneratedSql (cmd.ApplyValues (555.55)));
+        Assert.AreEqual ("select foo from bar where baz = 555", GetGeneratedSql (cmd.ApplyValues (555)));
+
+        HyenaSqliteCommand cmd2 = new HyenaSqliteCommand ("select foo from bar where baz = ?, bar = ?, boo = ?");
+        Assert.AreEqual ("select foo from bar where baz = NULL, bar = NULL, boo = 22", GetGeneratedSql (cmd2.ApplyValues (null, null, 22)));
+    }
+
+    static PropertyInfo tf = typeof(HyenaSqliteCommand).GetProperty ("CurrentSqlText", BindingFlags.Instance | BindingFlags.NonPublic);
+    private static string GetGeneratedSql (HyenaSqliteCommand cmd)
+    {
+        return tf.GetValue (cmd, null) as string;
+    }
+}

Modified: trunk/banshee/tests/Makefile.am
==============================================================================
--- trunk/banshee/tests/Makefile.am	(original)
+++ trunk/banshee/tests/Makefile.am	Mon Mar 31 23:01:14 2008
@@ -8,7 +8,8 @@
 ASSEMBLY_CSFILES =  \
 	$(srcdir)/Hyena/CryptoUtilTests.cs \
 	$(srcdir)/Hyena/RangeCollectionTests.cs \
-	$(srcdir)/Hyena/StringUtilTests.cs
+	$(srcdir)/Hyena/StringUtilTests.cs \
+	$(srcdir)/Hyena/SqliteCommandTests.cs
 
 #	Banshee.Core/KernelTests.cs
 #	Banshee.Core/FileNamePatternTests.cs
@@ -31,7 +32,7 @@
 all: $(ASSEMBLY)
 
 run-test: $(NUNIT_TESTER) $(ASSEMBLY)
-	MONO_PATH="$(top_builddir)/bin" mono --debug $(NUNIT_TESTER) $(ASSEMBLY)
+	LANG=it_IT MONO_PATH="$(top_builddir)/bin" mono --debug $(NUNIT_TESTER) $(ASSEMBLY)
 endif
 
 EXTRA_DIST = $(ASSEMBLY_CSFILES)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]