[libgda] DbRecordCollection and RecordCollection pass Unit Tests



commit 852db0ed1ea10516aa9161ff22394484c778abb0
Author: Daniel Espinosa <despinosa src gnome org>
Date:   Tue Jan 10 10:11:07 2012 -0600

    DbRecordCollection and RecordCollection pass Unit Tests

 libgda/data/DbRecordCollection.vala       |    5 +-
 libgda/data/GdaData-5.0.gir               |    8 +-
 libgda/data/Record.vala                   |    1 -
 libgda/data/RecordCollection.vala         |   72 ++++++---
 samples/vala/SampleDataModelIterable.vala |  132 +++++++++-------
 tests/vala/CheckDataModelIterator.vala    |  236 ++++++++++++++++++++++++-----
 6 files changed, 325 insertions(+), 129 deletions(-)
---
diff --git a/libgda/data/DbRecordCollection.vala b/libgda/data/DbRecordCollection.vala
index e07462c..a8f8e11 100644
--- a/libgda/data/DbRecordCollection.vala
+++ b/libgda/data/DbRecordCollection.vala
@@ -21,9 +21,10 @@ using Gee;
 using Gda;
 
 namespace GdaData {
-	public interface DbRecordCollection<A,G> : Object, Traversable<G>, Iterable<G>, Collection<G>
+	public interface DbRecordCollection<G> : Object, Traversable<DbRecord<Value?>>, 
+				Iterable<DbRecord<Value?>>, Collection<DbRecord<Value?>>
 	{
 		public abstract Connection   connection { get; set; }
-		public abstract DbTable<A>   table      { get; }
+		public abstract DbTable<G>   table      { get; }
 	}
 }
diff --git a/libgda/data/GdaData-5.0.gir b/libgda/data/GdaData-5.0.gir
index 062dc58..77ce059 100644
--- a/libgda/data/GdaData-5.0.gir
+++ b/libgda/data/GdaData-5.0.gir
@@ -192,7 +192,9 @@
 					<type name="Gda.DataModel" c:type="GdaDataModel*"/>
 				</parameter>
 				<parameter name="table" transfer-ownership="none">
-					<type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+					<type name="GdaData.DbTable" c:type="GdaDataDbTable*">
+						<type name="GObject.Value" c:type="GValue*"/>
+					</type>
 				</parameter>
 			</parameters>
 			<return-value transfer-ownership="full">
@@ -229,7 +231,9 @@
 					<type name="Gda.DataModelIter" c:type="GdaDataModelIter*"/>
 				</parameter>
 				<parameter name="table" transfer-ownership="none">
-					<type name="GdaData.DbTable" c:type="GdaDataDbTable*"/>
+					<type name="GdaData.DbTable" c:type="GdaDataDbTable*">
+						<type name="GObject.Value" c:type="GValue*"/>
+					</type>
 				</parameter>
 			</parameters>
 			<return-value transfer-ownership="full">
diff --git a/libgda/data/Record.vala b/libgda/data/Record.vala
index 35c9513..08e6ac4 100644
--- a/libgda/data/Record.vala
+++ b/libgda/data/Record.vala
@@ -208,7 +208,6 @@ namespace GdaData {
 			foreach (DbField<Value?> f in this.fields) {
 				r += "|" + Gda.value_stringify (f.value);
 			}
-			r+="\n";
 			return r;
         }
         // Comparable Interface
