예제 #1
0
void TestQgsField::gettersSetters()
{
  QgsField field;
  field.setName( QStringLiteral( "name" ) );
  QCOMPARE( field.name(), QString( "name" ) );
  field.setType( QVariant::Int );
  QCOMPARE( field.type(), QVariant::Int );
  field.setTypeName( QStringLiteral( "typeName" ) );
  QCOMPARE( field.typeName(), QString( "typeName" ) );
  field.setLength( 5 );
  QCOMPARE( field.length(), 5 );
  field.setPrecision( 2 );
  QCOMPARE( field.precision(), 2 );
  field.setComment( QStringLiteral( "comment" ) );
  QCOMPARE( field.comment(), QString( "comment" ) );
  field.setAlias( QStringLiteral( "alias" ) );
  QCOMPARE( field.alias(), QString( "alias" ) );
  field.setDefaultValueExpression( QStringLiteral( "1+2" ) );
  QCOMPARE( field.defaultValueExpression(), QString( "1+2" ) );
  QgsFieldConstraints constraints;
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintNotNull );
  QCOMPARE( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ), QgsFieldConstraints::ConstraintOriginProvider );
  QCOMPARE( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintOriginNotSet );
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginNotSet );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraints(), 0 );
  constraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique | QgsFieldConstraints::ConstraintNotNull );
  constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique );

  constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraintExpression(), QStringLiteral( "constraint expression" ) );
  QCOMPARE( field.constraints().constraintDescription(), QStringLiteral( "description" ) );
  QCOMPARE( field.constraints().constraints(), QgsFieldConstraints::ConstraintUnique | QgsFieldConstraints::ConstraintExpression ); //setting constraint expression should add constraint
  constraints.setConstraintExpression( QStringLiteral( "constraint expression" ), QStringLiteral( "description" ) );

  // check a constraint strength which hasn't been set
  QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ), QgsFieldConstraints::ConstraintStrengthNotSet );
  // check a constraint strength which has not been explicitly set
  QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard );
  constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthSoft );
  // try overwriting a provider constraint's strength
  constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
  constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
  field.setConstraints( constraints );
  QCOMPARE( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ), QgsFieldConstraints::ConstraintStrengthHard );
}
예제 #2
0
void TestQgsField::equality()
{
  QgsField field1;
  field1.setName( QStringLiteral( "name" ) );
  field1.setType( QVariant::Int );
  field1.setLength( 5 );
  field1.setPrecision( 2 );
  field1.setTypeName( QStringLiteral( "typename1" ) ); //typename is NOT required for equality
  field1.setComment( QStringLiteral( "comment1" ) ); //comment is NOT required for equality
  QgsFieldConstraints constraints;
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
  field1.setConstraints( constraints );
  QgsField field2;
  field2.setName( QStringLiteral( "name" ) );
  field2.setType( QVariant::Int );
  field2.setLength( 5 );
  field2.setPrecision( 2 );
  field2.setTypeName( QStringLiteral( "typename2" ) ); //typename is NOT required for equality
  field2.setComment( QStringLiteral( "comment2" ) ); //comment is NOT required for equality
  constraints = field2.constraints();
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
  field2.setConstraints( constraints );
  QVERIFY( field1 == field2 );
  QVERIFY( !( field1 != field2 ) );

  //test that all applicable components contribute to equality
  field2.setName( QStringLiteral( "name2" ) );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setName( QStringLiteral( "name" ) );
  field2.setType( QVariant::Double );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setType( QVariant::Int );
  field2.setLength( 9 );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setLength( 5 );
  field2.setPrecision( 9 );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setPrecision( 2 );
  field2.setAlias( QStringLiteral( "alias " ) );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setAlias( QString() );
  field2.setDefaultValueExpression( QStringLiteral( "1+2" ) );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  field2.setDefaultValueExpression( QString() );
  constraints = field2.constraints();
  constraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
  field2.setConstraints( constraints );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  constraints = field2.constraints();
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginLayer );
  field2.setConstraints( constraints );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  constraints = field2.constraints();
  constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
  constraints.setConstraintExpression( QStringLiteral( "exp" ) );
  field2.setConstraints( constraints );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  constraints = field2.constraints();
  constraints.setConstraintExpression( QStringLiteral( "exp" ), QStringLiteral( "desc" ) );
  field2.setConstraints( constraints );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
  constraints = QgsFieldConstraints();
  constraints.setConstraint( QgsFieldConstraints::ConstraintUnique );
  constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthHard );
  field2.setConstraints( constraints );
  constraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintStrengthSoft );
  field1.setConstraints( constraints );
  QVERIFY( !( field1 == field2 ) );
  QVERIFY( field1 != field2 );
}