You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## If not mocking the database, how do you ensure you don't fill your database with test insertions?
2
4
3
-
To test a real database, you need a way to create a test table at the start of each test, then test your database calls, and finally drop your table after each test.
5
+
To test a real database, you either need a way to create a test table at the start of each test, then test your database calls, and finally drop your table after each test... or have an existing test table in place, and simply ensure that after adding test insertions you remove them after testing.
4
6
5
-
If you want to work with similar table data in your tests, you can use SQLs VIEW method:
7
+
If you want to work with a similar table data in your tests, you can set up a View with SQLs VIEW method:
6
8
```sql
7
9
CREATEVIEWmy_viewAS
8
10
SELECT column_name(s)
9
11
FROM original_table
10
12
WHERE condition
11
13
```
12
-
Views act like temporary tables in memory. You can perform the same actions as you would a real table. After working with your view table, you can drop it...
14
+
Views act like temporary tables in memory. You can perform the same actions as you would a real table. You can even combine multiple tables into one view. After working with your view table, you can drop it...
13
15
```sql
14
16
DROPVIEW my_view
15
17
```
16
18
17
-
Otherwise, you can simply create a table in the same way, and drop it after testing.
19
+
Otherwise, you can simply create a new table as you are testing, then drop it afterwards.
18
20
19
-
To integrate this approach in a real testing framework, you would perform the following tasks-
21
+
To integrate the create/drop method in a real testing framework, you would need to perform the following tasks-
20
22
1. (Before Test) Create test database from desired testing table.
21
23
2. Perform actions (e.g add/delete/modify data).
22
24
3. Use SELECT queries on this test table, and asserts to test that data is manipulated.
23
25
4. (After Test) Drop test table.
24
26
25
27
Many testing frameworks allow beforeEach and AfterEach as a testing method, so steps **1** and **4** would be performed before and after each test.
26
28
27
-
Check out [redtape](https://github.com/eugeneware/redtape) for examples on how to use a tape like testing environment with before each and after each methods.
29
+
Check out [redtape](https://github.com/eugeneware/redtape) for examples on how to use a tape like testing environment with before each and after each methods. When testing, an example test could appear like so;
30
+
```javascript
31
+
test('checks results', function() {
32
+
var client =newClient();
33
+
client.connect();
34
+
client.query('SELECT * FROM person WHERE name = $1', ['Brian'], assert.calls(function(err, result) {
35
+
t.notOk(err);
36
+
t.equal(result.rows[0].name, 'Brian');
37
+
client.end();
38
+
t.end();
39
+
}))
40
+
}));
41
+
})
42
+
```
28
43
29
-
## Research and figure out how to use a mocking library.
30
44
## What are some advantages and disadvantages of mocking?
31
45
### Advantages of mocking
32
46
- using mock objects and data can be a good way to unit-test, but integration testing is needed to ensure that the system runs as a whole
@@ -39,11 +53,10 @@ Check out [redtape](https://github.com/eugeneware/redtape) for examples on how t
39
53
- there are security and privacy implications to mock databases
40
54
- once the db gets big, taking a copy of it and putting it into your dev environment can be a big deal
41
55
42
-
## What exactly needs testing about the database?
43
-
- Test if any errors are shown while executing queries
44
-
- Data Integrity is maintained while creating, updating or deleting data in database
45
-
- Check response time of queries and fine tune them if necessary
46
-
- Test data retrieved from your database is shown accurately in your web application
47
-
48
-
Find out more: http://www.guru99.com/web-application-testing.html
49
-
56
+
## Resources
57
+
-[An overview of database testing](http://www.softwaretestinghelp.com/database-testing-process/)
58
+
-[Strategies for unit-testing database-driven applications](http://stackoverflow.com/questions/145131/whats-the-best-strategy-for-unit-testing-database-driven-applications)
59
+
-[Testing: when to connect the database](http://softwareengineering.stackexchange.com/questions/206539/unit-tests-and-databases-at-which-point-do-i-actually-connect-to-the-database)
60
+
-[Wikipedia entry on mock objects](https://en.wikipedia.org/wiki/Mock_object)
61
+
-[Martin Fowler on classical testing vs mocking](http://martinfowler.com/articles/mocksArentStubs.html)
A database schema defines how a database is structured. It defines the tables that exist and their names, how those tables are configured, the fields that those tables contain, and the data format for those fields. A schema describes how real-world (or conceptual) entities are modeled by the database.
38
+
39
+
The schema doesn’t contain any data in itself. It is simply a description of how data added to a database must be structured.
40
+
41
+
If you start working on a project with an existing database, the first thing you should do is ask for a schema diagram. The diagram will show all of the tables in the database and the foreign keys they use to relate. It should look like this:
- You have a live site with live users, and ideally you want to avoid downtime
12
-
- User data is very sensitive and not to be messed with
47
+
Migration is the processing of transferring data from one schema to another - often within the same database. (It may also refer to the process of moving data between two different databases with the same schema, although this tends to be a simpler task - just export from one then import into the other.)
13
48
49
+
### When might you need to migrate a database?
14
50
15
-
### Example: cat lovers database 🐱
51
+
As a company expands or develops, the real-world or conceptual entities it deals with may change or become more complicated. When this happens, you might need to update your database schema to reflect the changes in the real-world entities that you're dealing with – but all of the data created under the old schema will need to be ‘migrated’ into the new format in order to preserve your previous records. See _Example: cat lovers database_ below.
52
+
53
+
### The steps of migrating a database
54
+
55
+
There are many approaches to database migration. One of the most commonly used patterns is __ETL (export, transform, load)__, although others exist.
56
+
57
+
To migrate data using the ETL pattern, you would first __identify which tables are affected by the migration__. Databases can be huge, so you only want to be working with the data that is specifically affected by the change of schema.
58
+
59
+
Making changes directly into a live database is a _very bad idea_ – a single typo or poorly written query can destroy millions of records. So when you’ve selected the affected data, you should __export__ it to a safe area, separate from your live data.
60
+
61
+
When you’ve exported your data, you then need to __transform__ the data from the old schema into the new schema. This should be done using scripts – firstly because this reduces the possibility of human error, and secondly because the transformation can then be applied to your databases in other environments (for example, your production, test and development environments). See _Example: cat lovers database_ for an example script.
62
+
63
+
When the data has been transformed and validated, you can then __load__ the transformed data back into the database.
64
+
65
+
### Things to consider
66
+
67
+
- If you have a live site with live users, ideally you want to avoid downtime.
68
+
- User data is very sensitive and not to be messed with.
69
+
70
+
### Example: cat lovers database 🐱
16
71
17
72
In our example, we have a really basic database with just one table. It stores a list of cat lovers and the names of their cats. This works OK for the organisation updating the cat lovers database for a few years, until some of the members start adopting new cats. The single-table database structure just isn't sufficient any more.
18
73
@@ -36,29 +91,25 @@ We'll write some SQL for that:
36
91
37
92
```
38
93
INSERT INTO cats (name, color, catLoverId) VALUES
39
-
SELECT cat_name, cat_color, id FROM catLovers
94
+
SELECT cat_name, cat_color, id FROM catLovers;
40
95
```
41
-
- Now all our data is in one place but we've got some extra columns left in our cat_lovers table which we don't really need any more. In practice, these columns aren't causing us any problems, so we'll leave them in place for an interim period so that we can be 500% sure that none of our data has been lost in migration.
42
-
- Later on, we might want to delete the old data for good. After all, some of those cats might have changed colour by now. We can do this with SQL:
96
+
- Now all our data is in one place but we've got some extra columns left in our cat_lovers table which we don't really need any more. In practice, these columns aren't causing us any problems, so we'll leave them in place for an interim period so that we can be 500% sure that none of our data has got lost in migration.
97
+
- Later on, we might want to delete the old data for good. After all, some of those cats might have changed colour by now and we wouldn't want to be storing inaccurate data. We can do this with SQL:
43
98
44
99
```
45
100
ALTER TABLE cat_lovers
46
-
DROP COLUMN (cat_name, cat_color)
101
+
DROP COLUMN (cat_name, cat_color);
47
102
```
48
103
49
104
- Our lovely two-columned database now looks more like this:
50
105
51
106

52
-
53
107
😻
54
108
55
109
## Resources
56
110
111
+
The [Wikipedia entry on ETL](https://en.wikipedia.org/wiki/Extract,_transform,_load) explains a little more about the pattern, its history and its uses.
112
+
57
113
This [post on StackOverflow](http://dba.stackexchange.com/questions/10913/best-practices-for-schema-changes-and-data-migrations-to-a-live-database-without) informed the cats example and talks about how to minimize impact of database migrations
58
114
59
115
Here's an [article](https://kostasbariotis.com/data-migration-with-nodejs/) that talks about migration in a node.js context
0 commit comments