Skip to content

Commit 0ea8a0a

Browse files
committed
test: Added tests for config bindings
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent dd68138 commit 0ea8a0a

File tree

9 files changed

+101
-20
lines changed

9 files changed

+101
-20
lines changed

bindings/python/py_model.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void bindPyModel(py::module &m) {
2929
.def(py::init<>())
3030
.def(py::init<const std::string &>())
3131

32+
.def_readonly_static("required_sections", &casbin::Model::required_sections)
33+
3234
.def("HasSection", &casbin::Model::HasSection)
3335
.def("AddDef", &casbin::Model::AddDef, "AddDef adds an assertion to the model.")
3436
.def("LoadModel", &casbin::Model::LoadModel, "LoadModel loads the model from model CONF file.")

casbin/config/config.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ std::shared_ptr<Config> Config::NewConfigFromText(const std::string& text) {
114114
return c;
115115
}
116116

117+
Config::Config() {
118+
}
119+
117120
Config::Config(const std::string& conf_name) {
118121
this->Parse(conf_name);
119122
}

casbin/model/model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ std::unordered_map<std::string, std::string> Model::section_name_map = {
3939

4040
std::vector<std::string> Model::required_sections{"r","p","e","m"};
4141

42-
void Model::LoadModelFromConfig(std::shared_ptr<ConfigInterface> cfg) {
42+
void Model::LoadModelFromConfig(std::shared_ptr<Config>& cfg) {
4343
for (auto [section_name, _] : section_name_map)
4444
LoadSection(this ,cfg, section_name);
4545

casbin/model/model.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "assertion.h"
2525
#include "function.h"
2626
#include "scope_config.h"
27+
#include "../config/config.h"
2728

2829
namespace casbin {
2930

@@ -68,7 +69,7 @@ class Model {
6869
// LoadModelFromText loads the model from the text.
6970
void LoadModelFromText(const std::string& text);
7071

71-
void LoadModelFromConfig(std::shared_ptr<ConfigInterface> cfg);
72+
void LoadModelFromConfig(std::shared_ptr<Config>& cfg);
7273

7374
// PrintModel prints the model to the log.
7475
void PrintModel();

tests/model_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TEST(TestModel, TestNewModelFromString) {
5050

5151
TEST(TestModel, TestLoadModelFromConfig) {
5252
InitTest();
53-
std::shared_ptr<casbin::Model> model = casbin::Model::NewModel();
53+
std::shared_ptr<casbin::Model> model = casbin::Model::NewModel();
5454
model->LoadModelFromConfig(basic_config);
5555

5656
model = casbin::Model::NewModel();

tests/python/config_path.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@
2828
priority_model_path = relative_path + 'examples/priority_model.conf'
2929
priority_policy_path = relative_path + 'examples/priority_policy.csv'
3030
basic_model_without_spaces_path = relative_path + 'examples/basic_model_without_spaces.conf'
31+
testini_path = relative_path + 'casbin/config/testdata/testini.ini'

tests/python/pycasbin_test_suite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pycasbin
1919
import test_enforcer
2020
import test_model
21+
import test_config
2122

2223
def suite():
2324

@@ -27,6 +28,7 @@ def suite():
2728

2829
suite.addTest(loader.loadTestsFromModule(test_enforcer))
2930
suite.addTest(loader.loadTestsFromModule(test_model))
31+
suite.addTest(loader.loadTestsFromModule(test_config))
3032

3133
return suite
3234

tests/python/test_config.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pycasbin as casbin
16+
from config_path import *
17+
import unittest
18+
19+
class TestConfig(unittest.TestCase):
20+
21+
def setUp(self):
22+
self.config = None
23+
24+
def tearDown(self):
25+
self.config = None
26+
27+
def test_Debug(self):
28+
self.config = casbin.Config.NewConfig(testini_path)
29+
self.assertTrue(self.config.GetBool('debug'))
30+
31+
def test_URL(self):
32+
self.config = casbin.Config.NewConfig(testini_path)
33+
self.assertEqual(self.config.GetString('url'), 'act.wiki')
34+
35+
def test_Redis(self):
36+
self.config = casbin.Config.NewConfig(testini_path)
37+
values = self.config.GetStrings('redis::redis.key')
38+
self.assertEqual('push1', values[0])
39+
self.assertEqual('push2', values[1])

tests/python/test_model.py

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,30 @@
1818

1919
class TestModel(unittest.TestCase):
2020

21-
# def setUp(self):
22-
# self.basic_config = casbin.Config.NewConfig(basic_model_path)
21+
def setUp(self):
22+
self.model = None
23+
self.config = None
24+
25+
def cleanUp(self):
26+
self.model = None
27+
self.config = None
28+
29+
def tearDown(self):
30+
self.model = None
31+
self.config = None
2332

2433
def test_NewModel(self):
25-
model = casbin.Model.NewModel()
26-
self.assertIsNotNone(model)
34+
self.cleanUp()
35+
self.model = casbin.Model.NewModel()
36+
self.assertIsNotNone(self.model)
2737

2838
def test_NewModelFromFile(self):
29-
model = casbin.Model.NewModelFromFile(basic_model_path)
30-
self.assertIsNotNone(model)
39+
self.cleanUp()
40+
self.model = casbin.Model.NewModelFromFile(basic_model_path)
41+
self.assertIsNotNone(self.model)
3142

3243
def test_NewModelFromString(self):
44+
self.cleanUp()
3345
model_string = """[request_definition]
3446
r = sub, obj, act
3547
@@ -41,19 +53,40 @@ def test_NewModelFromString(self):
4153
4254
[matchers]
4355
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act"""
44-
model = casbin.Model.NewModelFromString(model_string)
45-
self.assertIsNotNone(model)
56+
self.model = casbin.Model.NewModelFromString(model_string)
57+
self.assertIsNotNone(self.model)
4658

4759
def test_LoadModelFromConfig(self):
48-
basic_config = casbin.Config.NewConfig(basic_model_path)
49-
model = casbin.Model.NewModel()
50-
model.LoadModelFromConfig(basic_config)
51-
# model = casbin.Model.NewModel()
52-
# config = casbin.Config.NewConfigFromText("")
53-
# model.LoadModelFromConfig(config)
60+
self.cleanUp()
61+
self.config = casbin.Config.NewConfig(basic_model_path)
62+
self.model = casbin.Model.NewModel()
63+
self.model.LoadModelFromConfig(self.config)
64+
self.model = casbin.Model.NewModel()
65+
self.config = casbin.Config.NewConfigFromText('')
66+
# self.assertRaises('', model.LoadModelFromConfig(basic_config))
5467

5568
def test_HasSection(self):
56-
# config = casbin.Config.NewConfig(basic_model_path)
57-
# model = casbin.Model.NewModel()
58-
# casbin.LoadModelFromConfig(model, basic_config)
69+
self.cleanUp()
70+
self.config = casbin.Config.NewConfig(basic_model_path)
71+
self.model = casbin.Model.NewModel()
72+
self.model.LoadModelFromConfig(self.config)
73+
for required_section in casbin.Model.required_sections:
74+
self.assertTrue(self.model.HasSection(required_section))
75+
76+
self.cleanUp()
77+
self.model = casbin.Model.NewModel()
78+
self.config = casbin.Config.NewConfigFromText('')
79+
# self.assertRaises('', model.LoadModelFromConfig(config))
80+
81+
for required_section in casbin.Model.required_sections:
82+
self.assertFalse(self.model.HasSection(required_section))
83+
84+
def test_ModelAddDef(self):
85+
self.cleanUp()
86+
self.model = casbin.Model.NewModel()
87+
s = 'r'
88+
v = 'sub, obj, act'
89+
90+
self.assertTrue(self.model.AddDef(s, s, v))
5991

92+
self.assertFalse(self.model.AddDef(s, s, ''))

0 commit comments

Comments
 (0)