Merge: Added contributing guidelines and link from readme
[nit.git] / contrib / nitrpg / src / test_helper.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014-2015 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Test tools for NitRPG.
18 module test_helper
19
20 import test_suite
21 import game
22 import github::cache
23
24 # Used to factorize test treatments.
25 abstract class NitrpgTestHelper
26 super TestSuite
27
28 # Github API client
29 var api: GithubAPI do
30 var api = new GithubAPI(get_github_oauth)
31 api.enable_cache = true
32 return api
33 end
34
35 # Mongo API client
36 var mongo = new MongoClient("mongodb://localhost:27017/")
37
38 # Load a new test database by with a name
39 private fun load_db(name: String): MongoDb do return mongo.database(name)
40
41 # Load a repo by its name.
42 fun load_repo(name: String): Repo do
43 var repo = api.load_repo(name)
44 assert repo != null
45 return repo
46 end
47
48 # Load a game by its name.
49 fun load_game(name: String, db: MongoDb): Game do
50 var game = new Game(api, load_repo(name))
51 game.db_name = db.name
52 return game
53 end
54
55 # Stack of db used for testing.
56 var test_dbs = new Array[MongoDb]
57
58 # Gen a test db with a random name (to avoid race conditions).
59 fun gen_test_db: MongoDb do
60 var testid = "NIT_TESTING_ID".environ.to_i
61 var db_name = "test_nitrpg_{testid}"
62 var db = load_db(db_name)
63 test_dbs.add db
64 return db
65 end
66
67 # Should be called after your test.
68 fun drop_test_db do
69 var db = test_dbs.pop
70 db.drop
71 end
72
73 redef fun after_test do drop_test_db
74 end