<commit_before>use Test::More; # tests => 1;
use strict;
use warnings;

use Log::Report;

use t::lib::DataSheet;

# Don't create users, as the normal find_or_create check won't find the
# existing user at that ID due to the site_id constraint and will then fail
my $sheet_site1 = t::lib::DataSheet->new(site_id => 1);
my $schema      = $sheet_site1->schema;
$sheet_site1->create_records;
# Set the site_id. This should be use throughout the next data sheet creation
$schema->site_id(2);
my $sheet_site2 = t::lib::DataSheet->new(schema => $schema, instance_id => 2, curval_offset => 6);
$sheet_site2->create_records;

# Check site 1 records
$schema->site_id(1);
my $records_site1 = GADS::Records->new(
    user    => undef,
    layout  => $sheet_site1->layout,
    schema  => $schema,
);
is( $records_site1->count, 2, "Correct number of records in site 1" );
my @current_ids = map { $_->current_id } @{$records_site1->results};
is( "@current_ids", "1 2", "Current IDs correct for site 1" );
# Try and access record from site 2
my $record = GADS::Record->new(
    user   => undef,
    layout => $sheet_site1->layout,
    schema => $schema,
);
is( $record->find_current_id(1)->current_id, 1, "Retrieved record from same site (1)" );
$record->clear;
try {$record->find_current_id(3)};
ok( $@, "Failed to retrieve record from other site (2)" );

# Site 2 tests
$schema->site_id(2);
my $records_site2 = GADS::Records->new(
    user    => undef,
    layout  => $sheet_site2->layout,
    schema  => $schema,
);

is( $records_site2->count, 2, "Correct number of records in site 2" );
@current_ids = map { $_->current_id } @{$records_site2->results};
is( "@current_ids", "3 4", "Current IDs correct for site 2" );

# Try and access record from site 1
$record = GADS::Record->new(
    user   => undef,
    layout => $sheet_site2->layout,
    schema => $schema,
);
is( $record->find_current_id(3)->current_id, 3, "Retrieved record from same site (2)" );
$record->clear;
try {$record->find_current_id(1)};
ok( $@, "Failed to retrieve record from other site (1)" );

# Try and access columns between layouts using ID
my $string_site1 = $sheet_site1->columns->{string1};
ok(!$sheet_site2->layout->column($string_site1->id), "Failed to access column from other site by ID");
# And reverse
my $string_site2 = $sheet_site2->columns->{string1};
ok(!$sheet_site1->layout->column($string_site2->id), "Failed to access column from other site by ID - reverse");

# Then same with short name
ok(!$sheet_site2->layout->column_by_name_short($string_site1->name_short), "Failed to access column from other site by short name");

done_testing();<commit_msg>Check correct instances returned for multisite<commit_after>use Test::More; # tests => 1;
use strict;
use warnings;

use Log::Report;

use t::lib::DataSheet;

# Don't create users, as the normal find_or_create check won't find the
# existing user at that ID due to the site_id constraint and will then fail
my $sheet_site1 = t::lib::DataSheet->new(site_id => 1);
my $schema      = $sheet_site1->schema;
$sheet_site1->create_records;
# Set the site_id. This should be use throughout the next data sheet creation
$schema->site_id(2);
my $sheet_site2 = t::lib::DataSheet->new(schema => $schema, instance_id => 2, curval_offset => 6);
$sheet_site2->create_records;

# Check site 1 records
$schema->site_id(1);
my $records_site1 = GADS::Records->new(
    user    => undef,
    layout  => $sheet_site1->layout,
    schema  => $schema,
);
is( $records_site1->count, 2, "Correct number of records in site 1" );
my @current_ids = map { $_->current_id } @{$records_site1->results};
is( "@current_ids", "1 2", "Current IDs correct for site 1" );
# Try and access record from site 2
my $record = GADS::Record->new(
    user   => undef,
    layout => $sheet_site1->layout,
    schema => $schema,
);
is( $record->find_current_id(1)->current_id, 1, "Retrieved record from same site (1)" );
$record->clear;
try {$record->find_current_id(3)};
ok( $@, "Failed to retrieve record from other site (2)" );

# Site 2 tests
$schema->site_id(2);
my $records_site2 = GADS::Records->new(
    user    => undef,
    layout  => $sheet_site2->layout,
    schema  => $schema,
);

is( $records_site2->count, 2, "Correct number of records in site 2" );
@current_ids = map { $_->current_id } @{$records_site2->results};
is( "@current_ids", "3 4", "Current IDs correct for site 2" );

# Try and access record from site 1
$record = GADS::Record->new(
    user   => undef,
    layout => $sheet_site2->layout,
    schema => $schema,
);
is( $record->find_current_id(3)->current_id, 3, "Retrieved record from same site (2)" );
$record->clear;
try {$record->find_current_id(1)};
ok( $@, "Failed to retrieve record from other site (1)" );

# Try and access columns between layouts using ID
my $string_site1 = $sheet_site1->columns->{string1};
ok(!$sheet_site2->layout->column($string_site1->id), "Failed to access column from other site by ID");
# And reverse
my $string_site2 = $sheet_site2->columns->{string1};
ok(!$sheet_site1->layout->column($string_site2->id), "Failed to access column from other site by ID - reverse");

# Then same with short name
ok(!$sheet_site2->layout->column_by_name_short($string_site1->name_short), "Failed to access column from other site by short name");

# Check that only one site's instances are returned
my $instances = GADS::Instances->new(schema => $schema, user => undef, user_permission_override => 1);
is(@{$instances->all}, 1, "Only one instance returned for site");

done_testing();
{'fraction_non_alphanumeric': 0.15472519499365137, 'fraction_numerical': 0.02140395428986033, 'mean_word_length': 4.878464818763327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2915226', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1808, 'n_words': 570}