diff --git a/libgda/data/RecordCollection.vala b/libgda/data/RecordCollection.vala
index 465511e..614d438 100644
--- a/libgda/data/RecordCollection.vala
+++ b/libgda/data/RecordCollection.vala
@@ -21,11 +21,10 @@ using Gee;
 using Gda;
 
 namespace GdaData {
-	public class RecordCollection : AbstractCollection<DbRecord<Value?>>, 
-	DbRecordCollection<Value?,DbRecord<Value?>>
+	public class RecordCollection : AbstractCollection<DbRecord<Value?>>, DbRecordCollection<Value?>
 	{
 		private DataModel _model;
-		private DbTable _table;
+		private DbTable<Value?> _table;
 		
 		public DbTable<Value?> table { get { return table; } }
 		
@@ -33,11 +32,9 @@ namespace GdaData {
 		
 		public Connection connection { get; set; }
 		
-		public RecordCollection (DataModel m, DbTable table)
+		public RecordCollection (DataModel m, DbTable<Value?> table)
 		{
-			((DataSelect) m).compute_modification_statements ();
-			var pm = DataProxy.new (m);
-			_model = (DataModel) pm;
+			_model = m;
 			_table = table;
 		}
 		// AbstractCollection Implementation
@@ -78,7 +75,7 @@ namespace GdaData {
 		public override Gee.Iterator<DbRecord<Value?>> iterator () 
 		{ 
 			var iter = _model.create_iter ();
-			return new RecordCollectionIterator (iter, table); 
+			return new RecordCollectionIterator (iter, _table); 
 		}
 		public override bool remove (DbRecord<Value?> item)
 		{
@@ -117,6 +114,19 @@ namespace GdaData {
 				return _model.get_n_rows ();
 			} 
 		}
+		// Traversable Interface
+		public override Iterator<DbRecord<Value?>> chop (int offset, int length = -1)
+		{
+			return this.iterator().chop (offset, length);
+		}
+		public override Gee.Iterator<DbRecord<Value?>> filter (owned Gee.Predicate<DbRecord<Value?>> f)
+		{
+			return this.iterator().filter (f);			
+		}
+		public override Iterator<A> stream<A> (owned StreamFunc<DbRecord<Value?>, A> f)
+		{
+			return this.iterator().stream<A> (f);
+		}
 		// 
 		public string to_string ()
 		{
@@ -127,21 +137,24 @@ namespace GdaData {
 	public class RecordCollectionIterator : Object, Traversable<DbRecord<Value?>>, Iterator<DbRecord<Value?>>
 	{
 		private DataModelIter _iter;
-		private DbTable _table;
+		private DbTable<Value?> _table;
 		private int _length = -1;
 		private HashMap<int,int> _elements = new HashMap<int,int>();
+		private int filter_pos = -1;
 		
-		public RecordCollectionIterator (DataModelIter iter, DbTable table)
+		public RecordCollectionIterator (DataModelIter iter, DbTable<Value?> table)
 		{
 			_iter = iter;
 			_table = table;
 			_length = _iter.data_model.get_n_rows ();
 		}
 		
-		private RecordCollectionIterator.filtered_elements (DataModelIter iter, DbTable table, HashMap<int,int> elements)
+		private RecordCollectionIterator.filtered_elements (DataModelIter iter, DbTable table, 
+															HashMap<int,int> elements)
 		{
 			_iter = iter;
 			_table = table;
+			_length = _iter.data_model.get_n_rows ();
 			_elements = elements;
 		}
 		private RecordCollectionIterator.with_lenght (DataModelIter iter, DbTable table, int length)
@@ -156,6 +169,8 @@ namespace GdaData {
 		
 		// Traversable Interface
 		public Gee.Iterator<DbRecord<Value?>> chop (int offset, int length = -1) 
+			requires (offset <= _iter.data_model.get_n_rows ())
+			requires (offset + length <= _iter.data_model.get_n_rows ())
 		{
 			var iter = _iter.data_model.create_iter ();
 			iter.move_to_row (offset);
@@ -167,11 +182,13 @@ namespace GdaData {
 			while (_iter.move_next ()) {
 				var r = this.get ();
 				if (f(r)) {
-					elements.set (_iter.get_row (), _iter.get_row ());
+					stdout.printf ("SELECTED row: " + _iter.current_row.to_string () 
+									+ "Values:\n" + this.get().to_string ());
+					elements.set (++filter_pos, _iter.current_row);
 				}
 			}
 			var iter = _iter.data_model.create_iter ();
-			return (Iterator<DbRecord<Value?>>) new RecordCollectionIterator.filtered_elements (iter, _table, elements);
+			return new RecordCollectionIterator.filtered_elements (iter, _table, elements);
 		}
 		public new void @foreach (Gee.ForallFunc<DbRecord<Value?>> f) 
 		{
@@ -182,7 +199,7 @@ namespace GdaData {
 		}
 		public Gee.Iterator<A> stream<A> (owned Gee.StreamFunc<DbRecord<Value?>,A> f) 
 		{
-			return Gee.Iterator<A>.stream_impl<DbRecord<Value?>,A> (this, f);
+			return stream_impl<DbRecord<Value?>, A> (this, f);
 		}
 		
 		// Iterator Interface
@@ -192,25 +209,30 @@ namespace GdaData {
 			r.connection = _table.connection;
 			r.table = _table;
 			for (int c = 0; c < _iter.data_model.get_n_columns (); c++) {
-				// FIXME: get attributes from table
-				var f = new Field<Value?>(_iter.data_model.get_column_name (c), DbField.Attribute.NONE);
-				f.value = _iter.get_value_at (c);
-				r.set_field (f);
+				r.set_field_value (_iter.data_model.get_column_name (c), _iter.get_value_at (c));
 			}
-			return (DbRecord<Value?>) r;
+			return r;
 		}
 		public bool has_next () 
 		{
-			return _iter.current_row + 1 <= _iter.data_model.get_n_rows () ? true : false;
+			return (_iter.current_row + 1 <= _iter.data_model.get_n_rows ()) ||
+					(_iter.current_row + 1 <= _length) ? true : false;
 		}
 		public bool next () 
 		{ 
-			var r = _iter.move_next ();
-			if (_iter.current_row > _length) {
-				_iter.invalidate_contents ();
-				return false;
+			bool ret = false;
+			if (_elements.size > 0 && ++filter_pos < _elements.size) {
+				_iter.move_to_row (_elements.get (filter_pos));
 			}
-			return r;
+			else {
+				if (this.has_next ())
+					ret = _iter.move_next ();
+				else {
+					ret = false;
+					_iter.invalidate_contents ();
+				}
+			}
+			return ret;
 		}
 		public void remove () { _iter.data_model.remove_row (_iter.current_row); }
 		public bool read_only 
diff --git a/samples/vala/SampleDataModelIterable.vala b/samples/vala/SampleDataModelIterable.vala
index f5dd036..0f3e509 100644
--- a/samples/vala/SampleDataModelIterable.vala
+++ b/samples/vala/SampleDataModelIterable.vala
@@ -24,7 +24,7 @@ namespace Sample {
 	
 	class App : GLib.Object {
 		private Gda.Connection connection;
-		private DataModelIterable itermodel;
+		private RecordCollection itermodel;
 		
 		public void init ()
 			throws Error
@@ -36,6 +36,11 @@ namespace Sample {
 			this.connection.execute_non_select_command("CREATE TABLE user (id int PRIMARY KEY, name string, city string)");
 			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (1, \"Daniel\", \"Mexico\")");
 			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (2, \"Jhon\", \"USA\")");
+			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (3, \"James\", \"Germany\")");
+			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (4, \"Jack\", \"United Kindom\")");
+			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (5, \"Elsy\", \"EspaÃa\")");
+			this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (6, \"Mayo\", \"Mexico\")");
+			
 			
 			stdout.printf("Creating table 'company'...\n");
 			this.connection.execute_non_select_command("CREATE TABLE company (id int PRIMARY KEY, name string, responsability string)");
@@ -43,90 +48,101 @@ namespace Sample {
 			this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (2, \"Viasa\", \"Accessories\")");
 			this.connection.update_meta_store(null);
 			var model = this.connection.execute_select_command ("SELECT * FROM user");
-			this.itermodel = new DataModelIterable (model);
+			var t = new Table<Value?> ();
+			t.connection = connection;
+			t.name = "user";
+			this.itermodel = new RecordCollection (model, t);
 
 		}
 		
 		public void iterating ()
 			throws Error
 		{
-			stdout.printf ("Iterating over all Values in DataModel\n");
-			stdout.printf ("VALUE\tTYPE\n");
-			foreach (Value v in itermodel) {
-				stdout.printf ("%s\t%s\n", v.type_name (), Gda.value_stringify (v));
+			stdout.printf ("Iterating over all Records in DataModel using foreach\n");
+			foreach (DbRecord<Value?> r in itermodel) {
+				stdout.printf (r.to_string ());
+			}
+			stdout.printf ("Iterating over all Records in DataModel using a Gee.Iterator\n");
+			var iter = itermodel.iterator ();
+			while (iter.next ())
+			{
+				stdout.printf (iter.get ().to_string ());
 			}
+			stdout.printf ("\n");
 		}
 		
 		public void choping ()
 		{
-			stdout.printf ("Choping to get the 5th Value\n");
-			var iter = itermodel.chop (4,0);
-			iter.next ();
-			Value v = iter.get ();
-			stdout.printf ("Value=%s; Row = %i Col = %i\n", Gda.value_stringify (v), 
-							  ((GdaData.DataModelIterator) iter).current_row, 
-							  ((GdaData.DataModelIterator) iter).current_column);
-			
-			stdout.printf ("Choping to get 2nd to 5th Values...\n");
-			var iter2 = itermodel.chop (1,4);
-			stdout.printf ("TYPE\t\tVALUE\n");
+			stdout.printf ("Choping to get the 2nd DbRecord...\n");
+			var iter = itermodel.chop (1);
+			while (iter.next ()) {
+				stdout.printf (iter.get ().to_string () + "\n");
+			}
+			stdout.printf ("Choping to get the 4th to 5th DbRecords...\n");
+			var iter2 = itermodel.chop (3,4);
 			while (iter2.next ()) {
-				Value v2 = iter2.get ();
-				stdout.printf ("%s\t\t%s\n", v2.type_name (), Gda.value_stringify (v2));
+				var r2 = iter2.get ();
+				stdout.printf (iter2.get ().to_string ());
 			}
+			stdout.printf ("\n");
 		}
 		
 		public void filtering ()
 		{
-			stdout.printf ("Filtering Values: Any STRING with a letter 'n'...\n");
-			Gee.Predicate <Value?> f = (g) => {
-						bool ret = false;
-						if (g.type () == typeof (string)) {
-							string t = (string) g;
-							if (t.contains ("n"))
-								ret = true;
-							else
-								ret = false;
-						}
-						else
-							ret = false;
-						stdout.printf ("To be Included?: %s, %s\n", 
-										Gda.value_stringify (g), ret == true ? "TRUE" : "FALSE");
-						return ret;
-					};
+			stdout.printf ("Filtering Records: Any field type STRING with a letter 'x'...\n");
 			
-			var iter3 = itermodel.filter (f);
+			var iter = itermodel.filter (
+			(g) => {
+				bool ret = false;
+				foreach (DbField<Value?> fl in g.fields) {
+					string t = Gda.value_stringify (fl.value);
+					stdout.printf ("Value to check: " + t);
+					if (t.contains ("x")) {
+						ret = true;
+						stdout.printf ("...SELECTED\n");
+						break;
+					}
+					else {
+						ret = false;
+						stdout.printf ("...REJECTED\n");
+					}
+				}
+				if (ret) stdout.printf ("SELECTED ROW: \n" + g.to_string () + "\n");
+				return ret;
+			}
+			);
 			stdout.printf ("Printing Filtered Values...\n");
-			stdout.printf ("TYPE\tVALUE\n");
-			while (iter3.next ()) {
-				Value v3 = iter3.get ();
-				stdout.printf (v3.type_name (), Gda.value_stringify (v3) );
+			while (iter.next ()) {
+				stdout.printf (iter.get().to_string ());
 			}
-			
+			stdout.printf ("\n");
 		}
 		public void streaming ()
 		{
 			/* FIXME: This code doesn't return YIELDED strings maybe becasue Lazy is not correctly built. 
 				In theory stream() function is working correctly*/
-			stdout.printf ("Streaming Values: Any STRING will be YIELDED...\n");
-			Gee.StreamFunc<Value?,string> s = (state, g, lazy) =>	{
-						if (state == Gee.Traversable.Stream.CONTINUE) {
-							Value vs = g.get ();
-							stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (vs));
-							string ts = "YIELDED Value = " + Gda.value_stringify (vs) + "\n";
-							lazy = new Gee.Lazy<string>.from_value (ts.dup ());
-							stdout.printf (ts);
-							return Gee.Traversable.Stream.YIELD;
+			stdout.printf ("Streaming Values: Fist field type STRING will be YIELDED...\n");
+			var iter = itermodel.stream<string> (
+				(state, g, out lazy) =>	{
+					if (state == Gee.Traversable.Stream.CONTINUE) {
+						var r = g.value;
+						foreach (DbField<Value?> f in r.fields) {
+							if (f.value.type () == typeof (string)) {
+								stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (f.value));
+								string ts = "YIELDED Value = " + Gda.value_stringify (f.value) + "\n";
+								lazy = new Gee.Lazy<string>.from_value (ts.dup ());
+							}
 						}
-						return Gee.Traversable.Stream.END;
-					};
-			
-			var iter5 = itermodel.stream<string> (s);
+						return Gee.Traversable.Stream.YIELD;
+					}
+					return Gee.Traversable.Stream.END;
+				}
+			);
 			stdout.printf ("Printing Streamed Values...\n");
-			while (iter5.next ()) {
-				string sv = iter5.get ();
-				stdout.printf ("%s\n", sv);
+			while (iter.next ()) {
+				stdout.printf (iter.get());
 			}
+			stdout.printf ("\n");
 		}
 				
 		public static int main (string[] args) {
@@ -140,9 +156,9 @@ namespace Sample {
 				return 1;
 			}
 			app.iterating ();
+			app.streaming ();
 			app.choping ();
 			app.filtering ();
-			app.streaming ();
 			return 0;
 		}
 	}
diff --git a/tests/vala/CheckDataModelIterator.vala b/tests/vala/CheckDataModelIterator.vala
index adaf0cd..ab12900 100644
--- a/tests/vala/CheckDataModelIterator.vala
+++ b/tests/vala/CheckDataModelIterator.vala
@@ -24,6 +24,7 @@ namespace Check {
 	
 	class Tests : GLib.Object {
 		private Gda.Connection connection;
+		private DbRecordCollection itermodel;
 		
 		Tests()
 		{
@@ -31,15 +32,15 @@ namespace Check {
 				GLib.FileUtils.unlink("datamodeliterator.db");
 				stdout.printf("Creating Database...\n");
 				this.connection = Connection.open_from_string("SQLite", "DB_DIR=.;DB_NAME=datamodeliterator", null, Gda.ConnectionOptions.NONE);
-				stdout.printf("Creating table 'user'...\n");
+			stdout.printf("Creating table 'user'...\n");
 				this.connection.execute_non_select_command("CREATE TABLE user (id int PRIMARY KEY, name string, city string)");
 				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (1, \"Daniel\", \"Mexico\")");
 				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (2, \"Jhon\", \"USA\")");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (3, \"James\", \"Germany\")");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (4, \"Jack\", \"United Kindom\")");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (5, \"Elsy\", \"EspaÃa\")");
+				this.connection.execute_non_select_command("INSERT INTO user (id, name, city) VALUES (6, \"Mayo\", \"Mexico\")");
 				
-				stdout.printf("Creating table 'company'...\n");
-				this.connection.execute_non_select_command("CREATE TABLE company (id int PRIMARY KEY, name string, responsability string)");
-				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (1, \"Telcsa\", \"Programing\")");
-				this.connection.execute_non_select_command("INSERT INTO company (id, name, responsability) VALUES (2, \"Viasa\", \"Accessories\")");
 				this.connection.update_meta_store(null);
 			}
 			catch (Error e) {
@@ -47,52 +48,201 @@ namespace Check {
 			}
 		}
 		
-		public int t1()
+		public int iterating ()
 			throws Error
 		{
-			stdout.printf (">>> NEW TEST: GdaData.RecordCollection & RecordCollectionIterator API tests\n"
-						 	+ ">>> Testing Iterable, Traversable Interfaces\n");
 			int fails = 0;
-			var model = this.connection.execute_select_command ("SELECT * FROM user");
-			var t = new Table<Value?> ();
-			t.connection = this.connection;
-			t.name = "user";
-			var itermodel = new RecordCollection (model, t);
-//			stdout.printf ("First Record contents: \n" + r.to_string ());
-			stdout.printf ("Iterating over all Records in DataModel\n");
+			stdout.printf ("Iterating over all Records in DataModel using foreach...\n");
+			int i = 0;
 			foreach (DbRecord<Value?> r in itermodel) {
-				stdout.printf (r.to_string ());
+				string t = r.to_string ();
+				if (t == null) {
+					fails++;
+					break;
+				}
+				i++;
+				stdout.printf (t + "\n");
 			}
-			
-			stdout.printf ("Choping to get first record id= 1\n");
-			var iter = itermodel.chop (0,0);
-			iter.next ();
-			var v = iter.get ().get_field ("id").value;
-			if (v.type () == typeof (int))
-				stdout.printf ("ID=%i\n", (int) v);
+			if (fails > 0 || i != 6)
+				stdout.printf ("----- FAIL "+ fails.to_string () + "\n");
+			else
+				stdout.printf ("+++++ PASS\n");
+			stdout.printf ("Iterating using an Gee.Iterator...\n");
+			i = 0;
+			var iter = itermodel.iterator ();
+			while (iter.next ()) {
+				string t2 = iter.get ().to_string ();
+				if (t2 == null) {
+					fails++;
+					break;
+				}
+				else {
+					stdout.printf (t2 + "\n");
+					i++;
+				}
+			}
+			if (fails > 0 || i != 6)
+				stdout.printf ("----- FAIL "+ fails.to_string () + "\n");
+			else
+				stdout.printf ("+++++ PASS\n");
+			return fails;
+		}
+		
+		public int choping ()
+		{
+			int fails = 0;
+			stdout.printf ("Choping to get the 2nd DbRecord...\n");
+			var iter = itermodel.chop (1);
+			stdout.printf (iter.get ().to_string () + "\n");
+			var name = (string) iter.get().get_value ("name");
+			if (name == "Jhon")
+				stdout.printf ("+++++ PASS\n");
 			else {
 				fails++;
-				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
-							   " but Value is type = '%s'" +
-							   " with value = %s\n",
-							   fails, typeof (int).name (), v.type_name (), 
-							   Gda.value_stringify (v));			
+				stdout.printf ("----- FAIL: " + (fails++).to_string () + "\n");
+			}
+			stdout.printf ("Choping to get the 4th to 5th DbRecords...\n");
+			var iter2 = itermodel.chop (3,4);
+			stdout.printf ("Iterating over chopped records...\n");
+			while (iter2.next ()) {
+				stdout.printf (iter2.get ().to_string ());
+				var name2 = (string) iter.get().get_value ("name");
+				if (name2 == "Jack" || name2 == "Elsy")
+					stdout.printf ("+++++ PASS\n");
+				else {
+					fails++;
+					stdout.printf ("----- FAIL: " + (fails++).to_string () + "\n");
+				}
 			}
+			stdout.printf ("\n");
+			return fails;
+		}
+		
+		public int filtering ()
+		{
+			int fails = 0;			
+			stdout.printf ("Filtering Records: Any field type STRING with a letter 'x'...\n");
 			
-			stdout.printf ("Choping to get 5th Value = 'Jhon'\n");
-			var iter2 = itermodel.chop (1,0);
-			iter2.next ();
-			var v2 = iter2.get ().get_value ("name");
-			if (v2.type () == typeof (string))
-				stdout.printf ("Name = %s;\n", (string) v2);
-			else {
+			var iter = itermodel.filter (
+			(g) => {
+				bool ret = false;
+				foreach (DbField<Value?> fl in g.fields) {
+					string t = Gda.value_stringify (fl.value);
+					stdout.printf ("Value to check: " + t);
+					if (t.contains ("x")) {
+						ret = true;
+						stdout.printf ("...SELECTED\n");
+						break;
+					}
+					else {
+						ret = false;
+						stdout.printf ("...REJECTED\n");
+					}
+				}
+				if (ret) stdout.printf ("SELECTED ROW: \n" + g.to_string () + "\n");
+				return ret;
+			}
+			);
+			stdout.printf ("Printing Filtered Values...\n");
+			iter.next ();
+			stdout.printf ("Row 1:\n" + iter.get ().to_string () + "\n");
+			iter.next ();
+			stdout.printf ("Row 2:\n" + iter.get ().to_string () + "\n");
+			if(iter.next ()) {
 				fails++;
-				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
-							   " but Value is type = '%s'" +
-							   " with value = %s\n", 
-							   fails, typeof (string).name (), v2.type_name (), 
-							   Gda.value_stringify (v2));			
+				stdout.printf ("(INCORRECT) )Row 3:\n" + iter.get().to_string () + "\n");
+				stdout.printf ("----- FAIL" + fails.to_string () + "\n");
 			}
+			else
+				stdout.printf ("+++++ PASS\n");
+			return fails;
+		}
+		public int streaming ()
+		{
+			int fails = 0;
+			/* FIXME: This code doesn't return YIELDED strings maybe becasue Lazy is not correctly built. 
+				In theory stream() function is working correctly*/
+			stdout.printf ("Streaming Values: Fist field type STRING will be YIELDED...\n");
+			var iter = itermodel.stream<string> (
+				(state, g, out lazy) =>	{
+					if (state == Gee.Traversable.Stream.CONTINUE) {
+						var r = g.value;
+						foreach (DbField<Value?> f in r.fields) {
+							if (f.value.type () == typeof (string)) {
+								stdout.printf ("Value to YIELD: %s\n", Gda.value_stringify (f.value));
+								string ts = "YIELDED Value = " + Gda.value_stringify (f.value) + "\n";
+								lazy = new Gee.Lazy<string>.from_value (ts.dup ());
+							}
+						}
+						return Gee.Traversable.Stream.YIELD;
+					}
+					return Gee.Traversable.Stream.END;
+				}
+			);
+			stdout.printf ("Printing Streamed Values...\n");
+			while (iter.next ()) {
+				stdout.printf (iter.get());
+			}
+			stdout.printf ("\n");
+			return fails;
+		}
+		public int InitIter()
+			throws Error
+		{
+			stdout.printf (">>> NEW TEST: GdaData.RecordCollection & RecordCollectionIterator API tests\n"
+						 	+ ">>> Testing Iterable, Traversable Interfaces\n");
+			int fails = 0;
+			var model = this.connection.execute_select_command ("SELECT * FROM user");
+			stdout.printf ("DataModel rows:\n" + model.dump_as_string ());
+			stdout.printf ("Setting up Table...");
+			var t = new Table<Value?> ();
+			t.connection = this.connection;
+			t.name = "user";
+			stdout.printf ("+++++ PASS\n");
+			stdout.printf ("Setting up DbRecordCollection...\n");
+			itermodel = new RecordCollection (model, t);
+			
+//			stdout.printf ("Getting iterator...\n");
+//			var iter = itermodel.iterator ();
+//			stdout.printf ("Go to first row...\n");
+//			iter.next ();
+//			stdout.printf ("Getting firts row...\n");
+//			var r = iter.get ();
+//			stdout.printf ("First Record contents: \n" + r.to_string ());
+//			stdout.printf ("Iterating over all Records in DataModel\n");
+//			foreach (DbRecord<Value?> record in itermodel) {
+//				stdout.printf (record.to_string ());
+//			}
+//			
+//			stdout.printf ("Choping to get first record id= 1\n");
+//			var iter2 = itermodel.chop (0,0);
+//			iter2.next ();
+//			var v = iter.get ().get_field ("id").value;
+//			if (v.type () == typeof (int))
+//				stdout.printf ("ID=%i\n", (int) v);
+//			else {
+//				fails++;
+//				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
+//							   " but Value is type = '%s'" +
+//							   " with value = %s\n",
+//							   fails, typeof (int).name (), v.type_name (), 
+//							   Gda.value_stringify (v));			
+//			}
+//			
+//			stdout.printf ("Choping to get 5th Value = 'Jhon'\n");
+//			var iter3 = itermodel.chop (1,0);
+//			iter3.next ();
+//			var v2 = iter2.get ().get_value ("name");
+//			if (v2.type () == typeof (string))
+//				stdout.printf ("Name = %s;\n", (string) v2);
+//			else {
+//				fails++;
+//				stdout.printf ("FAIL:'%i': Expected type = '%s'," +
+//							   " but Value is type = '%s'" +
+//							   " with value = %s\n", 
+//							   fails, typeof (string).name (), v2.type_name (), 
+//							   Gda.value_stringify (v2));			
+//			}
 			// FIXME: test filter and stream
 			return fails;
 		}
@@ -133,7 +283,11 @@ namespace Check {
 			stdout.printf ("Checking Gda.DataModelIterator implementation...\n");
 			int failures = 0;
 			var app = new Tests ();
-			failures += app.t1 ();
+			failures += app.InitIter ();
+			failures += app.iterating ();
+			failures += app.streaming ();
+			failures += app.filtering ();
+			failures += app.choping ();
 			return failures != 0 ? 1 : 0;
 		}
 	}



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