Skip to content

Commit

Permalink
MDL-40883 ddl: unittest for objects name length
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Nov 8, 2013
1 parent dcde391 commit a9d11e3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/ddl/tests/ddl_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,59 @@ public function test_char_size_limit() {
}
}

public function test_object_name() {
$gen = $this->tdb->get_manager()->generator;

// This will form short object name and max length should not be exceeded.
$table = 'tablename';
$fields = 'id';
$suffix = 'pk';
for ($i=0; $i<12; $i++) {
$this->assertLessThanOrEqual($gen->names_max_length,
strlen($gen->getNameForObject($table, $fields, $suffix)),
'Generated object name is too long. $i = '.$i);
}

// This will form too long object name always and it must be trimmed to exactly 30 chars.
$table = 'aaaa_bbbb_cccc_dddd_eeee_ffff_gggg';
$fields = 'aaaaa,bbbbb,ccccc,ddddd';
$suffix = 'idx';
for ($i=0; $i<12; $i++) {
$this->assertEquals($gen->names_max_length,
strlen($gen->getNameForObject($table, $fields, $suffix)),
'Generated object name is too long. $i = '.$i);
}

// Same test without suffix.
$table = 'bbbb_cccc_dddd_eeee_ffff_gggg_hhhh';
$fields = 'aaaaa,bbbbb,ccccc,ddddd';
$suffix = '';
for ($i=0; $i<12; $i++) {
$this->assertEquals($gen->names_max_length,
strlen($gen->getNameForObject($table, $fields, $suffix)),
'Generated object name is too long. $i = '.$i);
}

// This must only trim name when counter is 10 or more.
$table = 'cccc_dddd_eeee_ffff_gggg_hhhh_iiii';
$fields = 'id';
$suffix = 'idx';
// Since we don't know how long prefix is, loop to generate tablename that gives exactly maxlengh-1 length.
// Skip this test if prefix is too long.
while (strlen($table) && strlen($gen->prefix.preg_replace('/_/','',$table).'_id_'.$suffix) >= $gen->names_max_length) {
$table = rtrim(substr($table, 0, strlen($table) - 1), '_');
}
if (strlen($table)) {
$this->assertEquals($gen->names_max_length - 1,
strlen($gen->getNameForObject($table, $fields, $suffix)));
for ($i=0; $i<12; $i++) {
$this->assertEquals($gen->names_max_length,
strlen($gen->getNameForObject($table, $fields, $suffix)),
'Generated object name is too long. $i = '.$i);
}
}
}

// Following methods are not supported == Do not test.
/*
public function testRenameIndex() {
Expand Down

0 comments on commit a9d11e3

Please sign in to comment.