From 4117da5380618f3699d7d89cdefa2a97bc04befa Mon Sep 17 00:00:00 2001 From: James John Date: Wed, 1 Sep 2021 23:41:53 +0000 Subject: [PATCH] Modify insertTests --- tests/JSONDBTest.php | 36 +++++++++++------------------- tests/users.json | 53 +------------------------------------------- tests/users.sql | 23 ------------------- 3 files changed, 14 insertions(+), 98 deletions(-) delete mode 100644 tests/users.sql diff --git a/tests/JSONDBTest.php b/tests/JSONDBTest.php index f3cfc91..e25425b 100644 --- a/tests/JSONDBTest.php +++ b/tests/JSONDBTest.php @@ -7,12 +7,15 @@ class InsertTest extends TestCase { private $db; - public function load_db() { - $this->db = new JSONDB( __DIR__ ); + protected function setUp(): void { + $this->db = new JSONDB(__DIR__); + } + + public function tearDown() { + @unlink( __DIR__ . '/users.sql' ); } public function testInsert() : void { - $this->load_db(); $names = [ 'James£', 'John£', 'Oji£', 'Okeke', 'Bola', 'Thomas', 'Ibrahim', 'Smile' ]; $states = [ 'Abia', 'Lagos', 'Benue', 'Kano', 'Kastina', 'Abuja', 'Imo', 'Ogun' ]; shuffle( $names ); @@ -29,17 +32,21 @@ public function testInsert() : void { 'age' => $age ]); - $this->assertArrayHasKey( 0, $indexes ); + $user = $this->db->select( '*' ) + ->from( 'users' ) + ->where( [ 'name' => $name, 'state' => $state, 'age' => $age ], 'AND' ) + ->get(); $this->db->insert( "users", array( "name" => "Dummy", "state" => "Lagos", "age" => 12 )); + + $this->assertEquals( $name, $user[0]['name'] ); } public function testGet() : void { - $this->load_db(); printf( "\nCheck exist\n" ); $users = ( $this->db->select( '*' ) ->from( 'users' ) @@ -48,7 +55,6 @@ public function testGet() : void { } public function testWhere() : void { - $this->load_db(); $result = ( $this->db->select( '*' ) ->from( 'users' ) ->where([ 'name' => 'Okeke' ]) @@ -74,8 +80,6 @@ public function testWhere() : void { } public function testMultiWhere() : void { - $this->load_db(); - $this->db->insert( "users", array( "name" => "Jajo", "age" => null, @@ -93,8 +97,6 @@ public function testMultiWhere() : void { } public function testAND() : void { - $this->load_db(); - $this->db->insert( "users", array( "name" => "Jajo", "age" => 50, @@ -114,8 +116,6 @@ public function testAND() : void { } public function testRegexAND() : void { - $this->load_db(); - $this->db->insert( "users", array( "name" => "Paulo", "age" => 50, @@ -149,8 +149,6 @@ public function testRegexAND() : void { } public function testRegex() : void { - $this->load_db(); - $this->db->insert( "users", array( "name" => "Jajo", "age" => 89, @@ -172,8 +170,6 @@ public function testRegex() : void { } public function testUpdate() : void { - $this->load_db(); - $this->db->update([ 'name' => 'Jammy', 'state' => 'Sokoto' ]) ->from( 'users' ) ->where([ 'name' => 'Okeke' ]) @@ -194,16 +190,12 @@ public function testUpdate() : void { } public function testSQLExport() : void { - $this->load_db(); - $this->db->to_mysql( "users", "tests/users.sql" ); $this->assertTrue(file_exists( "tests/users.sql" ) ); } public function testDelete() : void { - $this->load_db(); - $this->db->delete() ->from( 'users' ) ->where([ 'name' => 'Jammy' ]) @@ -217,9 +209,7 @@ public function testDelete() : void { $this->assertEmpty( $result ); } - public function testDeleteAll() : void { - $this->load_db(); - + public function testDeleteAll() : void { /* I add a select action with where statement */ $result_before = $this->db->select( '*' ) ->from( 'users' ) diff --git a/tests/users.json b/tests/users.json index 61af1b1..0637a08 100644 --- a/tests/users.json +++ b/tests/users.json @@ -1,52 +1 @@ -[ - { - "name": "John£", - "state": "Benue", - "age": 22 - }, - { - "name": "Jajo", - "age": null, - "state": "Lagos" - }, - { - "name": "Johnny", - "age": 30, - "state": "Ogun" - }, - { - "name": "Jajo", - "age": 50, - "state": "Lagos" - }, - { - "name": "Johnny", - "age": 50, - "state": "Ogun" - }, - { - "name": "Paulo", - "age": 50, - "state": "Algeria" - }, - { - "name": "Nina", - "age": 50, - "state": "Nigeria" - }, - { - "name": "Ogwo", - "age": 49, - "state": "Nigeria" - }, - { - "name": "Jajo", - "age": 89, - "state": "Abia" - }, - { - "name": "Mitchell", - "age": 45, - "state": "Zamfara" - } -] \ No newline at end of file +[] \ No newline at end of file diff --git a/tests/users.sql b/tests/users.sql deleted file mode 100644 index 92052eb..0000000 --- a/tests/users.sql +++ /dev/null @@ -1,23 +0,0 @@ --- PHP-JSONDB JSON to MySQL Dump --- - --- Table Structure for `users` --- - -CREATE TABLE `users` -( - `name` VARCHAR( 255 ), - `state` VARCHAR( 255 ), - `age` INT -); -INSERT INTO `users` ( `name`, `state`, `age` ) VALUES ( 'John£', 'Benue', '22' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Jammy', '21', 'Sokoto' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Jajo', '', 'Lagos' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Johnny', '30', 'Ogun' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Jajo', '50', 'Lagos' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Johnny', '50', 'Ogun' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Paulo', '50', 'Algeria' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Nina', '50', 'Nigeria' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Ogwo', '49', 'Nigeria' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Jajo', '89', 'Abia' ); -INSERT INTO `users` ( `name`, `age`, `state` ) VALUES ( 'Mitchell', '45', 'Zamfara' );