Ejemplo n.º 1
0
void SqlBackendTest::transactions()
{
	ObjectRef<CustomerOrder> order = CustomerOrder::create();
	order->setNumber( 50000 );
	order->setDate( QDate::currentDate() );
	order->setOrder( order );

	ObjectRef<Article> a1 = Article::create();
	a1->setCode( "1" );
	a1->setLabel( "Article One" );
	a1->setDescription( "Description of article number one" );
	order->articles()->add( a1 );

	ObjectRef<Article> a2 = Article::create();
	a2->setCode( "2" );
	a2->setLabel( "Article Two" );
	a2->setDescription( "Description of article number two" );
	a1->setArticle( a2 );

	ObjectRef<Customer> c = Customer::create();
	c->setCode( "0001" );
	c->setCustomerName( "Customer One" );
	c->setAddress( "Street" );
	c->setCity( "City" );
	c->setZipCode( "Zip Code" );
	c->setCountry( "Country" );
	c->setDefaultArticle( a1 );
	c->setSecondDefaultArticle( a2 );
	c->discountedArticles()->add( a1 );
	c->discountedArticles()->add( a2 );
	c->adaptedArticles()->add( a1 );
	c->adaptedArticles()->add( a2 );
	order->setCustomer( c );

	CHECK( Manager::self()->commit(), true );
	
	// Check data has been saved correctly to the database
	QSqlCursor cursor( "customerorder" );
	cursor.select( "number = 50000" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "date" ).toDate().toString() , QDate::currentDate().toString() );
	CHECK( variantToOid( cursor.value( "customerorder_customerorder" ) ), variantToOid( cursor.value( "dboid" ) ) );
	CHECK( variantToOid( cursor.value( "customer_customerorder" ) ), c->oid() );
	CHECK( cursor.contains( "numberOfArticles" ), false );
	CHECK( cursor.next(), false ); 

	cursor.setName( "article" );
	cursor.select( "code = '1'" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "label" ).toString(), QString("Article One") );
	CHECK( cursor.value( "description" ).toString(), QString("Description of article number one") );
	CHECK( cursor.next(), false );

	cursor.setName( "article_customerorder" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customerorder = " + oidToString( order->oid() ) );
	CHECK( cursor.next(), true );
	CHECK( cursor.next(), false );

	cursor.setName( "discounted_articles" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customer = " + oidToString( c->oid() ) );
	CHECK( cursor.next(), true );
	CHECK( cursor.next(), false );
	
	cursor.setName( "adapted_articles" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customer = " + oidToString( c->oid() ) );
	CHECK( cursor.next(), true );
	CHECK( cursor.next(), false );

//	exit();
		
	cursor.setName( "article" );
	cursor.select( "code = '2'" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "label" ).toString(), QString("Article Two") );
	CHECK( cursor.value( "description" ).toString(), QString("Description of article number two") );
	CHECK( cursor.next(), false );
	
	cursor.setName( "customer" );
	cursor.select( "code = '0001'" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "customername" ).toString(), QString("Customer One") );
	CHECK( cursor.value( "address" ).toString(), QString("Street") );
	CHECK( cursor.value( "city" ).toString(), QString("City") );
	CHECK( cursor.value( "zipcode" ).toString(), QString("Zip Code") );
	CHECK( cursor.value( "country" ).toString(), QString("Country") );
	CHECK( variantToOid( cursor.value( "default_article" ) ), a1->oid() );
	CHECK( variantToOid( cursor.value( "second_default_article" ) ), a2->oid() );
	CHECK( cursor.next(), false );
	
	
	a1->setDescription( "MODIFIED description of article number one" );
	// Check data has been saved correctly to the database
	CHECK( Manager::self()->commit(), true );
	cursor.setName( "article" );
	cursor.select( "code = '1'" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "description" ).toString(), QString("MODIFIED description of article number one") );
	CHECK( cursor.next(), false );

	ObjectRef<Customer> c2 = Customer::create();
	c2->setCode( "0002" );
	c2->setCustomerName( "Customer Two" );
	c2->setAddress( "Street" );
	c2->setCity( "City" );
	c2->setZipCode( "Zip Code" );
	c2->setCountry( "Country" );
	order->setCustomer( c2 );
	CHECK( Manager::self()->commit(), true );
	
	// Check data has been saved correctly to the database
	cursor.setName( "customer" );
	cursor.select( "code = '0002'" );
	CHECK( cursor.next(), true );
	CHECK( cursor.value( "customername" ).toString(), QString("Customer Two") );
	CHECK( cursor.value( "address" ).toString(), QString("Street") );
	CHECK( cursor.value( "city" ).toString(), QString("City") );
	CHECK( cursor.value( "zipcode" ).toString(), QString("Zip Code") );
	CHECK( cursor.value( "country" ).toString(), QString("Country") );
	CHECK( cursor.next(), false );
	
	cursor.setName( "customerorder" );
	cursor.select( "number = 50000" );
	CHECK( cursor.next(), true );
	CHECK( variantToOid( cursor.value( "customer_customerorder" ) ), variantToOid( c2->oid() ) );
	CHECK( cursor.next(), false );

	order->articles()->add( a2 );
	CHECK( Manager::self()->commit(), true );
	
	// Check still both articles are in the database
	cursor.setName( "article_customerorder" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customerorder = " + oidToString( order->oid() ) );
	CHECK( cursor.next(), true );
	CHECK( cursor.next(), false );
	cursor.select( "article = " + oidToString( a2->oid() ) + " AND customerorder = " + oidToString( order->oid() ) );
	CHECK( cursor.next(), true );
	CHECK( cursor.next(), false );

	order->articles()->remove( a1 );
	CHECK( Manager::self()->commit(), true );

	// Check a1 has been removed from the list
	cursor.setName( "article_customerorder" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customerorder = " + oidToString( order->oid() ) );
	CHECK( cursor.next(), false );

	order->articles()->add( a1 );
	CHECK( Manager::self()->rollback(), true );

	// Check a1 hasn't appeared again
	cursor.setName( "article_customerorder" );
	cursor.select( "article = " + oidToString( a1->oid() ) + " AND customerorder = " + oidToString( order->oid() ) );
	CHECK( cursor.next(), false );

	order->setCustomer( c );
	CHECK( Manager::self()->commit(), true );
	
	cursor.setName( "customerorder" );
	cursor.select( "number = 50000" );
	CHECK( cursor.next(), true );
	CHECK( variantToOid( cursor.value( "customer_customerorder" ) ), c->oid() );
	CHECK( cursor.next(), false );
	

	order->setCustomer( c2 );
	CHECK( Manager::self()->rollback(), true );

	// Check customer hasn't changed
	cursor.setName( "customerorder" );
	cursor.select( "number = 50000" );
	CHECK( cursor.next(), true );
	CHECK( variantToOid( cursor.value( "customer_customerorder" ) ), c->oid() );
	CHECK( cursor.next(), false );

	CHECK( Manager::self()->commit(), true );

	// Check customer hasn't changed
	cursor.setName( "customerorder" );
	cursor.select( "number = 50000" );
	CHECK( cursor.next(), true );
	CHECK( variantToOid( cursor.value( "customer_customerorder" ) ), c->oid() );
	CHECK( cursor.next(), false );

	Manager::self()->remove( c2 );
	CHECK( Manager::self()->commit(), true );
	
		// Check customer hasn't changed
	cursor.setName( "customer" );
	cursor.select( "code = '0002'" );
	CHECK( cursor.next(), false );
}
void DynamicObjectsTest::allTests()
{
	QString dbname = "testdynamic";
	
	Classes::setup();

	Classes::addClass( "Test", DynamicObject::createInstance, 0 );
	ClassInfo *ci = Classes::classInfo( "Test" );
	ci->addObject( "Customer", "Customer_Test", &Customer::createInstance );
	ci->addCollection( "Article", "Article_Test" );

	PropertyInfo *p;
	
	p = new PropertyInfo();
	p->setName( "Property1" );
	p->setType( QVariant::String );
	ci->addProperty( p );
	
	p = new PropertyInfo();
	p->setName( "Property2" );
	p->setType( QVariant::ULongLong );
	ci->addProperty( p );

	Classes::setupRelations();

	// Drop the database if already exists
	KProcess *proc = new KProcess;
	*proc << "dropdb";
	*proc << dbname;
	proc->start();
	proc->wait();
	delete proc;

	// Create the database
	proc = new KProcess;
	*proc << "createdb";
	*proc << dbname;
	CHECK( proc->start(), true );
	proc->wait();
	if ( ! proc->normalExit() || proc->exitStatus() != 0 ) {
		CHECK( true, false );
		delete proc;
		return;
	}
	delete proc;

	QSqlDatabase *db = QSqlDatabase::addDatabase( "QPSQL7" );
	db->setDatabaseName( dbname );
	db->setUserName( "ak213" );
	db->setPassword( "ak" );
	db->setHostName( "localhost" );
	if ( ! db->open() ) {
		kdDebug() << "Failed to open database: " << db->lastError().text() << endl;
		return;
	}
	DbBackendIface *backend = new SqlDbBackend( db );

	m_manager = new Manager( backend );
	m_manager->setMaxObjects( 1 );
	m_manager->createSchema();


	ObjectRef<Customer> customer = Customer::create();
	customer->setCustomerName( "Name of the customer" );

	ObjectRef<Article> a1 = Article::create();
	a1->setCode( "00001" );
	ObjectRef<Article> a2 = Article::create();
	a2->setCode( "00002" );

	ObjectRef<Object> obj = Classes::classInfo( "Test" )->create();
	CHECK( obj->property( "Property1" ).type(), QVariant::String );
	CHECK( obj->property( "Property2" ).type(), QVariant::ULongLong );
	CHECK( obj->containsObject( "Customer_Test" ), true );
	CHECK( obj->containsCollection( "Article_Test" ), true );
	obj->setProperty( "Property1", "Property number one" );
	obj->setProperty( "Property2", 2 );
	CHECK( obj->property( QString( "Property1" ) ).value().toString(), QString( "Property number one" ) );
	CHECK( obj->property( QString( "Property2" ) ).value().toULongLong(), 2 );
	
	obj->setObject( "Customer_Test", customer );
	obj->collection( "Article_Test" )->add( a1 );
	obj->collection( "Article_Test" )->add( a2 );

	m_manager->commit();

	CHECK( obj->property( "Property1" ).value().toString(), QString( "Property number one" ) );

	delete m_manager;
}