more refactoring and added first test

This commit is contained in:
Radu Ioan Fericean
2013-08-01 18:19:18 +03:00
parent 63a1fc6f31
commit 6c5d73d41a
14 changed files with 340 additions and 204 deletions

View File

@@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"github.com/cgrates/cgrates/cache2go"
"github.com/cgrates/cgrates/history"
"github.com/cgrates/cgrates/utils"
"log/syslog"
"strings"
@@ -54,8 +55,39 @@ var (
debitPeriod = 10 * time.Second
roundingMethod = "*middle"
roundingDecimals = 4
historyScribe, _ = history.NewMockScribe()
)
// Exported method to set the storage getter.
func SetDataStorage(sg DataStorage) {
storageGetter = sg
}
// Sets the global rounding method and decimal precision for GetCost method
func SetRoundingMethodAndDecimals(rm string, rd int) {
roundingMethod = rm
roundingDecimals = rd
}
/*
Sets the database for logging (can be de same as storage getter or different db)
*/
func SetStorageLogger(sg DataStorage) {
storageLogger = sg
}
/*
Exported method to set the debit period for caching purposes.
*/
func SetDebitPeriod(d time.Duration) {
debitPeriod = d
}
// Exported method to set the history scribe.
func SetHistoryScribe(scribe history.Scribe) {
historyScribe = scribe
}
/*
The input stucture that contains call information.
*/
@@ -94,31 +126,6 @@ func (cd *CallDescriptor) getUserBalance() (ub *UserBalance, err error) {
return cd.userBalance, err
}
// Exported method to set the storage getter.
func SetDataStorage(sg DataStorage) {
storageGetter = sg
}
// Sets the global rounding method and decimal precision for GetCost method
func SetRoundingMethodAndDecimals(rm string, rd int) {
roundingMethod = rm
roundingDecimals = rd
}
/*
Sets the database for logging (can be de same as storage getter or different db)
*/
func SetStorageLogger(sg DataStorage) {
storageLogger = sg
}
/*
Exported method to set the debit period for caching purposes.
*/
func SetDebitPeriod(d time.Duration) {
debitPeriod = d
}
/*
Restores the activation periods for the specified prefix from storage.
*/

View File

@@ -21,7 +21,6 @@ package engine
import (
"encoding/json"
"github.com/cgrates/cgrates/cache2go"
//"log"
"reflect"
"testing"
)

38
engine/history_test.go Normal file
View File

@@ -0,0 +1,38 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"github.com/cgrates/cgrates/history"
"testing"
)
func TestHistory(t *testing.T) {
scribe := historyScribe.(*history.MockScribe)
expected := `[{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}}]
[{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}}]
[{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}}]
[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}},{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}}]
[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}},{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}},{"Key":"NAT","Object":{"Id":"NAT","Prefixes":["0256","0257","0723"]}}]
[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}},{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}},{"Key":"NAT","Object":{"Id":"NAT","Prefixes":["0256","0257","0723"]}},{"Key":"RET","Object":{"Id":"RET","Prefixes":["0723","0724"]}}]
[{"Key":"ALL","Object":{"Id":"ALL","Prefixes":["49","41","43"]}},{"Key":"GERMANY","Object":{"Id":"GERMANY","Prefixes":["49"]}},{"Key":"GERMANY_O2","Object":{"Id":"GERMANY_O2","Prefixes":["41"]}},{"Key":"GERMANY_PREMIUM","Object":{"Id":"GERMANY_PREMIUM","Prefixes":["43"]}},{"Key":"NAT","Object":{"Id":"NAT","Prefixes":["0256","0257","0723"]}},{"Key":"RET","Object":{"Id":"RET","Prefixes":["0723","0724"]}},{"Key":"nat","Object":{"Id":"nat","Prefixes":["0257","0256","0723"]}}]`
if scribe.Buf.String() != expected {
t.Error("Error in history content: ", scribe.Buf.String())
}
}

View File

@@ -70,6 +70,7 @@ func (ms *MapStorage) GetDestination(key string) (dest *Destination, err error)
func (ms *MapStorage) SetDestination(dest *Destination) (err error) {
result, err := ms.ms.Marshal(dest)
ms.dict[DESTINATION_PREFIX+dest.Id] = result
historyScribe.Record(dest.Id, dest)
return
}