113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nulib\db\sqlite;
 | |
| 
 | |
| use nulib\db\Capacitor;
 | |
| use nulib\db\sqlite\impl\MyChannel;
 | |
| use nulib\db\sqlite\impl\MyChannelV2;
 | |
| use nulib\db\sqlite\impl\MyChannelV3;
 | |
| use nulib\db\sqlite\impl\MyIndexChannel;
 | |
| use nulib\output\msg;
 | |
| use nulib\output\std\StdMessenger;
 | |
| use nulib\php\time\DateTime;
 | |
| use nulib\tests\TestCase;
 | |
| 
 | |
| class ChannelMigrationTest extends TestCase {
 | |
|   static function setUpBeforeClass(): void {
 | |
|     parent::setUpBeforeClass();
 | |
|     msg::set_messenger_class(StdMessenger::class);
 | |
|   }
 | |
| 
 | |
|   protected function addData(MyChannel $channel, array $data): void {
 | |
|     foreach ($data as [$name, $value, $dateCre, $dateMod, $age]) {
 | |
|       $channel->charge([
 | |
|         "name" => $name,
 | |
|         "value" => $value,
 | |
|         "date_cre" => $dateCre,
 | |
|         "date_mod" => $dateMod,
 | |
|         "age" => $age,
 | |
|       ]);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   function testMigration() {
 | |
|     $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | |
|     $data = [
 | |
|       ["first", "premier", new DateTime(), new DateTime(), 15],
 | |
|       ["second", "deuxieme", new DateTime(), new DateTime(), 15],
 | |
|     ];
 | |
| 
 | |
|     new Capacitor($storage, $channel = new MyChannel());
 | |
|     $channel->reset(true);
 | |
|     $this->addData($channel, $data);
 | |
| 
 | |
|     new Capacitor($storage, $channel = new MyChannelV2());
 | |
|     $this->addData($channel, $data);
 | |
| 
 | |
|     new Capacitor($storage, $channel = new MyChannelV3());
 | |
|     $this->addData($channel, $data);
 | |
| 
 | |
|     $sql = $channel->getCapacitor()->getCreateSql();
 | |
|     $class = MyChannelV3::class;
 | |
|     $expected = <<<EOT
 | |
| -- -*- coding: utf-8 mode: sql -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| -- autogénéré à partir de $class
 | |
| 
 | |
| -- 0init
 | |
| create table  if not exists my (
 | |
|   id_ integer primary key autoincrement
 | |
| , name varchar
 | |
| , value varchar
 | |
| , item__ mediumtext
 | |
| , item__sum_ varchar(40)
 | |
| , created_ datetime
 | |
| , modified_ datetime
 | |
| );
 | |
| 
 | |
| -- dates
 | |
| alter table my add column date_cre datetime;
 | |
| alter table my add column date_mod datetime;
 | |
| 
 | |
| -- infos
 | |
| alter table my add column age integer;
 | |
| 
 | |
| EOT;
 | |
|     self::assertSame($expected, $sql);
 | |
|   }
 | |
| 
 | |
|   function testMigrationIndex() {
 | |
|     $storage = new SqliteStorage(__DIR__.'/capacitor.db');
 | |
|     $data = [
 | |
|       ["un", "premier", "first"],
 | |
|       ["deux", "deuxieme", "second"],
 | |
|     ];
 | |
| 
 | |
|     new Capacitor($storage, $channel = new MyIndexChannel());
 | |
|     $channel->reset(true);
 | |
|     $channel->chargeAll($data);
 | |
| 
 | |
|     $sql = $channel->getCapacitor()->getCreateSql();
 | |
|     $class = MyIndexChannel::class;
 | |
|     $expected = <<<EOT
 | |
| -- -*- coding: utf-8 mode: sql -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| -- autogénéré à partir de $class
 | |
| 
 | |
| -- 0init
 | |
| create table  if not exists my_index (
 | |
|   name varchar not null primary key
 | |
| , first varchar
 | |
| , second varchar
 | |
| , item__ mediumtext
 | |
| , item__sum_ varchar(40)
 | |
| , created_ datetime
 | |
| , modified_ datetime
 | |
| );
 | |
| 
 | |
| -- index
 | |
| create index my_index_first on my_index(first);
 | |
| create index my_index_second on my_index(second);
 | |
| 
 | |
| EOT;
 | |
|     self::assertSame($expected, $sql);
 | |
|   }
 | |
| }
 |