EntityModel

 view release on metacpan or  search on metacpan

t/async.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
BEGIN {
	plan skip_all => 'no IO::Async' unless eval { require IO::Async::Loop };
	plan skip_all => 'i broke it';
}

use Test::Deep;
use EntityModel;
use EntityModel::Async;
use Async::MergePoint;

# Set up an EntityModel from a Perl hash
my $loop;
BEGIN {
	my $start = Time::HiRes::time;
	$loop = IO::Async::Loop->new;
	my $model = new_ok('EntityModel');
	ok($model->load_from(
	Perl	=> {
 "name" => "mymodel",
 "entity" => [ {
  "name" => "thing",
  "primary" => "idthing",
  "field" => [
   { "name" => "idthing", "type" => "int" },
   { "name" => "name", "type" => "varchar" }
  ] }, {
  "name" => "other",
  "primary" => "idother",
  "field" => [
   { "name" => "idother", "type" => "int" },
   { "name" => "idthing", "type" => "int", "refer" => [
     { table => 'thing', field => 'idthing', delete => 'cascade', update => 'cascade' }
   ] },
   { "name" => "extra", "type" => "varchar" }
  ] }, {
  "name" => "book",
  "primary" => "idbook",
  "field" => [
   { "name" => "idbook", "type" => "int" },
   { "name" => "idauthor", "type" => "int", "refer" => [
     { table => 'author', field => 'idauthor', delete => 'cascade', update => 'cascade' }
   ] },
   { "name" => "title", "type" => "varchar" }
  ] }, {
  "name" => "author",
  "primary" => "idauthor",
  "field" => [
   { "name" => "idauthor", "type" => "int" },
   { "name" => "idaddress", "type" => "int", "refer" => [
     { table => 'address', field => 'idaddress', delete => 'cascade', update => 'cascade' }
   ] },
   { "name" => "name", "type" => "varchar" }
  ] }, {
  "name" => "address",
  "primary" => "idaddress",
  "field" => [
   { "name" => "idaddress", "type" => "int" },
   { "name" => "street", "type" => "varchar" },
   { "name" => "city", "type" => "varchar" },
   { "name" => "country", "type" => "varchar" },
  ] }
 ] }), 'load model');
	ok($model->add_storage(PerlAsync => {
		loop	=> $loop
	}), 'add Perl backend storage');
#	ok($model->add_storage(Perl => { }), 'add Perl backend storage');
	ok($model->add_support(Perl => { }), 'add Perl class structure');
	note sprintf("Took %2.3fms to set up model", (Time::HiRes::time - $start) * 1000.0);
}

sub first_test {
# Try creating something with a task-on-commit
my $other;
ok(my $thing = Entity::Thing->create({
	name	=> 'EntityEditor'
})->then(sub {
	my $thing = shift;
	ok($other = Entity::Other->create({
		thing	=> $thing,
		extra	=> 'entitymodel.com'
	}), 'create new Other object');
}), 'create with commit callback using ->then');

EntityModel::Gather->new(
	thing	=> $thing,
	other	=> $other,
)->when_ready(sub {
	my %data = @_;

	# Verify that we created okay
	isa_ok($data{thing}, 'Entity::Thing');
	isa_ok($data{other}, 'Entity::Other');
	ok($data{thing}->id, 'have ID for thing');
	ok($data{other}->id, 'have ID for other');
});

Entity::Thing->new($thing->id)->then(sub {
	my $t = shift;
	is($t->id, $thing->id, 'Thing id matches after instantiation');
});
Entity::Other->new($other->id)->then(sub {
	my $o = shift;
	is($o->id, $other->id, 'Other id matches after instantiation');
	is($o->thing->id, $thing->id, 'ref matches');
});

# Now try some updates
$thing->name('renamed')->then(sub {
	my $thing = shift;
	is($thing->name, 'renamed', 'have renamed successfully');



( run in 0.551 second using v1.01-cache-2.11-cpan-e1769b4cff6 )