Merge: concurrent_collections: Add implementation of has method
[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 game
21 import github::cache
22
23 # Used to factorize test treatments.
24 abstract class NitrpgTestHelper
25
26 # Github API client
27 var api: GithubAPI do
28 var api = new GithubAPI(get_github_oauth)
29 api.enable_cache = true
30 return api
31 end
32
33 # Mongo API client
34 var mongo = new MongoClient("mongodb://mongo:27017/")
35
36 # Load a new test database by with a name
37 private fun load_db(name: String): MongoDb do return mongo.database(name)
38
39 # Load a repo by its name.
40 fun load_repo(name: String): Repo do
41 var repo = api.load_repo(name)
42 assert repo != null
43 return repo
44 end
45
46 # Load a game by its name.
47 fun load_game(name: String, db: MongoDb): Game do
48 var game = new Game(api, load_repo(name))
49 game.db_name = db.name
50 return game
51 end
52
53 # Stack of db used for testing.
54 var test_dbs = new Array[MongoDb]
55
56 # Gen a test db with a random name (to avoid race conditions).
57 fun gen_test_db: MongoDb do
58 var testid = "NIT_TESTING_ID".environ.to_i
59 var db_name = "test_nitrpg_{testid}"
60 var db = load_db(db_name)
61 test_dbs.add db
62 return db
63 end
64
65 # Should be called after your test.
66 fun drop_test_db do
67 var db = test_dbs.pop
68 db.drop
69 end
70
71 # Drop the databse after each test
72 fun after_test is after do drop_test_db
73 end