Exemplo n.º 1
0
 public function setUp()
 {
     parent::setUp();
     $this->initModels([new User(), new Group(), new Membership(), new Customer()]);
     $group = new Group();
     $group->name = 'Administrators';
     $group->save();
     $group_prog = new Group();
     $group_prog->name = 'Programmers';
     $group_prog->save();
     $anton = new User();
     $anton->username = '******';
     $anton->password = '******';
     $anton->save();
     $group_prog->users->link($anton);
     $anton_home = new Customer();
     $anton_home->address = "Anton home";
     $anton_home->user = $anton;
     $anton_home->save();
     $anton_work = new Customer();
     $anton_work->address = "Anton work";
     $anton_work->user = $anton;
     $anton_work->save();
     $max = new User();
     $max->username = '******';
     $max->password = '******';
     $max->save();
     $group->users->link($max);
     $max_home = new Customer();
     $max_home->address = "Max home";
     $max_home->user = $max;
     $max_home->save();
 }
Exemplo n.º 2
0
 public function setUp()
 {
     parent::setUp();
     $this->basePath = realpath(__DIR__ . '/../../protected');
     $group = new Group();
     $group->name = 'Administrators';
     $group->save();
     $group_prog = new Group();
     $group_prog->name = 'Programmers';
     $group_prog->save();
     $anton = new User();
     $anton->username = '******';
     $anton->password = '******';
     $anton->save();
     $group_prog->users->link($anton);
     $anton_home = new Customer();
     $anton_home->address = "Anton home";
     $anton_home->user = $anton;
     $anton_home->save();
     $anton_work = new Customer();
     $anton_work->address = "Anton work";
     $anton_work->user = $anton;
     $anton_work->save();
     $max = new User();
     $max->username = '******';
     $max->password = '******';
     $max->save();
     $group->users->link($max);
     $group_prog->users->link($max);
     $max_home = new Customer();
     $max_home->address = "Max home";
     $max_home->user = $max;
     $max_home->save();
 }
Exemplo n.º 3
0
 public function testInit()
 {
     $this->assertEquals(5, Product::objects()->count());
     $this->assertEquals(1, Category::objects()->count());
     $this->assertEquals(1, User::objects()->count());
     $this->assertEquals(1, Customer::objects()->count());
     $this->assertEquals(1, Order::objects()->count());
     $this->assertEquals(5, Order::objects()->get(['pk' => 1])->products->count());
     $this->assertEquals(1, ProductList::objects()->count());
 }
 public function testLookupsClean()
 {
     $filter = ['user__groups__name__endswith' => 's'];
     $count = 3;
     $qs = Customer::objects()->filter($filter);
     // $sql = "SELECT COUNT(DISTINCT `tests_customer_1`.`id`) FROM `tests_customer` `tests_customer_1` LEFT OUTER JOIN `tests_user` `tests_user_2` ON `tests_customer_1`.`user_id` = `tests_user_2`.`id` LEFT OUTER JOIN `tests_membership` `tests_membership_3` ON `tests_user_2`.`id` = `tests_membership_3`.`user_id` LEFT OUTER JOIN `tests_group` `tests_group_4` ON `tests_membership_3`.`group_id` = `tests_group_4`.`id` WHERE (`tests_group_4`.`name` LIKE '%s')";
     // $this->assertEquals($sql, $qs->countSql());
     $this->assertEquals($count, $qs->count());
     $this->assertEquals($count, count($qs->all()));
 }
 public function testForeignLookup()
 {
     $addresses = Customer::objects()->order(['-user__username', '-address'])->all();
     $this->assertEquals(count($addresses), 3);
     $this->assertEquals($addresses[0]->address, 'Max home');
     $this->assertEquals($addresses[1]->address, 'Anton work');
     $this->assertEquals($addresses[2]->address, 'Anton home');
     $addresses = Customer::objects()->order(['user__username', 'address'])->all();
     $this->assertEquals(count($addresses), 3);
     $this->assertEquals($addresses[0]->address, 'Anton home');
     $this->assertEquals($addresses[1]->address, 'Anton work');
     $this->assertEquals($addresses[2]->address, 'Max home');
 }
Exemplo n.º 6
0
 public function setUp()
 {
     parent::setUp();
     $category = new Category();
     $category->name = 'test';
     $category->save();
     $user = new User();
     $user->password = 123456;
     $user->username = '******';
     $user->save();
     $customer = new Customer();
     $customer->user = $user;
     $customer->address = 'example super address';
     $customer->save();
     $products = [];
     foreach ([1, 2, 3, 4, 5] as $i) {
         $product = new Product();
         $product->name = $i;
         $product->price = $i;
         $product->description = $i;
         $product->category = $category;
         $product->save();
         $products[] = $product;
     }
     $order1 = new Order();
     $order1->customer = $customer;
     $order1->save();
     foreach ($products as $p) {
         $order1->products->link($p);
     }
     $order2 = new Order();
     $order2->customer = $customer;
     $order2->discount = 1;
     $order2->save();
     $order2->products = $products;
     $order2->save();
 }
