forked from entrepreneur-interet-general/opensignauxfaibles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
128 lines (115 loc) · 5.12 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os/exec"
"testing"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
var _ = flag.Bool("update", false, "Update the expected test values in golden file") // please keep this line until https://github.com/kubernetes-sigs/service-catalog/issues/2319#issuecomment-425200065 is fixed
const (
mongoImage = "mongo:4.2@sha256:1c2243a5e21884ffa532ca9d20c221b170d7b40774c235619f98e2f6eaec520a"
mongoContainer = "sf-mongodb"
mongoPort = 27016
mongoDatabase = "signauxfaibles"
)
func TestPrincipal(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
startMongoContainer(t) // the test will fail in case of error
t.Cleanup(stopMongoContainer)
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.SetConfigName("config-sample") // => config will be loaded from ./config-sample.toml
mongoURI := fmt.Sprintf("mongodb://localhost:%v", mongoPort)
viper.Set("DB_DIAL", mongoURI)
viper.Set("DB", mongoDatabase)
mongodb, err := mgo.Dial(mongoURI)
if err != nil {
t.Fatal(err)
}
// mongodb.SetSocketTimeout(72000 * time.Second)
db := mongodb.DB(mongoDatabase)
t.Run("Toutes les commandes (CLI) doivent fonctionner à condition qu'un batch valide soit fourni", func(t *testing.T) {
// Insertion d'un Batch valide
db.C("Admin").Insert(bson.M{
"_id": bson.M{
"key": "1910",
"type": "batch",
},
"files": bson.M{
"admin_urssaf": []string{"/../lib/urssaf/testData/comptesTestData.csv"},
"paydex": []string{"/../lib/paydex/testData/paydexTestData.csv"},
},
"param": bson.M{
"date_debut": time.Date(2019, 0, 1, 0, 0, 0, 0, time.UTC), // ISODate("2019-01-01T00:00:00.000+0000"),
"date_fin": time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), // ISODate("2019-02-01T00:00:00.000+0000"),
},
})
// Exécution des commandes et vérification qu'elles s'achèvent toutes avec un exit code nul
assert.Equal(t, 0, runCLI("sfdata", "check", "--batch=1910"))
assert.Equal(t, 0, runCLI("sfdata", "import", "--batch=1910", "--no-filter"))
assert.Equal(t, 0, runCLI("sfdata", "validate", "--collection=ImportedData"))
// assert.Equal(t, 0, runCLI("sfdata", "compact", "--since-batch=1910"))
assert.Equal(t, 0, runCLI("sfdata", "public", "--until-batch=1910"))
assert.Equal(t, 0, runCLI("sfdata", "public", "--until-batch=1910", "--key=012345678")) // pour couvrir PublicOne()
assert.Equal(t, 0, runCLI("sfdata", "reduce", "--until-batch=1910"))
assert.Equal(t, 0, runCLI("sfdata", "reduce", "--until-batch=1910", "--key=012345678")) // pour couvrir ReduceOne()
assert.Equal(t, 0, runCLI("sfdata", "etablissements"))
assert.Equal(t, 0, runCLI("sfdata", "entreprises"))
// assert.Equal(t, 0, runCLI("sfdata", "purgeNotCompacted", "--i-understand-what-im-doing")) // pour couvrir PurgeNotCompacted()
// assert.Equal(t, 0, runCLI("sfdata", "purge", "--since-batch=1910", "--i-understand-what-im-doing"))
// assert.Equal(t, 0, runCLI("sfdata", "purge", "--since-batch=1910", "--i-understand-what-im-doing", "--debug-for-key=012345678")) // pour couvrir PurgeBatchOne()
assert.Equal(t, 0, runCLI("sfdata", "parseFile", "--parser=diane", "--file=lib/diane/testData/dianeTestData.txt"))
assert.Equal(t, 2, runCLI("sfdata", "check")) // => "Erreur: paramètre `batch` obligatoire."
assert.Equal(t, 3, runCLI("sfdata", "pruneEntities", "--batch=1910")) // => "Erreur: Ce batch ne spécifie pas de filtre"
})
// t.Run("Les données importées sont validées par MongoDB", func(t *testing.T) {
// // Création d'une collection associée au schéma de validation de données JSON de ImportedData
// colName := "FakeImportedData"
// err := engine.CreateImportedDataCollection(db, colName)
// if assert.NoError(t, err) {
// coll := db.C(colName)
// // L'insertion d'un document valide ne doit pas provoquer d'erreur
// assert.NoError(t, coll.Insert(bson.M{
// "value": bson.M{
// "scope": "entreprise",
// "key": "000000002",
// "batch": bson.M{
// "2002_2": bson.M{
// "paydex": bson.M{
// "afafafafafafaf": bson.M{"date_valeur": time.Now(), "nb_jours": 4},
// },
// },
// },
// },
// }))
// // L'insertion d'un document invalide doit provoquer une erreur
// assert.EqualError(t, coll.Insert(bson.M{"a": 1}), "Document failed validation")
// }
// })
}
func startMongoContainer(t *testing.T) {
t.Log("Starting MongoDB in Docker container...")
exec.Command("docker", "stop", mongoContainer).Run()
exec.Command("docker", "rm", mongoContainer).Run()
portMapping := fmt.Sprintf("%v:27017", mongoPort)
startMongoCommand := exec.Command("docker", "run", "--rm", "-d", "-p", portMapping, "--name", mongoContainer, mongoImage)
slog.Info("démarre mongo", slog.Any("command", startMongoCommand.Args))
err := startMongoCommand.Run()
if err != nil {
t.Fatalf("docker run: %v", err)
}
}
func stopMongoContainer() {
if err := exec.Command("docker", "stop", mongoContainer).Run(); err != nil {
log.Println(err) // affichage à titre informatif
}
}