|
| 1 | +package knowledge |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + Asset1 = Asset{Type: "type1", Key: "value1"} |
| 12 | + Asset2 = Asset{Type: "type2", Key: "value2"} |
| 13 | + Asset3 = Asset{Type: "type3", Key: "value3"} |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + Relation1 = Relation{From: AssetKey(Asset1), Type: "is_linked_to", To: AssetKey(Asset2)} |
| 18 | + Relation2 = Relation{From: AssetKey(Asset1), Type: "has_relation_with", To: AssetKey(Asset2)} |
| 19 | + Relation3 = Relation{From: AssetKey(Asset1), Type: "has_weird_relation_with", To: AssetKey(Asset3)} |
| 20 | +) |
| 21 | + |
| 22 | +func TestEncodeAssets(t *testing.T) { |
| 23 | + buff := bytes.NewBuffer(nil) |
| 24 | + |
| 25 | + encoder := NewGraphEncoder(buff) |
| 26 | + |
| 27 | + assert.NoError(t, encoder.EncodeAsset(Asset1)) |
| 28 | + assert.NoError(t, encoder.EncodeAsset(Asset2)) |
| 29 | + |
| 30 | + assert.Equal(t, |
| 31 | + "A{\"type\":\"type1\",\"key\":\"value1\"}\nA{\"type\":\"type2\",\"key\":\"value2\"}\n", |
| 32 | + string(buff.Bytes())) |
| 33 | +} |
| 34 | + |
| 35 | +func TestEncodeRelations(t *testing.T) { |
| 36 | + buff := bytes.NewBuffer(nil) |
| 37 | + |
| 38 | + encoder := NewGraphEncoder(buff) |
| 39 | + |
| 40 | + assert.NoError(t, encoder.EncodeRelation(Relation1)) |
| 41 | + assert.NoError(t, encoder.EncodeRelation(Relation2)) |
| 42 | + |
| 43 | + assert.Equal(t, |
| 44 | + "R{\"type\":\"is_linked_to\",\"from\":{\"type\":\"type1\",\"key\":\"value1\"},\"to\":{\"type\":\"type2\",\"key\":\"value2\"}}\nR{\"type\":\"has_relation_with\",\"from\":{\"type\":\"type1\",\"key\":\"value1\"},\"to\":{\"type\":\"type2\",\"key\":\"value2\"}}\n", |
| 45 | + string(buff.Bytes())) |
| 46 | +} |
| 47 | + |
| 48 | +func TestEncodeAssetsAndRelations(t *testing.T) { |
| 49 | + buff := bytes.NewBuffer(nil) |
| 50 | + |
| 51 | + encoder := NewGraphEncoder(buff) |
| 52 | + |
| 53 | + assert.NoError(t, encoder.EncodeRelation(Relation1)) |
| 54 | + assert.NoError(t, encoder.EncodeRelation(Relation2)) |
| 55 | + |
| 56 | + assert.Equal(t, |
| 57 | + "A{\"type\":\"type3\",\"key\":\"value3\"}\nR{\"type\":\"has_relation_with\",\"from\":{\"type\":\"type1\",\"key\":\"value1\"},\"to\":{\"type\":\"type2\",\"key\":\"value2\"}}\n", |
| 58 | + string(buff.Bytes())) |
| 59 | +} |
| 60 | + |
| 61 | +func TestEncodeAndDecode(t *testing.T) { |
| 62 | + buff := bytes.NewBuffer(nil) |
| 63 | + |
| 64 | + encoder := NewGraphEncoder(buff) |
| 65 | + |
| 66 | + assert.NoError(t, encoder.EncodeRelation(Relation1)) |
| 67 | + assert.NoError(t, encoder.EncodeRelation(Relation2)) |
| 68 | + assert.NoError(t, encoder.EncodeRelation(Relation3)) |
| 69 | + |
| 70 | + decoder := NewGraphDecoder(buff) |
| 71 | + |
| 72 | + graph := NewGraph() |
| 73 | + |
| 74 | + assert.NoError(t, decoder.Decode(graph)) |
| 75 | + |
| 76 | + assert.Len(t, graph.Assets(), 3) |
| 77 | + assert.Len(t, graph.Relations(), 3) |
| 78 | + |
| 79 | + assert.True(t, graph.HasAsset(Asset1)) |
| 80 | + assert.True(t, graph.HasAsset(Asset2)) |
| 81 | + assert.True(t, graph.HasAsset(Asset3)) |
| 82 | + |
| 83 | + assert.True(t, graph.HasRelation(Relation1)) |
| 84 | + assert.True(t, graph.HasRelation(Relation2)) |
| 85 | + assert.True(t, graph.HasRelation(Relation3)) |
| 86 | +} |
0 commit comments