Exemplo n.º 7
0
 public function testIssue63()
 {
     $this->assertEquals(2, User::objects()->count());
     foreach (User::objects()->all() as $i => $user) {
         $customer = Customer::objects()->get(['pk' => $i + 1]);
         $customer->user = $user;
         $customer->save(['user']);
     }
     $this->assertEquals(2, Customer::objects()->count());
     list($first, $last) = Customer::objects()->order(['pk'])->asArray()->all();
     $this->assertEquals(['id' => 1, 'user_id' => 1, 'address' => 'foo'], $first);
     $this->assertEquals(['id' => 2, 'user_id' => 2, 'address' => 'bar'], $last);
     $this->assertEquals(1, Customer::objects()->filter(['user__username' => 'foo'])->count());
     $this->assertEquals(1, Customer::objects()->filter(['user__username' => 'bar'])->count());
 }
Exemplo n.º 8
0
 public static function getFields()
 {
     return ['customer' => ['class' => ForeignField::className(), 'modelClass' => Customer::className()], 'products' => ['class' => ManyToManyField::className(), 'modelClass' => Product::className()], 'discount' => ['class' => IntField::className(), 'null' => true]];
 }
Exemplo n.º 9
0
 public function testValuesList()
 {
     $group = new Group();
     $group->name = 'Administrators';
     $group->save();
     $groupProg = new Group();
     $groupProg->name = 'Programmers';
     $groupProg->save();
     $anton = new User();
     $anton->username = '******';
     $anton->password = '******';
     $anton->save();
     $groupProg->users->link($anton);
     $anton_home = new Customer();
     $anton_home->address = "Anton home";
     $anton_home->user = $anton;
     $anton_home->save();
     $anton_work = new Customer();
     $anton_work->address = "Anton work";
     $anton_work->user = $anton;
     $anton_work->save();
     $max = new User();
     $max->username = '******';
     $max->password = '******';
     $max->save();
     $group->users->link($max);
     $max_home = new Customer();
     $max_home->address = "Max home";
     $max_home->user = $max;
     $max_home->save();
     $values = Customer::objects()->valuesList(['address', 'user__username']);
     $this->assertEquals([['address' => 'Anton home', 'user__username' => 'Anton'], ['address' => "Anton work", 'user__username' => 'Anton'], ['address' => "Max home", 'user__username' => 'Max']], $values);
     $this->assertEquals([['address' => 'Anton home', 'user__username' => 'Anton'], ['address' => "Anton work", 'user__username' => 'Anton'], ['address' => "Max home", 'user__username' => 'Max']], Customer::objects()->valuesList(['address', 'user__username']));
     $this->assertEquals(['Anton', 'Anton', 'Max'], Customer::objects()->valuesList(['user__username'], true));
 }
Exemplo n.º 10
0
 public function testCreate()
 {
     $user = User::objects()->get(['pk' => 1]);
     Customer::objects()->create(['user' => $user, 'address' => 'Broadway']);
     Customer::objects()->create(['user_id' => $user->id, 'address' => 'Broadway']);
     $address1 = Customer::objects()->get(['pk' => 3]);
     $address2 = Customer::objects()->get(['pk' => 4]);
     $this->assertEquals($user->id, $address1->user->id);
     $this->assertEquals($user->id, $address2->user_id);
 }
Exemplo n.º 11
0
 public static function getFields()
 {
     return ['username' => ['class' => CharField::className(), 'validators' => [new MinLengthValidator(3), new MaxLengthValidator(20)]], 'password' => ['class' => PasswordField::className(), 'null' => true], 'groups' => ['class' => ManyToManyField::className(), 'modelClass' => Group::className(), 'through' => Membership::className()], 'addresses' => ['class' => HasManyField::className(), 'modelClass' => Customer::className(), 'relatedName' => 'user', 'editable' => false]];
 }
 public function lookupProvider()
 {
     return [[Customer::className(), ['user__username' => 'Anton'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username"=\'Anton\')', 2], [Customer::className(), ['user__username__startswith' => 'A'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username" LIKE \'A%\')', 2], [Customer::className(), ['user__groups__name' => 'Administrators'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_group_4"."name"=\'Administrators\')', 3], [Customer::className(), ['user__groups__name__endswith' => 's'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_group_4"."name" LIKE \'%s\')', 3], [User::className(), ['addresses__address__contains' => 'Anton'], 'SELECT COUNT(DISTINCT "tests_user_1"."id") FROM "tests_user" "tests_user_1" LEFT OUTER JOIN "tests_customer" "tests_customer_2" ON "tests_user_1"."id" = "tests_customer_2"."user_id" WHERE ("tests_customer_2"."address" LIKE \'%Anton%\')', 1], [Customer::className(), ['user__username' => 'Max', 'user__pk' => '2'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username"=\'Max\') AND ("tests_user_2"."id"=\'2\')', 1], [Customer::className(), ['user__username' => 'Max', 'user__groups__pk' => '1'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_user_2"."username"=\'Max\') AND ("tests_group_4"."id"=\'1\')', 1]];
 }