Skip to content

Commit 4fd88b4

Browse files
update unit tests
1 parent ff9f900 commit 4fd88b4

24 files changed

+2791
-89
lines changed

.gitignore

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
1-
node_modules
2-
dist
3-
npm-debug.log
1+
## See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
## compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
8+
## dependencies
9+
/node_modules
10+
11+
## IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
419

20+
## IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
## misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
testem.log
34+
/typings
535
#properties
636

7-
.idea
37+
## e2e
38+
/e2e/*.js
39+
/e2e/*.map
40+
41+
## System Files
42+
.DS_Store
43+
Thumbs.db

README.md

+283
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ $ cd <project-name>
66
$ yarn install
77
```
88

9+
Install `mocha`:
10+
```bash
11+
$ sudo yarn global add mocha
12+
```
13+
914
# Run
1015
Make sure you have typescript installed, then run:
1116

@@ -20,3 +25,281 @@ yarn test
2025

2126
# Database
2227
When you first run this project, it will connect to a remote MongoDB instance I have setup so this project can be run with minimal overhead. However, I advise you to create your own `<project>/properties/local.properties.json` and `<project>/properties/test.properties.json`, since the remote mongo instance is cleaned on a regular basis.
28+
29+
30+
----------------------------
31+
32+
# Create MongoDB database and account user
33+
34+
## MongoDB Connection
35+
36+
Check db server:
37+
```bash
38+
$ service mongod status
39+
```
40+
41+
Run db server:
42+
```bash
43+
$ sudo service mongod start
44+
45+
$ service mongod status
46+
...
47+
Active: active (running)
48+
```
49+
50+
[Getting Started with the mongo Shell](https://docs.mongodb.com/v3.0/tutorial/getting-started-with-the-mongo-shell/)
51+
52+
Start the mongo Shell:
53+
```bash
54+
$ mongo
55+
56+
// To display the database you are using:
57+
$ MongoDB> db
58+
test
59+
60+
// Print help
61+
$ MongoDB> help
62+
63+
// Exit mongo shell
64+
$ MongoDB> exit
65+
$
66+
```
67+
> ```bash
68+
> db.help() help on db methods
69+
> db.mycoll.help() help on collection methods
70+
> sh.help() sharding helpers
71+
> rs.help() replica set helpers
72+
> help admin administrative help
73+
> help connect connecting to a db help
74+
> help keys key shortcuts
75+
> help misc misc things to know
76+
> help mr mapreduce
77+
>
78+
> show dbs show database names
79+
> show collections show collections in current database
80+
> show users show users in current database
81+
> show profile show most recent system.profile entries with time >= 1ms
82+
> show logs show the accessible logger names
83+
> show log [name] prints out the last segment of log in memory, 'global' is > default
84+
> use <db_name> set current database
85+
> db.foo.find() list objects in collection foo
86+
> db.foo.find( { a : 1 } ) list objects in foo where a == 1
87+
> it result of the last line evaluated; use to further iterate
88+
> DBQuery.shellBatchSize = x set default number of items to display on shell
89+
> exit quit the mongo shell
90+
> MongoDB Enterprise >
91+
>
92+
> ```
93+
>> To switch databases, issue the `use <db>`
94+
95+
96+
## How to create a MongoDB database
97+
<http://theholmesoffice.com/how-to-create-a-mongodb-database/>
98+
> With MongoDB you create a database the first time you save data into it.
99+
100+
```bash
101+
$ mongo
102+
103+
mongo> show dbs
104+
105+
admin 0.000GB
106+
local 0.000GB
107+
>
108+
>
109+
```
110+
111+
Step 1. "`Use`" the database you wish to create:
112+
> rather than explicitly creating a database, MongoDB will `create a database when it first has data saved to`.
113+
>> In order to save data, we need to connect to our new database `even though it doesn’t exist yet`.
114+
115+
```bash
116+
// new database: `ts-mean-test`
117+
mongo> use ts-mean-test
118+
>
119+
```
120+
> the database hasn’t been created yet.
121+
122+
Step 2. Insert some data into the `ts-mean-test` database:
123+
> there are no `tables`, `rows` or `columns`.
124+
> In their place, MongoDB uses ***`collections`*** and ***`objects`***. Think of `collections` as tables, and `objects` as table rows.
125+
126+
```bash
127+
mongo> db
128+
ts-mean-test
129+
130+
// new collection: `teams`
131+
> db.teams.save({Country:"Korea",GroupName:"Codebits.Design"})
132+
>
133+
> show dbs
134+
135+
admin 0.000GB
136+
local 0.000GB
137+
ts-mean-test 0.000GB
138+
>
139+
>
140+
```
141+
> That will create a new collection called “`teams`”, and save an object with two fields “`Country`” and “`GroupName`”. Crucially, this will now also save our database.
142+
> Check that it has been created by running `show dbs` again.
143+
144+
Step 3. testing by reading the data back out:
145+
```bash
146+
mongo> show collections
147+
148+
teams
149+
>
150+
> db.teams.find()
151+
152+
{ "_id" : ObjectId("5902ef7eeeffb94cf18735d4"), "Country" : "Korea", "GroupName" : "Codebits.Design" }
153+
>
154+
>
155+
```
156+
157+
> - A **database** holds a set of collections
158+
> - A **collection** holds a set of documents
159+
> - A **document** is a set of fields
160+
> - A **field** is a key-value pair
161+
> - A **key** is a name (string)
162+
> - A **value** is a - basic type like string, integer, float, timestamp, binary, etc., a document, or an array of values
163+
164+
165+
## How to create a MongoDB accout user
166+
[User Management on MongoDB](https://docs.mongodb.com/v3.0/reference/method/#user-management)
167+
168+
- `show users`: Print a list of users for current database.
169+
- `db.createUser(user, writeConcern)` : Creates a new user.
170+
171+
Create User with Roles :
172+
<https://docs.mongodb.com/v3.0/reference/method/db.createUser/#db.createUser>
173+
174+
The following operation creates ***`mongoUser`*** in the ***`ts-mean-test`*** database and gives the user the `readWrite` and `dbAdmin` roles.
175+
```bash
176+
mongo> use ts-mean-test
177+
>
178+
> db.createUser(
179+
{
180+
user: "mongoUser",
181+
pwd: "password",
182+
roles: [ "readWrite", "dbAdmin" ]
183+
}
184+
)
185+
186+
>
187+
>
188+
```
189+
190+
Show the account users of `ts-mean-test` database:
191+
```bash
192+
mongo> use ts-mean-test
193+
>
194+
> show users
195+
196+
{
197+
        "_id" : "ts-mean-test.mongoUser",
198+
        "user" : "mongoUser",
199+
        "db" : "ts-mean-test",
200+
        "roles" : [
201+
                {
202+
                        "role" : "readWrite",
203+
                        "db" : "ts-mean-test"
204+
                },
205+
                {
206+
                        "role" : "dbAdmin",
207+
                        "db" : "ts-mean-test"
208+
                }
209+
        ]
210+
}
211+
212+
>
213+
> exit
214+
$
215+
```
216+
217+
Connect `ts-mean-test` database with `mongoUser` account:
218+
```bash
219+
$ mongo ts-mean-test -u mongoUser -p
220+
Enter password:
221+
222+
mongo> db
223+
224+
ts-mean-test
225+
>
226+
>
227+
```
228+
229+
230+
----------------------------
231+
232+
233+
# DefinitelyTyped for TypeScript
234+
235+
## TypeScript 2.x
236+
237+
If you are using TypeScript 2.x you no longer need to have Typings or Definitely Typed installed. Simply install the following package.
238+
```bash
239+
$ yarn add @types/node --dev
240+
or
241+
$ npm install @types/node --save-dev
242+
```
243+
244+
> OPTIONAL: If you are specifying typeroots or types in your tsconfig.json. You may need to update the tsconfig.json to include node as a type. By default any package under @types is already included in your build unless you've specified either of these options.
245+
246+
Below is proper configuration for each of these options:
247+
`tsconfig.js`
248+
```json
249+
{
250+
"compilerOptions": {
251+
// types option has been previously configured
252+
"types": [
253+
// add node as an option
254+
"node"
255+
],
256+
// typeRoots option has been previously configured
257+
"typeRoots": [
258+
// add path to @types
259+
"node_modules/@types"
260+
]
261+
}
262+
}
263+
```
264+
265+
## TypeScript 1.x
266+
267+
The definitive step-by-step guide to use DefinitelyTyped typings for applications written in TypeScript. - Posted on 2016/06/20
268+
<https://neoheurist.wordpress.com/2016/06/20/definitely-typed/>
269+
270+
Install `typings`:
271+
```bash
272+
$ yarn add typings
273+
```
274+
275+
Run `typings`:
276+
```bash
277+
$ yarn run typings
278+
279+
```
280+
281+
Create an empty `typings.json` file using the typings init command:
282+
```bash
283+
$ yarn run typings init
284+
```
285+
286+
Find a package typings:
287+
```bash
288+
$ yarn run typings search express
289+
290+
Viewing 20 of 53
291+
NAME SOURCE HOMEPAGE DESCRIPTION VERSIONS UPDATED
292+
express npm https://www.npmjs.com/package/express 1 2017-02-17T20:54:33.000Z
293+
express dt http://expressjs.com 2 2017-01-18T06:03:22.000Z
294+
...
295+
```
296+
297+
To determine which `typings` file to use, run the `typings` `info` command:
298+
```bash
299+
$ yarn run typings info dt~express --versions
300+
```
301+
302+
Install package typings:
303+
```bash
304+
$ yarn run typings install dt~express --save --global
305+
```

package.json

+25-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
"name": "typescript-mean-backend",
33
"version": "0.0.0",
44
"description": "Typescript MEAN Backend, MongoDB-Express-Node App for `typescript-mean-seed` git-submodule.",
5-
"main": "index.js",
5+
"main": "dist/index.js",
6+
"files": [
7+
"dist/**/*",
8+
"properties/*.json"
9+
],
610
"scripts": {
7-
"test": "mocha --reporter spec --compilers ts:ts-node/register '**/*.test.ts'",
11+
"lint": "tslint \"src/**/*.ts\"",
12+
"clean": "rimraf dist",
13+
"html": "cd ./src && find . \\( -name '*.html' -or -name '*.css' -or -name '*.json' \\) -type f -exec cp --parents {} ../dist \\;",
14+
"tsc": "tsc",
15+
"build": "yarn run clean && yarn run tsc",
16+
"test-spec": "mocha dist/**/*.test.js -R spec --bail",
17+
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.test.js\" -R spec --bail",
18+
"test": "yarn run build && yarn run lint && yarn run html && yarn run test-cov",
819
"start": "tsc && node dist/index.js",
9-
"spec": "mocha --reporter spec --compilers ts:ts-node/register --grep ${TEST} '**/*.test.ts'"
20+
"spec": "mocha --reporter spec --compilers ts:ts-node/register --recursive --grep \"${TEST}\" '**/*.test.ts'",
21+
"nyan": "mocha --reporter nyan --compilers ts:ts-node/register --recursive --grep \"${TEST}\" '**/*.test.ts'",
22+
"tap": "mocha --reporter tap --compilers ts:ts-node/register --recursive --grep \"${TEST}\" '**/*.test.ts'"
1023
},
1124
"author": "Seokjin Seo <[email protected]>",
1225
"license": "MIT",
@@ -17,7 +30,7 @@
1730
"body-parser": "^1.17.1",
1831
"express": "^4.15.2",
1932
"mongodb": "^2.2.26",
20-
"typescript-mean-models": "^0.0.1",
33+
"typescript-mean-models": "^0.0.8",
2134
"winston": "^2.3.1",
2235
"winston-color": "^1.0.0"
2336
},
@@ -28,11 +41,16 @@
2841
"@types/express": "^4.0.33",
2942
"@types/mocha": "^2.2.32",
3043
"@types/mongodb": "^2.1.41",
31-
"@types/node": "^6.0.46",
44+
"@types/node": "^7.0.14",
3245
"chai": "^3.5.0",
3346
"chai-http": "^3.0.0",
34-
"mocha": "^3.1.2",
47+
"istanbul": "^0.4.5",
48+
"mocha": "^3.3.0",
49+
"rimraf": "^2.5.4",
50+
"superagent": "^3.5.2",
3551
"ts-node": "^3.0.2",
36-
"typescript": "^2.3.0"
52+
"tslint": "^5.1.0",
53+
"typescript": "^2.3.0",
54+
"typings": "^2.1.1"
3755
}
3856
}

0 commit comments

Comments
 (0)