Selaa lähdekoodia

[spod] Added tests for update check

Cauê Waneck 11 vuotta sitten
vanhempi
commit
1efb69349a
3 muutettua tiedostoa jossa 69 lisäystä ja 2 poistoa
  1. 9 2
      std/sys/db/Manager.hx
  2. 7 0
      tests/unit/MySpodClass.hx
  3. 53 0
      tests/unit/TestSpod.hx

+ 9 - 2
std/sys/db/Manager.hx

@@ -211,6 +211,13 @@ class Manager<T : Object> {
 	function doUpdate( x : T ) {
 		if( untyped !x._lock )
 			throw "Cannot update a not locked object";
+		var upd = getUpdateStatement(x);
+		if (upd == null) return;
+		unsafeExecute(upd);
+	}
+
+	function getUpdateStatement( x : T ):Null<String>
+	{
 		unmake(x);
 		var s = new StringBuf();
 		s.add("UPDATE ");
@@ -242,10 +249,10 @@ class Manager<T : Object> {
 			}
 		}
 		if( !mod )
-			return;
+			return null;
 		s.add(" WHERE ");
 		addKeys(s,x);
-		unsafeExecute(s.toString());
+		return s.toString();
 	}
 
 	function doDelete( x : T ) {

+ 7 - 0
tests/unit/MySpodClass.hx

@@ -22,6 +22,13 @@ import sys.db.Types;
   public var anEnum:SEnum<SpodEnum>;
 }
 
+@:keep class NullableSpodClass extends Object
+{
+	public var theId:SId;
+  @:relation(rnid) public var relationNullable:Null<OtherSpodClass>;
+  public var data:Null<SData<Array<ComplexClass>>>;
+}
+
 @:keep class ComplexClass
 {
 	public var val : { name:String, array:Array<String> };

+ 53 - 0
tests/unit/TestSpod.hx

@@ -19,8 +19,10 @@ class TestSpod extends Test
 		Manager.cnx = cnx;
 		try cnx.request('DROP TABLE MySpodClass') catch(e:Dynamic) {}
 		try cnx.request('DROP TABLE OtherSpodClass') catch(e:Dynamic) {}
+		try cnx.request('DROP TABLE NullableSpodClass') catch(e:Dynamic) {}
 		TableCreate.create(MySpodClass.manager);
 		TableCreate.create(OtherSpodClass.manager);
+		TableCreate.create(NullableSpodClass.manager);
 	}
 
 	private function setManager()
@@ -51,6 +53,57 @@ class TestSpod extends Test
 		return scls;
 	}
 
+	public function testUpdate()
+	{
+		setManager();
+		var c1 = new OtherSpodClass("first spod");
+		c1.insert();
+		var c2 = new OtherSpodClass("second spod");
+		c2.insert();
+		var scls = getDefaultClass();
+		scls.relation = c1;
+		scls.relationNullable = c2;
+		scls.insert();
+
+		var id = scls.theId;
+
+		//if no change made, update should return nothing
+		eq( untyped MySpodClass.manager.getUpdateStatement( scls ), null );
+		Manager.cleanup();
+		scls = MySpodClass.manager.get(id);
+		eq( untyped MySpodClass.manager.getUpdateStatement( scls ), null );
+		scls.delete();
+
+		//try now with null SData and null relation
+		var scls = new NullableSpodClass();
+		scls.insert();
+
+		var id = scls.theId;
+
+		//if no change made, update should return nothing
+		eq( untyped NullableSpodClass.manager.getUpdateStatement( scls ), null );
+		Manager.cleanup();
+		scls = NullableSpodClass.manager.get(id);
+		eq( untyped NullableSpodClass.manager.getUpdateStatement( scls ), null );
+		scls.delete();
+
+		//same thing with explicit null set
+		var scls = new NullableSpodClass();
+		scls.data = null;
+		scls.relationNullable = null;
+		scls.insert();
+
+		var id = scls.theId;
+
+		//if no change made, update should return nothing
+		eq( untyped NullableSpodClass.manager.getUpdateStatement( scls ), null );
+		Manager.cleanup();
+		scls = NullableSpodClass.manager.get(id);
+		eq( untyped NullableSpodClass.manager.getUpdateStatement( scls ), null );
+		scls.delete();
+
+	}
+
 	public function testSpodTypes()
 	{
 		setManager();