 -- This setup needs to run after webs setup.
+create or replace function startup_20_users()
+returns setof text as $test$
+	declare
+		nameholder	text;
+		userid		uuid;
+		grpid		uuid;
+	begin
+		delete from users.test_user;
+		loop
+			select md5(random()::text) into nameholder;
+			select id into userid
+				from users.user 
+				where name = lower(nameholder)
+					or email = lower(nameholder);
+			if found then
+				continue;
+			end if;
+			insert into users.test_user (name) 
+				values (nameholder);
+			if (select count(*) > 5 from users.test_user) then
+				insert into users.user (id, name, password, email)
+					values (public.uuid_generate_v1(), nameholder,
+					public.crypt('password', public.gen_salt('bf')),
+					nameholder);
+			end if;
+			if (select count(*) > 10 from users.test_user) then
+				update users.user set active = true 
+					where name = lower(nameholder);
+			end if;
+			exit when (select count(*) > 14 from users.test_user);
+		end loop;
+		delete from users.test_group;
+		loop 
+			select md5(random()::text) into nameholder;
+			select id into grpid
+				from users.group
+				where name = lower(nameholder);
+			if found then
+				continue;
+			end if;
+			insert into users.test_group (name)
+				values (nameholder);
+			if (select count(*) > 5 from users.test_group) then 
+				insert into users.group (id, name) values
+					(public.uuid_generate_v1(), nameholder);
+			end if;
+			exit when (select count(*) > 9 from users.test_group);
+		end loop;
+	exception
+		when invalid_schema_name or  undefined_function or
+			undefined_table or undefined_column then
+			--do nothing
+	end;
+$test$ language plpgsql;
+
+create or replace function setup_20_users()
+returns setof text as $test$
+	begin
+		update web.session 
+			set user_id = 
+				(select 
+					users.group_user_link.user_id 
+				from users.group,
+					users.group_user_link
+				where 
+					users.group.id = users.group_user_link.group_id
+					and users.group.name = 'admin'
+				limit 1)
+			where web.session.sess_id = 'web-session-1';
+	exception
+		when invalid_schema_name or undefined_function or
+			undefined_table or undefined_column then
+			--do nothing
+	end; 
+$test$ language plpgsql;
+
 create or replace function test_users_schema()
 returns setof text as $$
 	begin 
 		return next has_schema('users', 'There should be a users schema.');
 	end;
 $$ language plpgsql;
+
+create or replace function test_users_table_testusers_exists()
+returns setof text as $test$
+	begin
+		return next has_table('users', 'test_user', 
+			'There should be a table to hold test users.');
+	end;
+$test$ language plpgsql;
+
+create or replace function test_users_table_testusers_column_name_exists()
+returns setof text as $test$
+	begin
+		return next has_column('users', 'test_user', 'name',
+			'Needs a user name column in test users.');
+	end;
+$test$ language plpgsql;  
+
+create or replace function test_users_table_testuser_column_name_is_text()
+returns setof text as $test$
+	begin
+		return next col_type_is('users', 'test_user', 'name', 'text',
+			'The test user name needs to be text.');
+	end;
+$test$ language plpgsql;  
+
+create or replace function test_users_table_testuser_column_name_is_pk()
+returns setof text as $test$
+	begin 
+		return next col_is_pk('users', 'test_user', 'name',
+			'Name should be primary key for test users.');
+	end;
+$test$ language plpgsql;
+
+create or replace function test_users_table_testgroup_exists()
+returns setof text as $test$
+	begin
+		return next has_table('users', 'test_group', 
+			'There should be a table to hold test groups.');
+	end;
+$test$ language plpgsql;
+
+create or replace function test_users_table_testgroup_column_name_exists()
+returns setof text as $test$
+	begin
+		return next has_column('users', 'test_group', 'name',
+			'Needs a user name column in test users.');
+	end;
+$test$ language plpgsql;  
+
+create or replace function test_users_table_testgroup_column_name_is_text()
+returns setof text as $test$
+	begin
+		return next col_type_is('users', 'test_group', 'name', 'text',
+			'The test group name needs to be text.');
+	end;
+$test$ language plpgsql;  
+
+create or replace function test_users_table_testgroup_column_name_is_pk()
+returns setof text as $test$
+	begin 
+		return next col_is_pk('users', 'test_group', 'name',
+			'Name should be primary key for test group.');
+	end;
+$test$ language plpgsql;
 
 create or replace function test_users_user_exists()
 returns setof text as $$


create or replace function test_users_function_addgroup_inserts_data()
returns setof text as $test$
	begin
		perform users.add_group('web-session-1', 'group1');
		return next results_eq( 
			$$select name from users.group
				where name = 'group1'$$,
			$$values ('group1')$$,
			'Users add group needs to add data.');
	end;
$test$ language plpgsql;

	begin
		return next throws_ok(
			$$insert into users.user (id, name, password, email) values
				(uuid_generate_v1(), 'four', 'password', 
				md5(random()::text))$$,
			'23514', 
			'new row for relation "user" violates check constraint "name_len"',
			'User name must be a minimum of 5 characters.');
