### ----------------------------------------------------
### -- UpRooted
### -- Licenses: Artistic-2.0
### -- Authors: Paweł bbkr Pabian
### -- File: t/41-writer-mysqlfile.rakutest
### ----------------------------------------------------
use lib 'lib';

use Test;

use UpRooted::Schema;
use UpRooted::Table;
use UpRooted::Column;
use UpRooted::Relation;
use UpRooted::Tree;
use UpRooted::Reader;
use UpRooted::Writer::MySQLFile;

plan 3;

class UpRooted::Reader::Dummy does UpRooted::Reader {

    method !read-path ( $path, %conditions ) {
        
        given $path.leaf-table.name {
            when 't1' {
                take [ 1, Buf.new( 0x00, 0x01 ) ]
            }
            when 't2' {
                take [ 1, Buf.new( 0xF0, 0x9F, 0x98, 0x80 ) ];
                take [ 1, Any:U ];
            }
        
        }
    }
}

my $schema = UpRooted::Schema.new( name => 's1' );
UpRooted::Table.new( :$schema, name => 't1' );
UpRooted::Table.new( :$schema, name => 't2' );
UpRooted::Table.new( :$schema, name => 't3' );
UpRooted::Column.new( table => $schema.table( 't1' ), name => 'c1', :!is-nullable, type => 'int' );
UpRooted::Column.new( table => $schema.table( 't1' ), name => 'c2', :!is-nullable, type => 'blob' );
UpRooted::Column.new( table => $schema.table( 't2' ), name => 'c3', :!is-nullable );
UpRooted::Column.new( table => $schema.table( 't2' ), name => 'c4', :!is-nullable, type => 'text' );
UpRooted::Column.new( table => $schema.table( 't3' ), name => 'c5', :!is-nullable );
UpRooted::Relation.new(
    parent-columns => $schema.table( 't1' ).columns( 'c1' ),
    child-columns => $schema.table( 't2' ).columns( 'c3' ),
    name => 'r1'
);
UpRooted::Relation.new(
    parent-columns => $schema.table( 't1' ).columns( 'c1' ),
    child-columns => $schema.table( 't3' ).columns( 'c5' ),
    name => 'r2'
);
my $tree = UpRooted::Tree.new( root-table => $schema.table( 't1' ) );
my $reader = UpRooted::Reader::Dummy.new( :$tree );
my $writer = UpRooted::Writer::MySQLFile.new;

# clear if previous test run failed
unlink 'out.sql', 's1_1.sql';

$writer.write( $reader, c1 => 1 );

my $got-file = 'out.sql'.IO.slurp;
my $expected-file = q:to/END/;
    BEGIN;
    INSERT INTO `s1`.`t1` ( `c1`, `c2` ) VALUES ( '1', UNHEX( '0001' ) );
    INSERT INTO `s1`.`t2` ( `c3`, `c4` ) VALUES ( '1', '😀' );
    INSERT INTO `s1`.`t2` ( `c3`, `c4` ) VALUES ( '1', NULL );
    COMMIT;
    END

is $got-file, $expected-file, 'File generated';
unlink 'out.sql';

UpRooted::Writer::MySQLFile.new( :!use-schema-name ).write( $reader, c1 => 1 );
$got-file = 'out.sql'.IO.slurp;
ok $got-file.contains( 'INSERT INTO `t1`' ), 'Without UpRooted::Schema name';
unlink 'out.sql';

UpRooted::Writer::MySQLFile.new( 
    file-naming => sub ( $tree, %conditions ) {
        return $tree.root-table.schema.name ~ '_' ~ %conditions{ 'c1' } ~ '.sql';
    }
).write( $reader, c1 => 1 );
ok 's1_1.sql'.IO ~~ :e, 'With custom naming';
unlink 's1_1.sql';


