-- Test media_content
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(21);

set search_path to camper, public;

select has_table('media_content');
select has_pk('media_content');
select table_privs_are('media_content', 'guest', array['SELECT']);
select table_privs_are('media_content', 'employee', array['SELECT']);
select table_privs_are('media_content', 'admin', array['SELECT', 'INSERT', 'UPDATE', 'DELETE']);
select table_privs_are('media_content', 'authenticator', array[]::text[]);

select has_column('media_content', 'content_hash');
select col_is_pk('media_content', 'content_hash');
select col_type_is('media_content', 'content_hash', 'bytea');
select col_not_null('media_content', 'content_hash');
select col_has_default('media_content', 'content_hash');
select col_default_is('media_content', 'content_hash', 'sha256(bytes)');

select has_column('media_content', 'media_type');
select col_type_is('media_content', 'media_type', 'media_type');
select col_not_null('media_content', 'media_type');
select col_hasnt_default('media_content', 'media_type');

select has_column('media_content', 'bytes');
select col_type_is('media_content', 'bytes', 'bytea');
select col_not_null('media_content', 'bytes');
select col_hasnt_default('media_content', 'bytes');


set client_min_messages to warning;
truncate media_content cascade;
reset client_min_messages;

insert into media_content (media_type, bytes)
values ('text/plain', 'content2')
     , ('text/plain', 'content4')
;

select bag_eq(
	$$ select encode(content_hash, 'hex'), convert_from(bytes, 'utf-8') from media_content $$,
	$$ values ('dab741b6289e7dccc1ed42330cae1accc2b755ce8079c2cd5d4b5366c9f769a6', 'content2')
	        , ('b04813d4f04a27cbd8a5d7828344a0c7d206a486343f503cb7e3d53e1d8e95a0', 'content4')
	$$,
	'Should automatically compute the content_hash'
);


select *
from finish();

rollback;

