### ----------------------------------------------------
### -- UpRooted
### -- Licenses: Artistic-2.0
### -- Authors: Paweł bbkr Pabian
### -- File: t/11-schema-mysql.rakutest
### ----------------------------------------------------
use lib 'lib';
use lib 't/lib';

use Test;

use DBITest;
use UpRooted::Schema::MySQL;

my $connection = connect( 'mysql' );
load( $connection, 'mysql', 'cleanup.sql' );

plan 14;

my $schema;
lives-ok { $schema = UpRooted::Schema::MySQL.new( :$connection ) }, 'UpRooted::Schema discovered';

is-deeply $schema.tables, (), 'UpRooted::Schema is empty';

load( $connection, 'mysql', 'schema.sql' );
lives-ok { $schema = UpRooted::Schema::MySQL.new( :$connection ) }, 'UpRooted::Schema discovered';

# views should not be present here
is-deeply $schema.tables.map( { .name } ), ( 't1', 't2', 't3', 't4', 't5', 't6', 't7', 't8', 't9' ), 'UpRooted::Tables present';

# no need to check every UpRooted::Table
# TODO: lack of virtual columns test, difficult to perform on older MySQL versions
is-deeply $schema.table( 't1' ).columns.map( { .name } ), ( 'id', 'c_01', 'c_02', 'c_03', 'c_04', 'c_05', 'c_06', 'c_07', 'c_08', 'c_09', 'c_10', 'c_11', 'c_12', 'c_13', 'c_14', 'c_15', 'c_16', 'c_17', 'c_18' ), 'UpRooted::Columns present';

# TODO: JSON type, difficult to perform on older MySQL versions
is-deeply $schema.table( 't1' ).columns.map( { .type } ), ( 'bigint', 'char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'decimal', 'float', 'double', 'tinyblob', 'blob', 'mediumblob', 'longblob' ), 'UpRooted::Columns types';

# no need to check every UpRooted::Column, just both cases
is $schema.table( 't1' ).columns( 'id', 'c_01' ).map( { .is-nullable } ), ( False, True ), 'UpRooted::Columns nullability';

# check which UpRooted::Tables are connected with UpRooted::Relations,
# no need to check all of them, just specific cases
is-deeply $schema.table( 't1' ).children-relations.map( { .child-table.name } ), ( 't2', 't3', 't4', 't6', 't7', 't8', 't9', 't9' ), 'UpRooted::Relations present';
is-deeply $schema.table( 't4' ).children-relations.map( { .child-table.name } ), ( 't5', ), 'Multi column UpRooted::Relations present';
is-deeply $schema.table( 't6' ).children-relations.map( { .child-table.name } ), ( 't6', ), 'Self loop UpRooted::Relations present';
is-deeply $schema.table( 't7' ).children-relations.map( { .child-table.name } ), ( 't8', ), 'Long loop UpRooted::Relations present';
is-deeply $schema.table( 't8' ).children-relations.map( { .child-table.name } ), ( 't7', ), 'Long loop UpRooted::Relations present';

# no need to check every UpRooted::Relation,
# just parent and child UpRooted::Columns in one of them
is-deeply $schema.table( 't4' ).children-relations[0].parent-columns.map( { .name } ), ( 't1_id', 't2_id' ), 'UpRooted::Relation parent UpRooted::Columns';
is-deeply $schema.table( 't4' ).children-relations[0].child-columns.map( { .name } ), ( 't4_t1_id', 't4_t2_id' ), 'UpRooted::Relation child UpRooted::Columns';

# WARNING: check only if information_schema was read correctly,
# do not repeat exhaustive UpRooted::Schema tests here (they are already present),
# do not write UpRooted::Tree and UpRooted::Reader related tests (they are in separate file)

load( $connection, 'mysql', 'cleanup.sql' );


