Database-Abstraction

 view release on metacpan or  search on metacpan

t/extended_tests.t  view on Meta::CPAN

			ok($s1, 'selectall_array scalar MISS: returns a row');
			my $s2 = $db->selectall_array(entry => 'alpha');
			ok($s2, 'selectall_array scalar HIT (line 1127)');

			# EX13.7 — count() derives from selectall cache (lines 1248-1249)
			# For no_entry+DSN, selectall key is "SELECT * FROM t array" which
			# matches count's derived key "SELECT * FROM t array" exactly.
			my $cache2 = CHI->new(driver => 'RawMemory', global => 0);
			my $db2    = Database::extest->new(dsn => $dsn, cache => $cache2, no_entry => 1);
			$db2->selectall_arrayref();	# populate cache
			my $cnt = $db2->count();
			cmp_ok($cnt, '==', 3,
				'count() returns 3 by reading selectall cache (lines 1248-1249)');

			# EX13.8-9 — fetchrow_hashref scalar: MISS then HIT (line 1374)
			my $fh1 = $db->fetchrow_hashref(entry => 'beta');
			is($fh1->{'colour'}, 'blue', 'fetchrow_hashref scalar MISS');
			my $fh2 = $db->fetchrow_hashref(entry => 'beta');
			is($fh2->{'colour'}, 'blue',
				'fetchrow_hashref scalar HIT (line 1374)');

			# EX13.10 — fetchrow_hashref in wantarray context builds 'array' key (line 1360)
			my @wfh = $db->fetchrow_hashref(entry => 'alpha');
			is($wfh[0]{'score'}, 9.0,
				'fetchrow_hashref wantarray context (line 1360)');
		};
	}

	# -------------------------------------------------------------------------
	# EX14 — AUTOLOAD done_where ternary on CSV non-slurp (lines 1854, 1858)
	# -------------------------------------------------------------------------

	subtest 'EX14: AUTOLOAD done_where ternary on CSV non-slurp' => sub {
		plan tests => 3;

		# max_slurp_size => 0 forces SQL path on CSV so the CSV WHERE guard fires
		# and sets done_where=1.  Subsequent params are ANDed, not WHEREd.
		my $db = Database::test1->new(directory => $DATA_DIR, max_slurp_size => 0);

		# EX14.1 — defined param: " AND entry = ?" is appended (line 1854 done_where=1)
		my $v1;
		lives_ok { $v1 = $db->number(entry => 'one') }
			'AUTOLOAD defined param on CSV non-slurp lives (line 1854)';
		is($v1, 1, 'correct value returned via done_where AND path');

		# EX14.2 — undef param: " AND number IS NULL" is appended (line 1858)
		# The 'empty' row in test1.csv has a blank number column (undef).
		my $v2;
		lives_ok { $v2 = $db->entry(number => undef) }
			'AUTOLOAD undef param on CSV non-slurp lives (line 1858 IS NULL done_where)';
	};
}

# ---------------------------------------------------------------------------
# EX15 — Gzip-compressed CSV (lines 684-691) and DESTROY temp cleanup (line 1920)
# ---------------------------------------------------------------------------

SKIP: {
	skip('Gzip::Faster not available', 3) unless $HAS_GZIP;

	subtest 'EX15: gzip CSV is transparently decompressed (lines 684-691)' => sub {
		plan tests => 3;

		# Build a tiny CSV, gzip it, write to a temp directory
		my $csv_content = "entry!number\n\"uno\"!1\n\"dos\"!2\n";
		my $gzipped     = Gzip::Faster::gzip($csv_content);

		my $tmpdir = tempdir(CLEANUP => 1);
		my $gzfile = File::Spec->catfile($tmpdir, 'testgz.csv.gz');
		open(my $fh, '>', $gzfile) or die "cannot write $gzfile: $!";
		binmode $fh;
		print $fh $gzipped;
		close $fh;

		{
			package Database::testgz;
			use parent 'Database::Abstraction';
		}

		my $db;
		lives_ok { $db = Database::testgz->new(directory => $tmpdir) }
			'gzip CSV instantiation lives (lines 684-691)';
		cmp_ok($db->count(), '==', 2,
			'gzip CSV returns correct row count');
		ok(defined($db->{'_temp_fh'}), 'File::Temp object stored in _temp_fh after gunzip (line 691)');
		# DESTROY fires when $db goes out of scope, unlinks temp file (line 1920)
	};
}

done_testing();



( run in 1.545 second using v1.01-cache-2.11-cpan-7fcb06a456a )